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.
Download the latest version chromedriver_2.29_win32.zip,
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.
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();
- 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.
- 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.