下面列出了org.openqa.selenium.firefox.FirefoxOptions#setHeadless ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
/**
* 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");
}
/**
* Задает options для запуска Firefox драйвера
* options можно передавать, как системную переменную, например -Doptions=--load-extension=my-custom-extension
*
* @return FirefoxOptions
*/
private FirefoxOptions getFirefoxDriverOptions(DesiredCapabilities capabilities) {
log.info("---------------Firefox Driver---------------------");
FirefoxOptions firefoxOptions = !options[0].equals("") ? new FirefoxOptions().addArguments(options) : new FirefoxOptions();
capabilities.setVersion(loadSystemPropertyOrDefault(CapabilityType.BROWSER_VERSION, VERSION_LATEST));
firefoxOptions.setHeadless(getHeadless());
firefoxOptions.merge(capabilities);
return firefoxOptions;
}
private void setupFirefox() {
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("dom.webnotifications.enabled", false);
firefoxProfile.setAcceptUntrustedCertificates(true);
firefoxProfile.setPreference("app.update.enabled", false);
firefoxProfile.setPreference("devtools.devedition.promo.url", "");
firefoxProfile.setPreference("xpinstall.signatures.required", false);
firefoxProfile.setPreference("browser.startup.homepage;about:home", "about:blank");
firefoxProfile.setPreference("browser.startup.homepage_override.mstone", "ignore");
firefoxProfile.setPreference("browser.usedOnWindows10", false);
firefoxProfile.setPreference("browser.usedOnWindows10.introURL", "about:blank");
firefoxProfile.setPreference("startup.homepage_welcome_url", "about:blank");
firefoxProfile.setPreference("startup.homepage_welcome_url.additional", "about:blank");
firefoxProfile.setPreference("browser.helperApps.alwaysAsk.force", false);
firefoxProfile.setPreference("browser.download.folderList", 2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
firefoxProfile.setPreference("browser.download.manager.useWindow", false);
firefoxProfile.setPreference("browser.download.manager.alertOnEXEOpen", false);
firefoxProfile.setPreference("browser.download.dir", DOWNLOAD_DIRECTORY);
firefoxProfile.setPreference("browser.download.manager.focusWhenStarting", false);
firefoxProfile.setPreference("browser.download.useDownloadDir", true);
firefoxProfile.setPreference("browser.download.manager.closeWhenDone", true);
firefoxProfile.setPreference("browser.download.manager.showAlertOnComplete", false);
firefoxProfile.setPreference("browser.download.panel.shown", false);
firefoxProfile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
firefoxProfile.setPreference("pdfjs.disabled", true);
firefoxProfile.setPreference("plugin.scan.Acrobat", "99.0");
firefoxProfile.setPreference("plugin.scan.plid.all", false);
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/x-msdownload," +
"application/AppleLink," +
"application/x-newton-compatible-pkg," +
"image/png," +
"application/ris," +
"text/csv," +
"text/xml," +
"text/html," +
"text/plain," +
"application/xml," +
"application/zip," +
"application/x-zip," +
"application/x-zip-compressed," +
"application/download," +
"application/octet-stream," +
"application/excel," +
"application/vnd.ms-excel," +
"application/x-excel," +
"application/x-msexcel," +
"application/vnd.openxmlformats-officedocument.spreadsheetml.template," +
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet," +
"application/msword," +
"application/csv," +
"application/pdf," +
"application/vnd.openxmlformats-officedocument.wordprocessingml.document," +
"application/vnd.openxmlformats-officedocument.wordprocessingml.template");
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setProfile(firefoxProfile);
firefoxOptions.setHeadless(BROWSER_HEADLESS);
capabilities.merge(firefoxOptions);
}
@BeforeMethod
public void setup() {
System.setProperty("webdriver.gecko.driver",
"./src/test/resources/drivers/geckodriver 2");
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setHeadless(true);
driver = new FirefoxDriver(firefoxOptions);
driver.get("http://demo-store.seleniumacademy.com/");
}