Using Selenium WebDriver with Chrome browser
Selenium WebDriver is open source tool to automate testing the Web application across many browsers. Every browser has its own driver to execute Selenium WebDriver Scripts. It supports browsers such as Mozilla Firefox, Google Chrome, Internet Explorer, Safari etc.
Selenium (WebDriver) versions before 47 support only Mozilla Firefox browser by default. To run the selenium WebDriver in Google Chrome browser we need a ChromeDriver. ChromeDriver is a standalone server which is supported by Chromium team. Selenium WebDriver uses ChromeDriver to control the chrome browser.
ChromeDriver is divided into the three parts:
1. Browser: The Chrome browser.
2. Language binding: The language (Java,PHP,C#) that is used to create a testing script to send commands to the Chrome browser.
3. ChromeDriver: It acts as an intermediate component between Chrome browser and script to execute the script on the browser.
How Google ChromeDriver Works:
Google ChromeDriver instlls a server on your local machine. When we run the script in Eclipse IDE then IDE first connects with the ChromeDriver and then ChromeDriver connects with Chrome browser in a native language. Further ChromeDriver sends the commands mentioned in the test cases to chrome browser.
To run the selenium WebDriver on the Google Chrome we need to follow the followings steps:
1. Download the ChromeDriver according to the working environment as windows, mac, Linux.
Click here to download: “https://sites.google.com/a/chromium.org/ChromeDriver/downloads”:
2. Extract the ChromeDriver zip file and keep it somewhere location in your PC. After extraction, you will get a chromeDriver.exe:
3. Set a system property “webdriver.chrome.driver” to the path of executable file“ChromeDriver.exe“ such as (“C:/chromedriver.exe”). If you miss path or if you give the wrong path, you will face an error message:
Error: The path to the driver executable must be set by the webdriver.chrome.driver system property
4. Instantiate a ChromeDriver class:
Example of Selenium WebDriver with chromeDriver:
Code: package testing; import org.openqa.selenium.Capabilities; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.remote.RemoteWebDriver; public class Chromebrowser { WebDriver driver; public void chromeDriver() { //Set the path for chromeDriver System.setProperty("webdriver.chrome.driver", "C:/chromedriver.exe"); driver = new ChromeDriver(); //Go to the URL driver.get("https://sites.google.com/a/chromium.org/chromedriver/"); //Maximize the browser window size driver.manage().window().maximize(); //Get title of current page in upper case letter String title = driver.getTitle().toUpperCase(); System.out.println("Page Title: " + " " + title); //Use the getCapabilities method to get the browser information Capabilities capability = ((RemoteWebDriver) driver).getCapabilities(); //Get the browser name in upper case letter String browserName = capability.getBrowserName().toUpperCase(); System.out.println("Browser Name:" + " " + browserName); //Get the browser current Version String browserVersion = capability.getVersion().toString(); System.out.println("Browser Version:" + " " + browserVersion); //Close the browser driver.close(); } public static void main(String[] args) { Chromebrowser call = new Chromebrowser(); call.chromeDriver(); } }
Result: