- Selenium Framework Design in Data-Driven Testing
- Carl Cocchiaro
- 181字
- 2025-02-18 04:59:04
varargs
The following example shows how to use the varargs parameter in the setDriver method, which is called optPreferences. This is the setDriver method so far, from what we have built:
@SafeVarargs
public final void setDriver(String browser,
String environment,
String platform,
Map<String, Object>... optPreferences)
throws Exception {
DesiredCapabilities caps = null;
String localHub = "http://127.0.0.1:4723/wd/hub";
String getPlatform = null;
switch (browser) {
case "firefox":
caps = DesiredCapabilities.firefox();
FirefoxProfile ffProfile = new FirefoxProfile();
ffProfile.setPreference("browser.autofocus",
true);
caps.setCapability(FirefoxDriver.PROFILE,
ffProfile);
caps.setCapability("marionette",
true);
System.setProperty("webdriver.gecko.driver",
"gecko_driver_windows_path/geckodriver.exe");
if ( optPreferences.length > 0 ) {
processFFProfile(ffProfile, optPreferences);
}
webDriver.set(new FirefoxDriver(caps));
break;
case "chrome":
caps = DesiredCapabilities.chrome();
ChromeOptions chOptions = new ChromeOptions();
Map<String, Object> chromePrefs =
new HashMap<String, Object>();
chromePrefs.put("credentials_enable_service",
false);
chOptions.setExperimentalOption("prefs",
chromePrefs);
chOptions.addArguments("--disable-plugins",
"--disable-extensions",
"--disable-popup-blocking");
caps.setCapability(ChromeOptions.CAPABILITY,
chOptions);
caps.setCapability("applicationCacheEnabled",
false);
System.setProperty("webdriver.chrome.driver",
"chrome_driver_windows_path/chromedriver.exe");
if ( optPreferences.length > 0 ) {
processCHOptions(chOptions, optPreferences);
}
webDriver.set(new ChromeDriver(caps));
break;
case "internet explorer":
caps = DesiredCapabilities.internetExplorer();
InternetExplorerOptions ieOpts =
new InternetExplorerOptions();
ieOpts.requireWindowFocus();
ieOpts.merge(caps);
caps.setCapability("requireWindowFocus",
true);
System.setProperty("webdriver.ie.driver",
"ie_driver_windows_path/IEDriverServer.exe");
if ( optPreferences.length > 0 ) {
processDesiredCaps(caps, optPreferences);
}
webDriver.set(new InternetExplorerDriver(caps));
break;
}
// etc...
}
The Oracle Java doc for varargs is located at https://docs.oracle.com/javase/8/docs/technotes/guides/language/varargs.html.