下面列出了怎么用org.openqa.selenium.remote.CommandExecutor的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* コンストラクタ
*
* @param executor CommandExecutor
* @param capabilities Capability
*/
PtlIPhoneDriver(CommandExecutor executor, PtlCapabilities capabilities) {
super(executor, capabilities);
Object headerHeightCapability = capabilities.getCapability("headerHeight");
if (headerHeightCapability == null) {
throw new TestRuntimeException("Capability \"headerHeight\" is required");
} else {
if (headerHeightCapability instanceof Number) {
headerHeight = ((Number) headerHeightCapability).intValue();
} else {
headerHeight = Integer.parseInt(headerHeightCapability.toString());
}
}
Object footerHeightCapability = capabilities.getCapability("footerHeight");
if (footerHeightCapability == null) {
throw new TestRuntimeException("Capability \"footerHeight\" is required");
} else {
if (footerHeightCapability instanceof Number) {
footerHeight = ((Number) footerHeightCapability).intValue();
} else {
footerHeight = Integer.parseInt(footerHeightCapability.toString());
}
}
}
@Test
public void shouldGetMeaningfulExceptionOnBrowserDeath() throws Exception {
FirefoxDriver driver2 = new FirefoxDriver();
driver2.get(pages.formPage);
// Grab the command executor
CommandExecutor keptExecutor = driver2.getCommandExecutor();
SessionId sessionId = driver2.getSessionId();
try {
Field field = RemoteWebDriver.class.getDeclaredField("executor");
field.setAccessible(true);
CommandExecutor spoof = mock(CommandExecutor.class);
doThrow(new IOException("The remote server died"))
.when(spoof).execute(ArgumentMatchers.any());
field.set(driver2, spoof);
driver2.get(pages.formPage);
fail("Should have thrown.");
} catch (UnreachableBrowserException e) {
assertThat(e.getMessage()).contains("Error communicating with the remote browser");
} finally {
keptExecutor.execute(new Command(sessionId, DriverCommand.QUIT));
}
}
@Test
public void shouldIncludeRemoteInfoForWrappedDriverTimeout() throws IOException {
Capabilities caps = new MutableCapabilities();
Response response = new Response(new SessionId("foo"));
response.setValue(caps.asMap());
CommandExecutor executor = mock(CommandExecutor.class);
when(executor.execute(any(Command.class))).thenReturn(response);
RemoteWebDriver driver = new RemoteWebDriver(executor, caps);
WebDriver testDriver = mock(WebDriver.class, withSettings().extraInterfaces(WrapsDriver.class));
when(((WrapsDriver) testDriver).getWrappedDriver()).thenReturn(driver);
TickingClock clock = new TickingClock();
WebDriverWait wait =
new WebDriverWait(testDriver, Duration.ofSeconds(1), Duration.ofMillis(200), clock, clock);
assertThatExceptionOfType(TimeoutException.class)
.isThrownBy(() -> wait.until(d -> false))
.withMessageContaining("Capabilities {javascriptEnabled: true, platform: ANY, platformName: ANY}")
.withMessageContaining("Session ID: foo");
}
protected RemoteSession(
Dialect downstream,
Dialect upstream,
HttpHandler codec,
SessionId id,
Map<String, Object> capabilities) {
this.downstream = Require.nonNull("Downstream dialect", downstream);
this.upstream = Require.nonNull("Upstream dialect", upstream);
this.codec = Require.nonNull("Codec", codec);
this.id = Require.nonNull("Session id", id);
this.capabilities = Require.nonNull("Capabilities", capabilities);
File tempRoot = new File(StandardSystemProperty.JAVA_IO_TMPDIR.value(), id.toString());
Require.stateCondition(tempRoot.mkdirs(), "Could not create directory %s", tempRoot);
this.filesystem = TemporaryFilesystem.getTmpFsBasedOn(tempRoot);
CommandExecutor executor = new ActiveSessionCommandExecutor(this);
this.driver = new Augmenter().augment(new RemoteWebDriver(
executor,
new ImmutableCapabilities(getCapabilities())));
}
private HttpClient extractHttpClient(RemoteWebDriver driver) {
CommandExecutor executor = driver.getCommandExecutor();
try {
Field client = null;
Class<?> current = executor.getClass();
while (client == null && (current != null || Object.class.equals(current))) {
client = findClientField(current);
current = current.getSuperclass();
}
if (client == null) {
throw new IllegalStateException("Unable to find client field in " + executor.getClass());
}
if (!HttpClient.class.isAssignableFrom(client.getType())) {
throw new IllegalStateException("Client field is not assignable to http client");
}
client.setAccessible(true);
return (HttpClient) client.get(executor);
} catch (ReflectiveOperationException e) {
throw new IllegalStateException(e);
}
}
default <T extends RemoteWebDriver> T getDriver(Class<T> driverClass, CommandExecutor commandExecutor, Capabilities capabilities) {
return AqualityServices.get(IActionRetrier.class).doWithRetry(() -> {
try {
if (commandExecutor != null) {
return driverClass.getDeclaredConstructor(CommandExecutor.class, Capabilities.class).newInstance(commandExecutor, capabilities);
}
return driverClass.getDeclaredConstructor(Capabilities.class).newInstance(capabilities);
} catch (ReflectiveOperationException e) {
throw new UnsupportedOperationException(String.format("Cannot instantiate driver with type '%1$s'.", driverClass), e);
}
}, Collections.emptyList());
}
private RemoteWebDriver createRemoteDriver(Capabilities capabilities) {
AqualityServices.getLocalizedLogger().info("loc.browser.grid");
ClientFactory clientFactory = new ClientFactory();
CommandExecutor commandExecutor = new HttpCommandExecutor(
ImmutableMap.of(),
browserProfile.getRemoteConnectionUrl(),
clientFactory);
RemoteWebDriver driver = getDriver(RemoteWebDriver.class, commandExecutor, capabilities);
driver.setFileDetector(new LocalFileDetector());
return driver;
}
private void setCodec() {
try {
CommandExecutor executor = getCommandExecutor();
setField("commandCodec", executor, Dialect.W3C.getCommandCodec());
setField("responseCodec", executor, Dialect.W3C.getResponseCodec());
} catch (Throwable e) {
logger.error("Unable to set W3C codec", e);
}
}
public UiDriver getDriver(WebDriverCommandLogger cmdLogger, String[] stb) {
String browser = STBArgs.browser_str.getFrom(stb);
String baseUrl = STBArgs.base_url.getFrom(stb);
QAFCommandProcessor commandProcessor =
new SeleniumCommandProcessor(STBArgs.sel_server.getFrom(stb),
Integer.parseInt(STBArgs.port.getFrom(stb)),
browser.split("_")[0], baseUrl);
CommandExecutor executor = getObject(commandProcessor);
QAFExtendedWebDriver driver =
new QAFExtendedWebDriver(executor, new DesiredCapabilities(), cmdLogger);
QAFWebDriverBackedSelenium selenium =
new QAFWebDriverBackedSelenium(commandProcessor, driver);
commandProcessor.addListener(new SubmitCommandListener());
commandProcessor.addListener(new SeleniumCommandLogger(new ArrayList<LoggingBean>()));
commandProcessor.addListener(new AutoWaitInjector());
if (browser.contains("iexproper") || browser.contains("iehta")) {
commandProcessor.addListener(new IEScreenCaptureListener());
}
String listners = ApplicationProperties.SELENIUM_CMD_LISTENERS.getStringVal("");
if (!listners.equalsIgnoreCase("")) {
commandProcessor.addListener(listners.split(","));
}
return selenium;
}
private CommandExecutor getObject(Object commandProcessor) {
try {
Class<?> clazz = Class.forName("org.openqa.selenium.SeleneseCommandExecutor");
Class<?> commandProcessorclazz =
Class.forName("com.thoughtworks.selenium.CommandProcessor");
Constructor<?> ctor = clazz.getConstructor(commandProcessorclazz);
return (CommandExecutor) ctor.newInstance(new Object[]{commandProcessor});
} catch (Exception e) {
throw new RuntimeException(e.getMessage()
+ "SeleneseCommandExecutor is not available. Please try with selenium 2.32 or older.");
}
}
/**
* コンストラクタ
*
* @param executor CommandExecutor
* @param capabilities Capability
*/
PtlInternetExplorerDriver(CommandExecutor executor, PtlCapabilities capabilities) {
super(executor, capabilities);
Object chromeWidthCapability = capabilities.getCapability("chromeWidth");
if (chromeWidthCapability == null) {
throw new TestRuntimeException("Capability \"chromeWidth\" is required.");
}
if (chromeWidthCapability instanceof Number) {
chromeWidth = ((Number) chromeWidthCapability).intValue();
} else {
chromeWidth = Integer.parseInt(chromeWidthCapability.toString());
}
}
/**
* コンストラクタ
*
* @param executor CommandExecutor
* @param capabilities Capability
*/
PtlIPadDriver(CommandExecutor executor, PtlCapabilities capabilities) {
super(executor, capabilities);
Object headerHeightCapability = capabilities.getCapability("headerHeight");
if (headerHeightCapability == null) {
throw new TestRuntimeException("Capability \"headerHeight\" is required");
} else {
if (headerHeightCapability instanceof Number) {
headerHeight = ((Number) headerHeightCapability).intValue();
} else {
headerHeight = Integer.parseInt(headerHeightCapability.toString());
}
}
}
/**
* コンストラクタ
*
* @param executor CommandExecutor
* @param capabilities Capability
*/
protected PtlWebDriver(CommandExecutor executor, PtlCapabilities capabilities) {
super(executor, capabilities);
this.capabilities = capabilities;
// JsonToWebElementConverterを上書きすることでfindElementから取得されるRemoteWebElementを差し替え
setElementConverter(new JsonToPtlWebElementConverter(this));
}
protected ChromiumDriver(CommandExecutor commandExecutor, Capabilities capabilities, String capabilityKey) {
super(commandExecutor, capabilities);
locationContext = new RemoteLocationContext(getExecuteMethod());
webStorage = new RemoteWebStorage(getExecuteMethod());
touchScreen = new RemoteTouchScreen(getExecuteMethod());
networkConnection = new RemoteNetworkConnection(getExecuteMethod());
HttpClient.Factory factory = HttpClient.Factory.createDefault();
connection = ChromiumDevToolsLocator.getChromeConnector(
factory,
getCapabilities(),
capabilityKey);
devTools = connection.map(DevTools::new);
}
@Override
public Void call() throws Exception {
RemoteWebDriver driver = (RemoteWebDriver) getUnwrappedDriver();
CommandExecutor executor = (driver).getCommandExecutor();
long start = System.currentTimeMillis();
Command command = new Command(driver.getSessionId(), ACTIONS, allParameters);
Response response = executor.execute(command);
new ErrorHandler(true)
.throwIfResponseFailed(response, System.currentTimeMillis() - start);
return null;
}
public MobileRecordingListener(CommandExecutor commandExecutor, O1 startRecordingOpt, O2 stopRecordingOpt,
TestArtifactType artifact) {
this.commandExecutor = commandExecutor;
this.startRecordingOpt = startRecordingOpt;
this.stopRecordingOpt = stopRecordingOpt;
this.videoArtifact = artifact;
}
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 QAFExtendedWebDriver(CommandExecutor cmdExecutor, Capabilities capabilities,
WebDriverCommandLogger reporter) {
super(cmdExecutor, capabilities);
init(reporter);
}
@Override
public PtlWebDriver createReusableWebDriver(CommandExecutor executor) {
return new PtlFirefoxDriver(executor, getCapabilities());
}
@Override
public PtlWebDriver createReusableWebDriver(CommandExecutor executor) {
return new PtlChromeDriver(executor, getCapabilities());
}
@Override
public PtlWebDriver createReusableWebDriver(CommandExecutor executor) {
return new PtlInternetExplorerDriver(executor, getCapabilities());
}
@Override
public PtlWebDriver createReusableWebDriver(CommandExecutor executor) {
return new PtlInternetExplorer7Driver(executor, getCapabilities());
}
@Override
public PtlWebDriver createReusableWebDriver(CommandExecutor executor) {
return new PtlInternetExplorer8Driver(executor, getCapabilities());
}
@Override
public PtlWebDriver createReusableWebDriver(CommandExecutor executor) {
return new PtlEdgeDriver(executor, getCapabilities());
}
@Override
public PtlWebDriver createReusableWebDriver(CommandExecutor executor) {
return new PtlSafariDriver(executor, getCapabilities());
}
@Override
public PtlWebDriver createReusableWebDriver(CommandExecutor executor) {
return new PtlIPhoneDriver(executor, getCapabilities());
}
@Override
public PtlWebDriver createReusableWebDriver(CommandExecutor executor) {
return new PtlIPadDriver(executor, getCapabilities());
}
@Override
public PtlWebDriver createReusableWebDriver(CommandExecutor executor) {
return new PtlAndroidDriver(executor, getCapabilities());
}
@Override
public PtlWebDriver createReusableWebDriver(CommandExecutor executor) {
return new PtlSelendroidDriver(executor, getCapabilities());
}
public xRemoteWebDriver(CommandExecutor executor, Capabilities desiredCapabilities,Log log)
{
super(executor, desiredCapabilities);
}