下面列出了怎么用org.openqa.selenium.remote.CommandInfo的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Returns an {@link HttpCommandExecutor} to a Selenium grid (e.g. SauceLabs) that contains basic authentication for
* access
*
* @param testEnvironment
* The {@link TestEnvironment} to the grid
* @return {@link HttpCommandExecutor} to Selenium grid augmented with credentials
* @throws MalformedURLException
* if the given gridUrl is invalid
*/
protected static HttpCommandExecutor createGridExecutor(String testEnvironment) throws MalformedURLException
{
TestEnvironment testEnvironmentProperties = MultibrowserConfiguration.getInstance().getTestEnvironment(testEnvironment);
if (testEnvironmentProperties == null)
{
throw new IllegalArgumentException("No properties found for test environment: \"" + testEnvironment + "\"");
}
final Map<String, CommandInfo> additionalCommands = new HashMap<String, CommandInfo>(); // just a dummy
URL gridUrl = new URL(testEnvironmentProperties.getUrl());
return new HttpCommandExecutor(additionalCommands, gridUrl, new NeodymiumProxyHttpClientFactory(testEnvironmentProperties));
}
public BufferedImage takeScreenshotEntirePageUsingChromeCommand() {
//should use devicePixelRatio by default as chrome command executor makes screenshot account for that
Object devicePixelRatio = executeJsScript(DEVICE_PIXEL_RATIO);
this.devicePixelRatio = devicePixelRatio instanceof Double ? (Double) devicePixelRatio : (Long) devicePixelRatio * 1.0;
defineCustomCommand("sendCommand", new CommandInfo("/session/:sessionId/chromium/send_command_and_get_result", HttpMethod.POST));
int verticalIterations = (int) Math.ceil(((double) this.getDocHeight()) / this.getViewportHeight());
for (int j = 0; j < verticalIterations; j++) {
this.scrollTo(0, j * this.getViewportHeight());
wait(betweenScrollTimeout);
}
Object metrics = this.evaluate(FileUtil.getJsScript(ALL_METRICS));
this.sendCommand("Emulation.setDeviceMetricsOverride", metrics);
wait(beforeShootCondition,beforeShootTimeout);
Object result = this.sendCommand("Page.captureScreenshot", ImmutableMap.of("format", "png", "fromSurface", true));
this.sendCommand("Emulation.clearDeviceMetricsOverride", ImmutableMap.of());
return decodeBase64EncodedPng((String) ((Map<String, ?>) result).get("data"));
}
public BufferedImage takeScreenshotEntirePageUsingGeckoDriver() {
// Check geckodriver version (>= 0.24.0 is requried)
String version = (String) ((RemoteWebDriver) driver).getCapabilities().getCapability("moz:geckodriverVersion");
if (version == null || Version.valueOf(version).satisfies("<0.24.0")) {
return takeScreenshotEntirePageDefault();
}
defineCustomCommand("mozFullPageScreenshot", new CommandInfo("/session/:sessionId/moz/screenshot/full", HttpMethod.GET));
Object result = this.executeCustomCommand("mozFullPageScreenshot");
String base64EncodedPng;
if (result instanceof String) {
base64EncodedPng = (String) result;
} else if (result instanceof byte[]) {
base64EncodedPng = new String((byte[]) result);
} else {
throw new RuntimeException(String.format("Unexpected result for /moz/screenshot/full command: %s",
result == null ? "null" : result.getClass().getName() + "instance"));
}
return decodeBase64EncodedPng(base64EncodedPng);
}
private void defineCustomCommand(String name, CommandInfo info) {
try {
Method defineCommand = HttpCommandExecutor.class.getDeclaredMethod("defineCommand", String.class, CommandInfo.class);
defineCommand.setAccessible(true);
defineCommand.invoke(((RemoteWebDriver) this.driver).getCommandExecutor(), name, info);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
private AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands, DriverService service,
URL addressOfRemoteServer,
HttpClient.Factory httpClientFactory) {
super(additionalCommands,
ofNullable(service)
.map(DriverService::getUrl)
.orElse(addressOfRemoteServer), httpClientFactory);
serviceOptional = ofNullable(service);
}
private static Map<String, CommandInfo> buildChromiumCommandMappings(String vendorKeyword) {
String sessionPrefix = "/session/:sessionId/";
String chromiumPrefix = sessionPrefix + "chromium";
String vendorPrefix = sessionPrefix + vendorKeyword;
HashMap<String, CommandInfo> mappings = new HashMap<>();
mappings.put(ChromiumDriverCommand.LAUNCH_APP,
new CommandInfo(chromiumPrefix + "/launch_app", HttpMethod.POST));
String networkConditions = chromiumPrefix + "/network_conditions";
mappings.put(ChromiumDriverCommand.GET_NETWORK_CONDITIONS,
new CommandInfo(networkConditions, HttpMethod.GET));
mappings.put(ChromiumDriverCommand.SET_NETWORK_CONDITIONS,
new CommandInfo(networkConditions, HttpMethod.POST));
mappings.put(ChromiumDriverCommand.DELETE_NETWORK_CONDITIONS,
new CommandInfo(networkConditions, HttpMethod.DELETE));
mappings.put( ChromiumDriverCommand.EXECUTE_CDP_COMMAND,
new CommandInfo(vendorPrefix + "/cdp/execute", HttpMethod.POST));
// Cast / Media Router APIs
String cast = vendorPrefix + "/cast";
mappings.put(ChromiumDriverCommand.GET_CAST_SINKS,
new CommandInfo(cast + "/get_sinks", HttpMethod.GET));
mappings.put(ChromiumDriverCommand.SET_CAST_SINK_TO_USE,
new CommandInfo(cast + "/set_sink_to_use", HttpMethod.POST));
mappings.put(ChromiumDriverCommand.START_CAST_TAB_MIRRORING,
new CommandInfo(cast + "/start_tab_mirroring", HttpMethod.POST));
mappings.put(ChromiumDriverCommand.GET_CAST_ISSUE_MESSAGE,
new CommandInfo(cast + "/get_issue_message", HttpMethod.GET));
mappings.put(ChromiumDriverCommand.STOP_CASTING,
new CommandInfo(cast + "/stop_casting", HttpMethod.POST));
mappings.put(ChromiumDriverCommand.SET_PERMISSION,
new CommandInfo(sessionPrefix + "/permissions", HttpMethod.POST));
return unmodifiableMap(mappings);
}
private EventFiringAppiumCommandExecutor(Map<String, CommandInfo> additionalCommands, DriverService service,
URL addressOfRemoteServer,
HttpClient.Factory httpClientFactory) {
super(additionalCommands,
ofNullable(service)
.map(DriverService::getUrl)
.orElse(addressOfRemoteServer),
httpClientFactory);
serviceOptional = ofNullable(service);
}
private static void setCommand(CommandExecutor executor) throws Exception {
CommandInfo cmd = new CommandInfo("/session/:sessionId/chromium/send_command_and_get_result", POST);
Method defineCommand = HttpCommandExecutor.class.getDeclaredMethod("defineCommand", String.class, CommandInfo.class);
defineCommand.setAccessible(true);
defineCommand.invoke(executor, "sendCommand", cmd);
}
public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands, DriverService service,
HttpClient.Factory httpClientFactory) {
this(additionalCommands, checkNotNull(service), null, httpClientFactory);
}
public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands,
URL addressOfRemoteServer, HttpClient.Factory httpClientFactory) {
this(additionalCommands, null, checkNotNull(addressOfRemoteServer), httpClientFactory);
}
public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands,
URL addressOfRemoteServer) {
this(additionalCommands, addressOfRemoteServer, HttpClient.Factory.createDefault());
}
public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands,
DriverService service) {
this(additionalCommands, service, HttpClient.Factory.createDefault());
}
protected Map<String, CommandInfo> getAdditionalCommands() {
//noinspection unchecked
return getPrivateFieldValue("additionalCommands", Map.class);
}
public EventFiringAppiumCommandExecutor(Map<String, CommandInfo> additionalCommands, DriverService service,
HttpClient.Factory httpClientFactory) {
this(additionalCommands, checkNotNull(service), null, httpClientFactory);
}
public EventFiringAppiumCommandExecutor(Map<String, CommandInfo> additionalCommands,
URL addressOfRemoteServer, HttpClient.Factory httpClientFactory) {
this(additionalCommands, null, checkNotNull(addressOfRemoteServer), httpClientFactory);
}
public EventFiringAppiumCommandExecutor(Map<String, CommandInfo> additionalCommands,
URL addressOfRemoteServer) {
this(additionalCommands, addressOfRemoteServer, new HttpClientFactoryCustom());
}
public EventFiringAppiumCommandExecutor(Map<String, CommandInfo> additionalCommands,
DriverService service) {
this(additionalCommands, service, new HttpClientFactoryCustom());
}
private Map<String, CommandInfo> getAdditionalCommands() {
// noinspection unchecked
return getPrivateFieldValue("additionalCommands", Map.class);
}
/**
* Creates an {@link DriverCommandExecutor} that supports non-standard
* {@code additionalCommands} in addition to the standard.
*
* @param service driver server
* @param additionalCommands additional commands the remote end can process
*/
protected DriverCommandExecutor(
DriverService service, Map<String, CommandInfo> additionalCommands) {
super(additionalCommands, service.getUrl());
this.service = service;
}