Getting Started with Selenium WebDriver

Table of Contents

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

  1. Launch Eclipse from the Start Menu or by double-clicking “eclipse.exe” in the folder “eclipse” where it was installed.
  2. To create a new Project, go to File > New and select
  1.  

3. Now name the project. I’ll be using “FirstWebDriverProject‘.

blank

4. Then click “Finish”. The project will be created and displayed in the Package Explorer.

blank

5. Right-click the project and select New >Package

blank

6. Name the package “newPackage” and click Finish.

blank

3.2) Creating a Class

  1. Right-click on the package and select New > Class.

blank

2. Give the name ‘myClass’ and click Finish.

blank

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!

Tushar Sharma
Tushar Sharmahttps://www.automationdojos.com
Hi! This is Tushar, the author of 'Automation Dojos'. A passionate IT professional with a big appetite for learning, I enjoy technical content creation and curation. Hope you are having a good time! Don't forget to subscribe and stay in touch. Wishing you happy learning!

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

SELENIUM TUTORIALS

Recent Posts

RELATED POSTS

Automation Tools Comparison (SilkTest vs QTP vs Selenium)

While manual testing and automated testing go hand in hand, one of the important benefits of automated testing is the assurance that the software...

A Brief Review Of WATIR

WATIR stands for 'Web Testing Application in Ruby'. It is an OLE-based testing application that automates your browser using Ruby libraries. Here are some...

Finding Web Elements with Selenium

I'm going to explain in this tutorial about the usage of the findElement and findElements method of Selenium Webdriver on the Chrome web browser....

Introducing Testing-as-a-Service (TaaS)

The 'Cloud Computing' technology has revolutionized the IT industry and it has something good to offer to the testing industry too. After Iaas, PaaS,...

Â

CHECKOUT 'HOW-TOs'

How To Install Oh-My-Posh On Windows PowerShell

Oh-My-Posh is a powerful custom prompt engine for any shell that has the ability to adjust the prompt string...

MORE ON CODEX

MORE IN THIS CATEGORY

How To Do API Testing with JMeter

Introduction Application Programming Interface is a very popular term among developers. It is simply a request provider that responds to your request. In other words,...

Introducing Testing-as-a-Service (TaaS)

The 'Cloud Computing' technology has revolutionized the IT industry and it has something good to offer to the testing industry too. After Iaas, PaaS,...

How To Do Database Testing with JMeter

Introduction In this 'How-To', You will be guided to perform a database load test using JMeter. We will be installing Apache JMeter to perform the...

Traditional Framework Definitions (QTP)

Over the years Functional Test Automation has come a long way from WinRunner to QTP and until ALM. The tools have evolved to a...

OTHER TUTORIALS

How To Install WordPress Locally using XAMPP

1. Introduction Installing WordPress on your computer helps you try out WordPress, test themes and plugins, and learn WordPress development. ...
- Advertisement -spot_img