Selenide - Create a Custom WebDriver

Selenide - Create a Custom WebDriver

The beauty of Selenide is that it opens a browser automatically and closes it after each test. You don’t need to do anything, this is an out of the box feature.
But what if you need to open a browser with a custom configuration, for e.g. open in an incognito window or disable popup blocking. At the moment Selenide has 3 options:

  1. setWebDriver
  2. WebDriverProvider

You can read more about these two here in the Selenide official blog:

setWebDriver or WebDriverProvider?
setWebDriver or WebDriverProvider?

3. WebdriverFactory

Using WebdriverFactory will help you to customize the webdriver. This is a more convenient way to create a ChromeDriver instance with the specified options.

To start using WebdriverFactory, first you’ll need to create your subclass from the ChromeDriverFactory class with the extends keyword.

 private static class MyFactory extends ChromeDriverFactory {
        @Override
        @CheckReturnValue
        @Nonnull
        public MutableCapabilities createCapabilities(Config config, Browser browser, @Nullable Proxy proxy, File browserDownloadsFolder) {
            ChromeOptions options = new ChromeOptions();
            
            options.setHeadless(false);
            options.addArguments("--incognito");
            
            return new MergeableCapabilities(options, createCommonCapabilities(config, browser, proxy));
        }
    }

Here you can add any ChromeOptions, just to name a few:

  • chromeOptions.addArguments("--incognito");
  • chromeOptions.addArguments("--start-maximized");
  • chromeOptions.addArguments("--disable-popup-blocking");
  • chromeOptions.addArguments("--disable-extensions");

Then you just need to use your configuration for each test with a custom webdriver:

 Configuration.browser = MyFactory.class.getName();

For e.g.

 @Test
    public void testDemo() {
        Configuration.browser = MyFactory.class.getName();
        open("https://www.audi.ca/");
    }

Selenide will start and shut down the browser automatically... but in case you need to open a browser with custom settings, this is also possible by using one of the above methods.