In this post, will discuss about controlling ChromeDriver’s lifetime in Selenium Webdriver.

Controlling ChromeDriver's lifetime in Selenium Webdriver

Normally we use ChromeDriver for webdriver’s execution of test scripts.
ChromeDriver is a separate executable that WebDriver uses to control Chrome.

Once Chrome Browser is installed in the system, webdriver user’s downloads ChromeDriver binary for the platform that they use to execute the scripts.

ChromeDriver expects Chrome Browser installed in the default location for any platform.

We can also force ChromeDriver to use a custom location by setting a special capability.

Setting custom location for ChromeDriver:
ChromeOptions options = new ChromeOptions();
options.setBinary("/path/to/other/chrome/binary");
Set a Chrome preference
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_settings.popups", 0);
options.setExperimentalOption("prefs", prefs);
Controlling ChromeDriver’s lifetime

The ChromeDriver class starts the ChromeDriver server process at creation and terminates it when quit is called.

This can waste a significant amount of time for large test suites where a ChromeDriver instance is created per test. To remedy this we can use ChromeDriverService

Use ChromeDriverService. Available in most of the languages and allows us to start\stop the ChromeDriver server.

When execute below example by looping the test several times(say 10 times), time got saved by 10 to 15 seconds by using ChromeDriverService instead of ChromeDriver.

In this example, I  quit the driver at method level. Same can be experimented in different ways at Class level and Suite level as well.

public class ChromeDriverService_Example {

WebDriver driver;
ChromeDriverService chromeService;
String chromeDriverPath;

@BeforeClass
public void setup() throws IOException
{
   chromeDriverPath = "\\chromedriver.exe";
   System.setProperty("webdriver.chrome.driver", chromeDriverPath);
   chromeService = new ChromeDriverService.Builder().usingDriverExecutable(new File(chromeDriverPath)).usingAnyFreePort().build();
   chromeService.start();
}

@BeforeMethod
public void createDriver() throws MalformedURLException
{
   driver = new RemoteWebDriver(service.getUrl(),DesiredCapabilities.chrome());
   //OR
   //driver = new ChromeDriver(chromeService);
}

@Test
public void chromeTest1() throws IOException
{
   driver.get("http://www.facebook.com");
   System.out.println("Title-----------"+driver.getTitle());
}

@AfterMethod
public void quitDriver()
{
   driver.quit();
}

@AfterClass
public void createAndStopService() {
    chromeService.stop();
}
Selenium Page Object Generator Chrome Extension
Basic Usage of TestNG
0