Selenium has been in the market for over a decade and has proven to be a robust tool for automation testing. Developed by Jason Huggins, its main objective was to overcome the limitations of manual testing.
What is Selenium?
Selenium is an open-source, automated testing tool used to test web applications across multiple browsers. It's primarily built in Java and supports several browsers and programming languages.
The Selenium test suite comprises of four tools:
- Selenium Integrated Development Environment (IDE)
- Selenium Remote Control (RC)
- Selenium WebDriver
- Selenium Grid
The article covers Selenium WebDriver in detail, including its features, architecture, the working principles, followed by a demo.
Need for Selenium WebDriver
To understand why WebDriver was introduced, let's look at the shortcomings of Selenium RC.
Selenium Remote Control (RC) is a test tool that allows you to write automated web application UI tests in any programming language against any HTTP website using any mainstream JavaScript-enabled browser.
Fig: Selenium RC
Selenium RC Server receives Selenium commands from your test program, interprets them, and reports the results back to the program. The Web browser is injected with Selenium core, which interprets and executes the Selenese commands used in the test script. The web browser now interacts with the web server accordingly. This setup, however, complicates the architecture and takes additional time for execution.
Let's see how Selenium WebDriver helped overcome these drawbacks. The WebDriver does not use an additional server, instead of making direct calls to the browser using each browser's native support for automation.
Fig: Selenium WebDriver
The execution time for commands is shorter due to the simple architecture. WebDriver also provides easy-to-use APIs, unlike RC, which used redundant and confusing APIs. WebDriver also supports a headless, GUI-less HtmlUnit browser.
What is Selenium WebDriver?
Paul Hammant developed Selenium WebDriver in 2006. Selenium WebDriver was the first cross-platform testing framework that could configure and control the browsers on the OS level. It served as a programming interface to create and run test cases.
WebDriver performs actions on web elements. It supports various programming languages like Java, C#, PHP, Python, among others. It can also be integrated with frameworks like TestNG and JUnit for test management.
The architecture of Selenium WebDriver is simple and easy to understand.
Fig: Selenium WebDriver Architecture
- Selenium test script - Selenium test script is the test code written in any of the mentioned programming languages that are interpreted by the driver
- JSON Wire Protocol - JSON Wire Protocol provides a transport mechanism to transfer data between a server and a client. JSON Wire Protocol is the industry standard for various web services
- Browser drivers - Selenium uses drivers, specific to each browser to establish a secure connection with the browser
- Browsers - Selenium WebDriver supports multiple web browsers to test and run applications on.
Selenium WebDriver Demo
1. Download and Install Java 8 or higher version; Install the latest version Java development kit. Click here to install it.
2. Download and configure Eclipse or any Java IDE of your choice; Open the URL link.
Scroll down the page and click on the latest version in the "more downloads" section.
The downloaded file will be a zip file. Unzip the file in a folder of your choice. Once unzipped, open the .exe eclipse file.
The next step is to configure a workspace. Select a directory you want to store all your projects in and click on the launch icon.
Once launched, the IDE workbench will look as shown:
Download Selenium WebDriver Java Client
- Navigate to the official Selenium page
- Scroll down through the web page and locate Selenium Client & WebDriver Language Bindings
- Click on the "Download" link of Java Client Driver as shown in the image below
Once downloaded, unzip the file in a directory. It consists of the jar files required to configure Selenium WebDriver in the IDE.
4. Download the browser driver
The automation scripts must be compatible with any browser. Every browser supported by Selenium comes with its driver files, which are essential to run the scripts. Next, download the latest driver file from this link.
5. Configure Selenium WebDriver
Now, the final step is to configure the Selenium WebDriver with the Eclipse IDE. In simple terms, we create a new Java project to build our test script.
Provide a project name and select the JRE that you wish to use. It's advisable to use the default JRE. Select it and click on finish.
The next and most crucial step is to add the downloaded Java executable files [Step 3]. To do this, Right-click on the project>>Build Path>>Configure Build Path
Select libraries and then Add External JARs.
Open the folders in which you've saved your JAR files and select the two executable JAR files. Click on open to add them.
Click on the libs folder>> Select the files>>Open
Once you're done adding the library files, click on Apply and Close.
With this, you've successfully configured the Webdriver with the Eclipse IDE. You can now go ahead and build your first test script.
Right-click on Src folder>>New>>Class
Once you’ve created a class, copy the following code to launch the browser:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class FirstSeleniumTest { WebDriver driver; public void launchBrowser() { System.setProperty("webdriver.gecko.driver","C:\\Users\\chinmayee.deshpande\\Downloads\\Drivers\\geckodriver.exe"); driver = new FirefoxDriver(); driver.get("https://www.ebay.com/"); } public static void main(String[] args) throws InterruptedException { FirstSeleniumTest obj = new FirstSeleniumTest(); obj.launchBrowser(); } |
Note: The second argument to the setProperty method is the location of your browser driver. In our case, we've installed the gecko driver. Hence, we paste the path along with the name of the file.
Now that we're done with the installation and setup, we can look into some of the commonly used browser APIs.
Fig: Browser APIs
Let's now create a simple test script that does the following.
Use case 1: As a user, I want to login to eBay.in and search for "JBL Speakers." Then I want to check the day's deals.
Use case 2: From eBay.in, navigate to the Simplilearn website. And navigate back to eBay.in
Use case 3: From eBay.in, I want to print the page title and close the browser
Use Case 1
We create a new method for this use case. Let's call it, searchProduct.
The first thing to do is navigate to www.ebay.com.
driver.get("https://www.ebay.com/");
Once you navigate to the page, you need to type "JBL Speakers" in the search box. For that, you need first to locate the search box, and you can do so by using the locator APIs.
Right-click anywhere on the page>>Inspect
1. Now click on the top-left icon. This icon provides the location for the part of the UI selected.
2. Hover the mouse over the search box and click on it. You can now see the HTML code corresponding to that location.
3. Copy that code. In this case, we're locating the element using the CSS selector.
Right-click on the code>>Copy>>Copy selector
This code can be pasted in your code.
Follow the same steps for clicking on the "search" icon and the "daily deals" icon.
So the code will look something like this:
driver.findElement(By.cssSelector("#gh-ac")).sendKeys("JBL Speakers");
driver.findElement(By.cssSelector("#gh-btn")).click();
driver.findElement(By.cssSelector("#gh-p-1 > a")).click();
The following is the test script for the first use case:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class FirstSeleniumTest { WebDriver driver; public void launchBrowser() { System.setProperty("webdriver.gecko.driver","C:\\Users\\chinmayee.deshpande\\Downloads\\Drivers\\geckodriver.exe"); driver = new FirefoxDriver(); driver.get("https://www.ebay.com/"); } public void searchProduct() throws InterruptedException { Thread.sleep(2000); driver.findElement(By.cssSelector("#gh-ac")).sendKeys("JBL Speakers"); driver.findElement(By.cssSelector("#gh-btn")).click(); Thread.sleep(2000); driver.findElement(By.cssSelector("#gh-p-1 > a")).click(); } public static void main(String[] args) throws InterruptedException { FirstSeleniumTest obj = new FirstSeleniumTest(); obj.launchBrowser(); obj.searchProduct(); } |
Use Case 2
From the eBay homepage, we next navigate to the Simplilearn website. For that, we create a new method, "navigate," and use the navigation commands.
The following code will help navigate to the Simplilearn and back to ebay.com:
public void navigate() throws InterruptedException { Thread.sleep(2000); driver.navigate().to("https://www.simplilearn.com/"); Thread.sleep(2000); driver.navigate().back(); } |
Use Case 3
To print the title of the page, we use the SysOut command. In the class "searchProduct" type the following command:
System.out.println("The title of the page is :" + driver.getTitle()); |
It's always good practice to create a separate method to close the browser, so create a method called "closeBrowser."
public void closeBrowser() { driver.quit(); } |
So the final code would look something like this:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class FirstSeleniumTest { WebDriver driver; public void launchBrowser() { System.setProperty("webdriver.gecko.driver","C:\\Users\\chinmayee.deshpande\\Downloads\\Drivers\\geckodriver.exe"); driver = new FirefoxDriver(); driver.get("https://www.ebay.com/"); } public void searchProduct() throws InterruptedException { Thread.sleep(2000); driver.findElement(By.cssSelector("#gh-ac")).sendKeys("JBL Speakers"); driver.findElement(By.cssSelector("#gh-btn")).click(); Thread.sleep(2000); driver.findElement(By.cssSelector("#gh-p-1 > a")).click(); System.out.println("The title of the page is :" + driver.getTitle()); } public void navigate() throws InterruptedException { Thread.sleep(2000); driver.navigate().to("https://www.simplilearn.com/"); Thread.sleep(2000); driver.navigate().back(); } public void closeBrowser() { driver.quit(); } public static void main(String[] args) throws InterruptedException { FirstSeleniumTest obj = new FirstSeleniumTest(); obj.launchBrowser(); obj.searchProduct(); obj.navigate(); obj.closeBrowser(); } |
Now go ahead and run the code. Run>>Run As>>Java Application.
Once the test script runs successfully, you can verify if the title of the page was printed. Scroll down the console to see the message.
With that, you've created your first Selenium test case!
Want More Hands-On Training?
To learn more about Selenium and how the various suite components work, check out the Simplilearn's video on "Selenium Tutorial for Beginners" curated by industry experts. In the video, you'll learn about the tool, and have a hands-on demo on the working of Selenium IDE and WebDriver.
If you’re interested in making a career as an automation engineer, the Automation Testing Masters will definitely come in handy and will help you master the complete Selenium suite.
The certification course is ideal for:
- Test Managers
- Test Engineers
- Test Leads
- Test Analysts
- QA Engineers
- Software Developers
- Engineers who want to learn automation testing