What do you think should be the first script in our journey to learn Selenium Automation testing? Yeah! For any web-based testing, first & foremost you need to launch a browser and open the application URL. How? Let’s learn how to launch Chrome browser using Selenium Webdriver!
Selenium Automation Testing | Pre-requisites
- Install Java Development Kit
- Download Eclipse IDE
- Download Selenium Webdriver – Java client
- Configure Eclipse with Selenium Webdriver
- Download Chrome browser
Download Chrome Driver
To run Selenium Webdriver scripts in different browsers, first we need to download the browser-specific driver, i.e. ChromeDriver used by Selenium Webdriver to control chrome browser. ChromeDriver is supported by the Chromium team, ChromeDriver is a standalone server which implements WebDriver’s wire protocol for Chromium.
Click here to download the latest version of ChromeDriver server for Selenium Webdriver (under Third Party Browser Drivers NOT DEVELOPED by seleniumhq heading)
Note: Choose the chromedriver based on your working environment. For Windows, use Chromedriver_win32.zip (can be used for 64-bit machine as well). Save the driver zip file & unzip it in your desired location to get chromedrive.exe file
The Java code to launch Chrome browser
Now that we have downloaded all the pre-requisites for Selenium Automation Testing, let’s see at the Java code to open or launch Chrome browser using Selenium Webdriver.
package learnautomation;
import org.openqa.selenium.chrome.ChromeDriver;
public class LaunchChrome {
public static void main(String[] args) {
System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\deepansagarwal\\Downloads\\Selenium\\chromedriver.exe”);
ChromeDriver driver=new ChromeDriver();
driver.get(“http://www.gmail.com”);
System.out.println(“Test script executed successfully.”);
}
}
Import ChromeDriver class
To use all the in-built functionality of ChromeDriver, i.e. the pre-defined functions – we need to first import the selenium.chrome.ChromeDriver class to our Java code.
import org.openqa.selenium.chrome.ChromeDriver;
The path to the driver executable must be set by the webdriver.chrome.driver system property
For Selenium to know the location of Chrome driver, we need to set it via the webdriver.chrome.driver as below,
System.setProperty(“webdriver.chrome.driver”,”pathofchromedriver\\chromedriver.exe”);
Note: To set variable in Java we use the ‘setProperty’ method of System class!
Driver declaration and instantiation
To use Chromedriver first we need to create an object of type Chromedriver and then instantiate it as below,
ChromeDriver driver=new ChromeDriver();
Now the ‘driver’ object has been created which can be used to call browser actions.
Launch chrome and open application URL
To launch Chrome, we will use the ‘get’ function using the ‘driver’ variable instantiated with ChromeDriver class, as below…
driver.get(“http://www.gmail.com”);
And there you go – once you execute the above code, Selenium Webdriver will launch the Chrome browser with URL as http://www.gmail.com!
Happy start with Selenium Automation Testing!!
Looking for a Job Change in QA/Testing technology? Get Regular Job notifications @ WhatsApp!