Common options object for all browser option classes in Selenium

1.5k views Asked by At

I was earlier using DesiredCapabilities class to create a generic method for setting all capabilities of the browser, using an external file(key=value format). Here's my code

public DesiredCapabilities setWebDriverCapabilities(String browser) throws IOException {

    switch (browser) {
        case "ie":
            capabilities = new DesiredCapabilities().internetExplorer();
            break;
        case "firefox":
            capabilities = new DesiredCapabilities().firefox();
            break;
        case "chrome":
            capabilities = new DesiredCapabilities().chrome();
            break;
        case "edge":
            capabilities = new DesiredCapabilities().edge();
            break;
        case "safari":
            capabilities = new DesiredCapabilities().safari();
            break;
        default:
            capabilities = null;
    }

    Properties prop = new Properties();
    String FS = File.separator;
    prop.load(new FileInputStream("src" + FS + "test" + FS + "resources" + FS + browser + ".capabilities"));
    Set < Object > keys = prop.keySet();
    for (Object k: keys) {
        String key = (String) k;
        capabilities.setCapability(key, prop.getProperty(key));
    }
    return capabilities;
}

I found out that its recommended to use Options classes as some of the capabilities classes are going to be deprecated. So I am trying to replace this method with a different method that would work for all Options classes

  • FirefoxOptions
  • ChromeOptions
  • InternetExplorerOptions
  • SafariOptions
  • EdgeOptions

I cannot find a common object type for all the classes mentioned above so that I can create a similar method in which I was using DesiredCapabilities.

I want to return a common object type from this new method so that I can use it for all driver initialization e.g.

driver = new ChromeDriver(setOptions());
driver = new FirefoxDriver(setOptions());
1

There are 1 answers

0
undetected Selenium On BEST ANSWER

You need to use the method merge() from MutableCapabilities Class to merge the DesiredCapabilities type of object into ChromeOptions type object and initiate the WebDriver and WebClient instance by passing the ChromeOptions object as follows :

DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
ChromeOptions options = new ChromeOptions();
options.merge(cap);
driver = new ChromeDriver(options);

You can find a detailed discussion in How to address “The constructor ChromeDriver(Capabilities) is deprecated” and WebDriverException: Timed out error with ChromeDriver and Chrome