下面列出了怎么用org.openqa.selenium.firefox.GeckoDriverService的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void copyDependentSystemProperties() {
copyVariableToDefaultLocation(Constants.CHROME_WEB_DRIVER_LOCATION_SYSTEM_PROPERTY);
copyVariableToDefaultLocation(Constants.CHROME_EXECUTABLE_LOCATION_SYSTEM_PROPERTY);
copyVariableToDefaultLocation(Constants.OPERA_WEB_DRIVER_LOCATION_SYSTEM_PROPERTY);
copyVariableToDefaultLocation(Constants.EDGE_WEB_DRIVER_LOCATION_SYSTEM_PROPERTY);
copyVariableToDefaultLocation(Constants.PHANTOM_JS_BINARY_PATH_SYSTEM_PROPERTY);
copyVariableToDefaultLocation(Constants.IE_WEB_DRIVER_LOCATION_SYSTEM_PROPERTY);
/*
Firefox driver system properties
*/
copyVariableToDefaultLocation(Constants.FIREFOX_PROFILE_SYSTEM_PROPERTY);
copyVariableToDefaultLocation(Constants.FIREFOX_EXECUTABLE_LOCATION_SYSTEM_PROPERTY);
copyVariableToDefaultLocation(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE);
copyVariableToDefaultLocation(FirefoxDriver.SystemProperty.BROWSER_LIBRARY_PATH);
copyVariableToDefaultLocation(FirefoxDriver.SystemProperty.BROWSER_LOGFILE);
copyVariableToDefaultLocation(FirefoxDriver.SystemProperty.DRIVER_XPI_PROPERTY);
/*
Marionette driver system properties
*/
copyVariableToDefaultLocation(GeckoDriverService.GECKO_DRIVER_EXE_PROPERTY);
}
@Test
public void builderPassesTimeoutToDriverService() {
File exe = new File("someFile");
Duration defaultTimeout = Duration.ofSeconds(20);
Duration customTimeout = Duration.ofSeconds(60);
GeckoDriverService.Builder builderMock = spy(GeckoDriverService.Builder.class);
doReturn(exe).when(builderMock).findDefaultExecutable();
builderMock.build();
verify(builderMock).createDriverService(any(), anyInt(), eq(defaultTimeout), any(), any());
builderMock.withTimeout(customTimeout);
builderMock.build();
verify(builderMock).createDriverService(any(), anyInt(), eq(customTimeout), any(), any());
}
@Test
@Ignore
public void shouldPreferMarionette() {
// Make sure we have at least one of the services available
Capabilities caps = new FirefoxOptions();
RemoteWebDriverBuilder.Plan plan = RemoteWebDriver.builder()
.addAlternative(caps)
.getPlan();
assertThat(new XpiDriverService.Builder().score(caps)).isEqualTo(0);
assertThat(new GeckoDriverService.Builder().score(caps)).isEqualTo(1);
assertThat(plan.isUsingDriverService()).isTrue();
assertThat(plan.getDriverService().getClass()).isEqualTo(GeckoDriverService.class);
}
@Override
public WebDriver createWebDriver(String webDriverPath) {
FirefoxBinary ffox = new FirefoxBinary();
if ("true".equals(System.getenv("TRAVIS"))) {
// xvfb is supposed to run with DISPLAY 99
ffox.setEnvironmentProperty("DISPLAY", ":99");
}
ffox.addCommandLineOptions("--headless");
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir",
FileUtils.getTempDirectory().toString() + "/firefox/");
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("browser.download.manager.showAlertOnComplete", false);
profile.setPreference("browser.download.manager.closeWhenDone", true);
profile.setPreference("app.update.auto", false);
profile.setPreference("app.update.enabled", false);
profile.setPreference("dom.max_script_run_time", 0);
profile.setPreference("dom.max_chrome_script_run_time", 0);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/x-ustar,application/octet-stream,application/zip,text/csv,text/plain");
profile.setPreference("network.proxy.type", 0);
System.setProperty(
GeckoDriverService.GECKO_DRIVER_EXE_PROPERTY, webDriverPath);
System.setProperty(
FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE, "true");
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setBinary(ffox);
firefoxOptions.setProfile(profile);
firefoxOptions.setLogLevel(FirefoxDriverLogLevel.TRACE);
return new FirefoxDriver(firefoxOptions);
}
private static WebDriver setupFirefoxDriver() throws MalformedURLException {
LOG.i("Setting up Firefox driver...");
setupDriver(firefoxdriver());
final DesiredCapabilities capabilities = new DesiredCapabilities();
final FirefoxOptions options = new FirefoxOptions(capabilities);
final GeckoDriverService firefoxService = GeckoDriverService.createDefaultService();
return new FirefoxDriver(firefoxService, options);
}
@Override
protected FirefoxDriver createBrowser() {
FirefoxOptions desiredCapabilities = new FirefoxOptions(createCapabilities());
desiredCapabilities.setCapability(FirefoxDriver.PROFILE, createProfile());
return new FirefoxDriver(new GeckoDriverService.Builder().usingFirefoxBinary(new FirefoxBinary()).build(),
desiredCapabilities);
}
@Test
public void shouldCreateFirefox() throws Exception {
FirefoxDriver mockFirefoxDriver = Mockito.mock(FirefoxDriver.class);
whenNew(FirefoxDriver.class)
.withParameterTypes(GeckoDriverService.class, FirefoxOptions.class)
.withArguments(isA(GeckoDriverService.class), isA(FirefoxOptions.class))
.thenReturn(mockFirefoxDriver);
final FirefoxDriver browser = config.createBrowser();
assertThat(browser, is(mockFirefoxDriver));
verifyNew(FirefoxDriver.class, times(1)).withArguments(isA(GeckoDriverService.class), isA(FirefoxOptions.class));
}
@Override
public DriverService createService(int port) {
GeckoDriverService.Builder builder = new GeckoDriverService.Builder();
BrowserConfig config = BrowserConfig.instance();
String environ = config.getValue(BROWSER, "browser-environment");
if (environ != null) {
Map<String, String> envMap = new HashMap<>();
BufferedReader reader = new BufferedReader(new StringReader(environ));
String line = null;
try {
while ((line = reader.readLine()) != null) {
String[] parts = line.split("=");
if (parts != null && parts.length == 2) {
envMap.put(parts[0], parts[1]);
}
}
} catch (IOException e) {
}
builder.withEnvironment(envMap);
}
String wdPath = config.getValue(BROWSER, "webdriver-exe-path");
if (wdPath != null)
builder.usingDriverExecutable(new File(wdPath));
String logFile = config.getValue(BROWSER, "webdriver-log-file-path");
if (logFile != null) {
builder.withLogFile(new File(logFile));
}
return builder.usingPort(port).build();
}
@Override
public void configureWebDriver(@NotNull final List<File> tempFiles) {
checkNotNull(tempFiles);
final boolean useSuppliedWebDrivers =
SYSTEM_PROPERTY_UTILS.getPropertyAsBoolean(
Constants.USE_SUPPLIED_WEBDRIVERS, true);
final boolean is64BitOS = OS_DETECTION.is64BitOS();
if (SystemUtils.IS_OS_WINDOWS) {
configureWindows(tempFiles, is64BitOS, useSuppliedWebDrivers);
} else if (SystemUtils.IS_OS_MAC) {
configureMac(tempFiles, is64BitOS, useSuppliedWebDrivers);
} else if (SystemUtils.IS_OS_LINUX) {
configureLinux(tempFiles, is64BitOS, useSuppliedWebDrivers);
}
/*
Log some details about the diver locations
*/
Stream.of(Constants.CHROME_WEB_DRIVER_LOCATION_SYSTEM_PROPERTY,
Constants.OPERA_WEB_DRIVER_LOCATION_SYSTEM_PROPERTY,
Constants.IE_WEB_DRIVER_LOCATION_SYSTEM_PROPERTY,
Constants.PHANTOM_JS_BINARY_PATH_SYSTEM_PROPERTY,
GeckoDriverService.GECKO_DRIVER_EXE_PROPERTY,
Constants.EDGE_WEB_DRIVER_LOCATION_SYSTEM_PROPERTY)
.forEach(x -> LOGGER.info(
"WEBAPPTESTER-INFO-0004: System property {}: {}",
x,
System.getProperty(x)));
}
private void setGeckoDriverPath() {
String geckoDriverPath = "";
if (SystemUtils.IS_OS_MAC || SystemUtils.IS_OS_MAC_OSX) {
geckoDriverPath = "resources/geckodriver_mac";
} else if (SystemUtils.IS_OS_UNIX) {
if (System.getProperty("os.arch").contains("64")
|| System.getProperty("sun.arch.data.model").equals("64")) {
geckoDriverPath = "resources/geckodriver_64";
} else {
geckoDriverPath = "resources/geckodriver_32";
}
} else if (SystemUtils.IS_OS_WINDOWS) {
try {
if (System.getProperty("os.arch").contains("64")
|| System.getProperty("sun.arch.data.model").equals("64"))
geckoDriverPath = "resources/geckodriver_64.exe";
else
geckoDriverPath = "resources/geckodriver_32.exe";
} catch (Exception e) {
geckoDriverPath = "resources/geckodriver_64.exe";
}
} else {
throw new IllegalArgumentException("Geckodriver is not supported in the current OS");
}
File geckoDriverFile = new File(geckoDriverPath);
// if above property is not working or not opening the application in browser
// then try below property
System.setProperty(GeckoDriverService.GECKO_DRIVER_EXE_PROPERTY, geckoDriverFile.getAbsolutePath());
System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE, "true");
}
private void configureWindows(
@NotNull final List<File> tempFiles,
final boolean is64BitOS,
final boolean useSuppliedWebDrivers) {
try {
final boolean marionetteWebDriverSet =
StringUtils.isNotBlank(
SYSTEM_PROPERTY_UTILS.getProperty(
GeckoDriverService.GECKO_DRIVER_EXE_PROPERTY));
if (useSuppliedWebDrivers && !marionetteWebDriverSet) {
System.setProperty(
GeckoDriverService.GECKO_DRIVER_EXE_PROPERTY,
extractZipDriver(
"/drivers/win64/marionette/geckodriver.exe.tar.gz",
"geckodriver.exe",
tempFiles));
}
final boolean chromeWebDriverSet =
StringUtils.isNotBlank(
SYSTEM_PROPERTY_UTILS.getProperty(
Constants.CHROME_WEB_DRIVER_LOCATION_SYSTEM_PROPERTY));
if (useSuppliedWebDrivers && !chromeWebDriverSet) {
System.setProperty(
Constants.CHROME_WEB_DRIVER_LOCATION_SYSTEM_PROPERTY,
extractDriver(
"/drivers/win32/chrome/chromedriver.exe",
"chrome.exe",
tempFiles));
}
final boolean operaWebDriverSet =
StringUtils.isNotBlank(
SYSTEM_PROPERTY_UTILS.getProperty(
Constants.OPERA_WEB_DRIVER_LOCATION_SYSTEM_PROPERTY));
if (useSuppliedWebDrivers && !operaWebDriverSet) {
System.setProperty(
Constants.OPERA_WEB_DRIVER_LOCATION_SYSTEM_PROPERTY,
extractDriver(
"/drivers/win" + (is64BitOS ? "64" : "32") + "/opera/operadriver.exe",
"opera.exe",
tempFiles));
}
final boolean edgeWebDriverSet =
StringUtils.isNotBlank(
SYSTEM_PROPERTY_UTILS.getProperty(
Constants.EDGE_WEB_DRIVER_LOCATION_SYSTEM_PROPERTY));
if (useSuppliedWebDrivers && !edgeWebDriverSet) {
System.setProperty(
Constants.EDGE_WEB_DRIVER_LOCATION_SYSTEM_PROPERTY,
extractDriver(
"/drivers/win32/edge/MicrosoftWebDriver.exe",
"MicrosoftWebDriver.exe",
tempFiles));
}
final boolean ieWebDriverSet =
StringUtils.isNotBlank(
SYSTEM_PROPERTY_UTILS.getProperty(
Constants.IE_WEB_DRIVER_LOCATION_SYSTEM_PROPERTY));
if (useSuppliedWebDrivers && !ieWebDriverSet) {
System.setProperty(
Constants.IE_WEB_DRIVER_LOCATION_SYSTEM_PROPERTY,
extractDriver(
"/drivers/win" + (is64BitOS ? "64" : "32") + "/ie/IEDriverServer.exe",
"ie.exe",
tempFiles));
}
final boolean phantomWebDriverSet =
StringUtils.isNotBlank(
SYSTEM_PROPERTY_UTILS.getProperty(
Constants.PHANTOM_JS_BINARY_PATH_SYSTEM_PROPERTY));
if (useSuppliedWebDrivers && !phantomWebDriverSet) {
System.setProperty(
Constants.PHANTOM_JS_BINARY_PATH_SYSTEM_PROPERTY,
extractZipDriver(
"/drivers/win32/phantomjs/phantomjs.exe.tar.gz",
"phantomjs.exe",
tempFiles));
}
} catch (final Exception ex) {
throw new DriverException(ex);
}
}
private void configureMac(
@NotNull final List<File> tempFiles,
final boolean is64BitOS,
final boolean useSuppliedWebDrivers) {
try {
final boolean marionetteWebDriverSet =
StringUtils.isNotBlank(
SYSTEM_PROPERTY_UTILS.getProperty(
GeckoDriverService.GECKO_DRIVER_EXE_PROPERTY));
if (useSuppliedWebDrivers && !marionetteWebDriverSet) {
System.setProperty(
GeckoDriverService.GECKO_DRIVER_EXE_PROPERTY,
extractZipDriver(
"/drivers/mac64/marionette/geckodriver.tar.gz",
"geckodriver",
tempFiles));
}
final boolean chromeWebDriverSet =
StringUtils.isNotBlank(
SYSTEM_PROPERTY_UTILS.getProperty(
Constants.CHROME_WEB_DRIVER_LOCATION_SYSTEM_PROPERTY));
if (useSuppliedWebDrivers && !chromeWebDriverSet) {
System.setProperty(
Constants.CHROME_WEB_DRIVER_LOCATION_SYSTEM_PROPERTY,
extractDriver(
"/drivers/mac64/chrome/chromedriver",
"chrome",
tempFiles));
}
final boolean operaWebDriverSet =
StringUtils.isNotBlank(
SYSTEM_PROPERTY_UTILS.getProperty(
Constants.OPERA_WEB_DRIVER_LOCATION_SYSTEM_PROPERTY));
if (useSuppliedWebDrivers && !operaWebDriverSet) {
System.setProperty(
Constants.OPERA_WEB_DRIVER_LOCATION_SYSTEM_PROPERTY,
extractDriver(
"/drivers/mac64/opera/operadriver",
"opera",
tempFiles));
}
final boolean phantomWebDriverSet =
StringUtils.isNotBlank(
SYSTEM_PROPERTY_UTILS.getProperty(
Constants.PHANTOM_JS_BINARY_PATH_SYSTEM_PROPERTY));
if (useSuppliedWebDrivers && !phantomWebDriverSet) {
System.setProperty(
Constants.PHANTOM_JS_BINARY_PATH_SYSTEM_PROPERTY,
extractZipDriver(
"/drivers/mac64/phantomjs/phantomjs.tar.gz",
"phantomjs",
tempFiles));
}
} catch (final Exception ex) {
throw new DriverException(ex);
}
}