How to launch Chrome browser in Selenium WebDriver?

Webdriver is the interface in API, which has several implementations. We mainly write test cases against those implementations only. HTMLDriver, FirefoxDriver, OperaDriver, ChromeDriver, InternetExplorerDriver, etc are the implementations to WebDriver interface. Today we will discuss ChromeDriver to launch chrome browser to run test cases in Selenium webdriver.

ChromeDriver is basically a project of Chromium.

How to implement ChromeDriver to launch Chrome Browser?

We can implement ChromeDriver in a just a single line of code, but that is not sufficient. WebDriver API does not have a native implementation for Chrome browser so we need additional Google Chrome Driver file. It is an executable file like Geckodriver that we discussed in the previous chapter. So we need to set the property in the system for Google Chrome Driver first.

Architechture of Chrome Driver and chrome browser

Image Credit: AssertSelenium Blog

 

How does Google Chrome Driver work?

Google Chrome Driver makes a server in your local system. So IDE (Eclipse or IntelliJ) first connect with Chrome driver and then driver communicates with Chrome browser in its native language, further, ChromeDriver sends commands mentioned in test cases to Chrome browser. Thus, like this ChromeDriver acts as a middle man or proxy between Test cases and Chrome browser.

 

Download Google Chrome Driver executable file

Visit Selenium download page, after scroll down you find the list of all the executable files of all the drivers.

The current version of ChromeDriver is 2.31 for Selenium 3.0.4.

Download ChromeDriver

 Chromedriver to launch chrome browser

Java code to implement ChromeDriver in Selenium to launch Chrome browser

You need to define Set Property before implementing ChromeDriver. So here is the easy to understand code snippet for you.

System.setProperty(“webdriver.chrome.driver”, “Path to your ChromeDriver .exe file”);

Example:

System.setProperty(“webdriver.chrome.driver”, “"F:\\chromedriver.exe");

TIP: Right click with Shift on ChromeDriver.exe file and select “Copy as Path”. Entire path will be copied automatically. Then paste the path in code and replace single slash with the double slash. See screenshot below:

Path to chromedriver to set up chrome browser in java

 

 

So the ChromeDriver implementation looks like this:

System.setProperty(“webdriver.chrome.driver”, "F:\\chromedriver.exe");

WebDriver driver = new ChromeDriver();

 

Code snippet to launch www.inviul.com in Chrome browser by using ChromeDriver.

package InviulTest;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FirefoxTest {

	public static void main(String[] args) {
		System.setProperty("webdriver.chrome.driver", "F:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		driver.navigate().to("http://www.inviul.com");
		driver.manage().window().maximize();
		driver.close();
		driver.quit();
	}
}

 

 

Leave a Reply