下面列出了org.openqa.selenium.firefox.FirefoxOptions#setBinary ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected Capabilities newCaps(Capabilities capsToMerge) {
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setAcceptInsecureCerts(true);
// **** 以上capabilities可被传入的caps覆盖 ****
firefoxOptions.merge(capsToMerge);
// **** 以下capabilities具有更高优先级,将覆盖传入的caps ****
if (!StringUtils.isEmpty(browser.getPath())) {
firefoxOptions.setBinary(browser.getPath());
}
return firefoxOptions;
}
private static FirefoxDriver createFirefoxDriver() {
FirefoxOptions options = new FirefoxOptions();
if (BrowserConfig.getFirefoxBinPath() != null) {
options.setBinary(BrowserConfig.getFirefoxBinPath());
}
if (BrowserConfig.getFirefoxDriverPath() != null) {
System.setProperty(FIREFOX_DRIVER_PATH_KEY, BrowserConfig.getChromeDriverPath().toString());
}
if (BrowserConfig.isHeadless()) {
options.setHeadless(true);
}
if (System.getProperty(FIREFOX_DRIVER_PATH_KEY) == null) {
setupDriverManagerConfig();
downloadDriverMessage("firefox");
WebDriverManager.firefoxdriver().setup();
}
return new FirefoxDriver(options);
}
@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);
}
/**
* Sets the webdriver to FireFox. We set our optimal parameters here
* to ensure our proxy is set correctly.
*/
private void setFirefoxDriver() {
FirefoxOptions options = new FirefoxOptions();
FirefoxProfile profile = new FirefoxProfile();
FirefoxBinary binary = new FirefoxBinary(this.driverLocation);
LoggingPreferences logPrefs = new LoggingPreferences();
System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true");
// hide firefox logs from console
System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"/tmp/rust_");
profile.setPreference("media.volume_scale", "0.0");
profile.setPreference("general.useragent.override", userAgent.randomUA());
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.http", this.proxies.getCurrentProxyModel().getIp());
profile.setPreference("network.proxy.http_port", this.proxies.getCurrentProxyModel().getPort());
profile.setPreference("network.proxy.ssl", this.proxies.getCurrentProxyModel().getIp());
profile.setPreference("network.proxy.ssl_port", this.proxies.getCurrentProxyModel().getPort());
logPrefs.enable(LogType.BROWSER, Level.ALL);
logPrefs.enable(LogType.PERFORMANCE, Level.INFO);
options.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
options.setProfile(profile);
options.setHeadless(true);
options.setBinary(binary);
// options.setProxy(this.proxies.getCurrentProxy());
options.setCapability("proxy", this.proxies.getCurrentProxy());
this.webDriver = new FirefoxDriver(options);
Log.WINFO(this.workerName, this.workerColor, "Firefox Driver Set");
}
public SeleniumConfig(long timeout) {
FirefoxBinary firefoxBinary = new FirefoxBinary();
firefoxBinary.addCommandLineOptions("--headless");
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setBinary(firefoxBinary);
driver = new FirefoxDriver(firefoxOptions);
driver.manage().timeouts().implicitlyWait(timeout, TimeUnit.SECONDS);
}
private static FirefoxDriver createFirefoxDriver(final String binary) {
if (binary != null) {
final FirefoxOptions options = new FirefoxOptions();
options.setBinary(binary);
return new FirefoxDriver(options);
}
return new FirefoxDriver();
}
/**
* Generates a firefox webdriver.
*
* @return
* A firefox webdriver
* @throws TechnicalException
* if an error occured when Webdriver setExecutable to true.
*/
private WebDriver generateFirefoxDriver() throws TechnicalException {
final String pathWebdriver = DriverFactory.getPath(Driver.FIREFOX);
if (!new File(pathWebdriver).setExecutable(true)) {
throw new TechnicalException(Messages.getMessage(TechnicalException.TECHNICAL_ERROR_MESSAGE_WEBDRIVER_SET_EXECUTABLE));
}
log.info("Generating Firefox driver ({}) ...", pathWebdriver);
System.setProperty(Driver.FIREFOX.getDriverName(), pathWebdriver);
final FirefoxOptions firefoxOptions = new FirefoxOptions();
final FirefoxBinary firefoxBinary = new FirefoxBinary();
firefoxOptions.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
firefoxOptions.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
setLoggingLevel(firefoxOptions);
// Proxy configuration
if (Context.getProxy().getProxyType() != ProxyType.UNSPECIFIED && Context.getProxy().getProxyType() != ProxyType.AUTODETECT) {
firefoxOptions.setCapability(CapabilityType.PROXY, Context.getProxy());
}
if (Context.isHeadless()) {
firefoxBinary.addCommandLineOptions("--headless");
firefoxOptions.setBinary(firefoxBinary);
}
firefoxOptions.setLogLevel(FirefoxDriverLogLevel.FATAL);
return new FirefoxDriver(firefoxOptions);
}
@Override
protected WebDriver initialValue()
{
System.setProperty("webdriver.gecko.driver", ApplicationConfigProvider.getInstance().getDriverLocation());
FirefoxBinary firefoxBinary = new FirefoxBinary();
firefoxBinary.addCommandLineOptions("--headless");
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setBinary(firefoxBinary);
return new FirefoxDriver(firefoxOptions); // can be replaced with other browser drivers
}
private WebDriver createDriver(String browser) {
String[] parts = browser.split(" ", 2);
browser = parts[0];
switch (browser) {
case "*chrome":
case "*firefox":
case "*firefoxproxy":
case "*firefoxchrome":
case "*pifirefox":
FirefoxOptions options = new FirefoxOptions().setLegacy(false);
if (parts.length > 1) {
options.setBinary(parts[1]);
}
return new FirefoxDriver(options);
case "*iehta":
case "*iexplore":
case "*iexploreproxy":
case "*piiexplore":
return new InternetExplorerDriver();
case "*googlechrome":
return new ChromeDriver();
case "*MicrosoftEdge":
return new EdgeDriver();
case "*opera":
case "*operablink":
return new OperaDriver();
case "*safari":
case "*safariproxy":
return new SafariDriver();
default:
throw new RuntimeException("Unrecognized browser: " + browser);
}
}
@Override
public MutableCapabilities getOptions(Parameter parameter,
Optional<Object> testInstance)
throws IOException, IllegalAccessException {
FirefoxOptions firefoxOptions = new FirefoxOptions();
if (parameter != null) {
// @Arguments
Arguments arguments = parameter.getAnnotation(Arguments.class);
if (arguments != null) {
stream(arguments.value()).forEach(firefoxOptions::addArguments);
}
// @Extensions
Extensions extensions = parameter.getAnnotation(Extensions.class);
if (extensions != null) {
for (String extension : extensions.value()) {
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.addExtension(getExtension(extension));
firefoxOptions.setProfile(firefoxProfile);
}
}
// @Binary
Binary binary = parameter.getAnnotation(Binary.class);
if (binary != null) {
firefoxOptions.setBinary(binary.value());
}
// @Preferences
managePreferences(parameter, firefoxOptions);
// @Options
FirefoxOptions optionsFromAnnotatedField = annotationsReader
.getFromAnnotatedField(testInstance, Options.class,
FirefoxOptions.class);
if (optionsFromAnnotatedField != null) {
firefoxOptions = optionsFromAnnotatedField
.merge(firefoxOptions);
}
}
return firefoxOptions;
}