Implementation:SeleniumHQ Selenium DevTools Command
| Knowledge Sources | |
|---|---|
| Domains | WebDriver, DevTools_Protocol |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
The Command class is a generic container representing a Chrome DevTools Protocol (CDP) command, encapsulating the method name, parameters, and a response mapper function.
Description
Command<X> models a single CDP command that can be sent to the browser through the DevTools session. Each command carries a method name (e.g., "Page.navigate"), an immutable map of parameters, and a Function<JsonInput, X> mapper used to deserialize the JSON response into the expected return type X. The class also supports a sendsResponse flag to handle edge-case CDP commands that do not return a response, via the doesNotSendResponse() factory method.
Usage
Use Command when constructing CDP commands to send via a DevTools session. In most cases, commands are created by the auto-generated CDP domain classes (e.g., Network.enable()), but custom commands can be constructed directly for unsupported or experimental CDP methods.
Code Reference
Source Location
- Repository: SeleniumHQ_Selenium
- File: java/src/org/openqa/selenium/devtools/Command.java
Signature
public class Command<X> {
public Command(String method, Map<String, Object> params)
public Command(String method, Map<String, Object> params, Type typeOfX)
public Command(String method, Map<String, Object> params, Function<JsonInput, X> mapper)
public String getMethod()
public Map<String, Object> getParams()
public boolean getSendsResponse()
public Command<X> doesNotSendResponse()
}
Import
import org.openqa.selenium.devtools.Command;
I/O Contract
Constructor Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
method |
String |
Yes | The CDP method name (e.g., "Page.navigate")
|
params |
Map<String, Object> |
Yes | The command parameters as key-value pairs |
typeOfX |
java.lang.reflect.Type |
No | The type to deserialize the response into (defaults to Object.class)
|
mapper |
Function<JsonInput, X> |
No | Custom function to deserialize the JSON response |
Return Values
| Method | Return Type | Description |
|---|---|---|
getMethod() |
String |
The CDP method name |
getParams() |
Map<String, Object> |
Immutable copy of command parameters |
getSendsResponse() |
boolean |
Whether the command expects a response from the browser |
doesNotSendResponse() |
Command<X> |
Returns a new Command instance flagged as not expecting a response |
Usage Examples
// Creating a command with a type-based mapper
Command<String> navigateCommand = new Command<>(
"Page.navigate",
Map.of("url", "https://example.com"),
String.class
);
// Creating a command with a custom mapper
Command<Boolean> customCommand = new Command<>(
"Emulation.setDeviceMetricsOverride",
Map.of("width", 1024, "height", 768, "mobile", false),
input -> input.read(Boolean.class)
);
// Sending a command via DevTools session
DevTools devTools = ((HasDevTools) driver).getDevTools();
devTools.createSession();
devTools.send(navigateCommand);
// Handling a command that does not send a response
Command<Void> fireAndForget = new Command<>(
"SomeMethod",
Map.of()
).doesNotSendResponse();
Related Pages
- DevTools Event -- companion class for CDP event subscriptions
- DevTools ConverterFunctions -- utility functions for building response mappers
- Separation of Concerns -- Command encapsulates method, params, and response mapping in a single object
- Immutability -- parameter maps are defensively copied via
Map.copyOf()