How To Run Test Cases Using Google Chrome In Selenium Automation Script

To run google chrome in your selenium webdriver automation script, like IE Driver server, you need a ChromeDriver.exe executable file also. The theory is same as IE, this driver also startup a server, and waiting for selenium commands come from webdriver scripts.

How to use chromedriver

First you need to download the latest version in following page.
https://chromedriver.storage.googleapis.com/index.html

Download the latest version chromedriver_2.29_win32.zip,
selenium chrome driver download page

Save download file to somewhere like C:\Workspace\dev2qa.com\Lib. Unzip the zip file in the Lib folder. You can see chromedriver.exe file under C:\Workspace\dev2qa.com\Lib\chromedriver_win32. save chrome driver in your local directory

Now you can start to write java code using it.

Starting web browser with Selenium WebDriver

You need two steps in your java code to start google web browser.

1. Specify system property “webdriver.chrome.driver”’s value to your downloaded Driver executiable file path.
2. Create a new instance of ChromeDriver class.

Following is the example code that can do above steps. You can see comments for each line of code to understand it.

 //Set string variable value to Chrome Driver executable file path.
 String chromeDriverPath = "C:\\Workspace\\dev2qa.com\\Lib\\chromedriver_win32\\chromedriver.exe";
 //Assign chromeDriverPath to system property "webdriver.chrome.driver"
 System.setProperty("webdriver.chrome.driver", chromeDriverPath);
 //Initiate a new instance
 ChromeDriver cDriver = new ChromeDriver();
 /*
 * Browser webpage dev2qa.com using the new created chrome driver instance.
 * You should add http:// protocal before www.dev2qa.com otherwise there will have an exception occured
 */
 cDriver.get("http://www.dev2qa.com/");
 //Quit web browser
 cDriver.quit();

Key Points

  1. When you use any web browser (chrome, IE or Firefox) to browse web page you need to add http:// before the domain name, otherwise an exception will be thrown.
    cDriver.get("http://www.dev2qa.com/");
  2. If you meet following compile error message “ChromeDriver cannot be resolved to a type”,  you need import that class into your java code. You just need to press “ctrl+shift+o” to import all the classes needed in your java code.
    cannot be resolved to a type in java
READ :   Use Selenium Webdriver To Select Options From DropDown List

Download “TestWithChrome.zip” TestWithChrome.zip – Downloaded 601 times – 654 B

(Visited 408 times, 1 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.