Table of Contents
- 1. Selenium and Selenium Web Driver
- 2. Setting-Up the Environment
- 3. Test Script with Selenium Web Driver
- 4. Code a Simple Selenium Script
- 5. Running the test
Testing is a critical step in the Software Development Life Cycle. Software testing helps to ensure the quality and effectiveness of the final product.
Automated Functional testing automates the process of testing and validating each functionality in the software through the execution of pre-defined Automation scripts. This is mainly done for test cases that need to be repeated and are tedious to perform manually.
1. Selenium and Selenium Web Driver?
Selenium is a collection of specialized tools for automating the testing process of web-based applications. This free and open-source tool can be used across different browsers and platforms.
Selenium Web driver is a browser automated framework and accepts written scripts and controls browsers directly. Furthermore, it supports test scripts written in several different languages such as Java, JavaScript, PHP, Python, and C#.
2. Setting-Up the Environment
The following prerequisites must be satisfied to create and run automated test scripts using Selenium.
-
- Installing Java Development Kit (JDK)
If JDK is not already available on your computer, downloaded it from here.
Make sure you download the exact JDK specified for your Operating System. Once you install it, configure it as shown in this tutorial.
-
- Installing Eclipse IDE
Download the latest version of Eclipse IDE for Java Developers from here and install it with the installation wizard.
-
- Download Selenium Client Drive
Download Selenium Client Driver from their official site. Make sure to choose the language Java when selecting the client driver.
-
- Configure Eclipse IDE with Web Driver
Selenium is a set of .jar files, so it only needs to be configured within the Eclipse IDE. Follow these steps.
Note: Select the latest “stable” version, instead of the latest version as it could be unstable.
3. Test Script with Selenium Web Driver
3.1) Creating a project
- Launch Eclipse from the Start Menu or by double-clicking “eclipse.exe” in the folder “eclipse” where it was installed.
- To create a new Project, go to File > New and select
3. Now name the project. I’ll be using “FirstWebDriverProject‘.
4. Then click “Finish”. The project will be created and displayed in the Package Explorer.
5. Right-click the project and select New >Package
6. Name the package “newPackage” and click Finish.
3.2) Creating a Class
- Right-click on the package and select New > Class.
2. Give the name ‘myClass’ and click Finish.
4. Code a Simple Selenium Script
We will now use the new Class to write a script in Selenium Web Driver using Firefox. It will fetch the site https://www.airbnb.com/, verify the title, print out the result and close the browser.
package newPackage; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class myClass { public static void main(String[]args) throws InterruptedException{ System.setProperty("webdriver.gecko.driver", "C:\\geckodriver\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); String baseUrl = "https://www.airbnb.com/"; String expectedTitle = "Vacation Rentals, Homes, Experiences & Places - Airbnb"; String actualTitle=""; driver.get(baseUrl); actualTitle = driver.getTitle(); if (actualTitle.contentEquals(expectedTitle)){ System.out.println("Test Passed!"); } else { System.out.println("Test Failed");} driver.close(); System.exit(0); } }
Let’s analyse and understand this code in bit detail:
Importing Packages
1import org.openqa.selenium.WebDriver;Import the Web Driver interface necessary to instantiate a new browser with specific drivers.import org.openqa.selenium.firefox.FirefoxDriver; References the Firefox class driver needed to instantiate a Firefox-specific driver into the browser instantiated by the Selenium Web Driver.
Instantiating Objects and Variables
2WebDriver driver = new FirefoxDriver(); Here a driver object of the WebDriver class is instantiated, as no parameters are specified a default Firefox browser will open in safe mode. Additionally, we have saved the URL in the string variable “baseUrl” and the text to verify in “expectedText”. “actualText” will be used to save the text found on the site.
Instantiating Gecko Driver
3Selenium 3 and the latest versions of Firefox have compatibility issues. To resolve them, download and install Gecko driver from here and set its system property in the code.System.setProperty(“webdriver.gecko.driver”,”C:\\geckodriver\\geckodriver.exe”
Starting a Browser Session and Opening a URL
4driver.get(baseUrl);The driver’s (get) method helps to open up a new browser session and directs it to the URL given as the parameter.
Verify the Required Element
5actualTitle = driver.getTitle();The getTitle() method in the Web driver interface will capture the title of the current page.
Compare Values
6This is a normal Java if-else statement used to compare the expected and the actual text values and print out the result in the console.
if (actualTitle.contentEquals(expectedTitle)) { System.out.println("Test Passed!"); } else { System.out.println("Test Failed");}
Close Browser
7driver.close();The “close()” method is used to close the browser session.
Terminate Program
8System.exit(0);This is used to terminate the entire program. We should make sure to close the browser first since terminating just the program will leave the browser open.
5. Running the test
To run the test script, click Run > Run in the Eclipse menu. Alternatively, you can press Ctrl + F11.
Once everything is done Eclipse will print out the result in its console.
This is just a simple example. There is a lot more that can be done with Selenium. Use this example as the beginning of your Selenium journey!