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.  

blank

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.

blank

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

Best Practices For Evaluating Automation Tool

With rapidly changing financial conditions, businesses face the tough challenge of optimizing resources to produce maximum results. Choosing the right automation tool for functional...

HP Borland’s Silk Test for Multi-Channel Testing

The growing web and mobile applications have posed several challenges for software QA teams. While usability and scalability are highly prioritized, compatibility across multiple...

Essentials of Typical QTP/UFT Framework

Table of Contents Essentials 1: Test Artefacts Repository Essentials 2: Error Handling and Recovery Essentials 3: Object Identification Method Essentials 4: Test Data Management Essentials 5: Result and...

Desired Capabilities in Selenium Web Driver

1. Desired Capabilities in Selenium The performance of a Web application may vary according to different browsers and operating systems. Hence to ship out a...

Â

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

Common Issues with HP Load Runner

HP Load Runner is a popular automated load and performance testing tool that emulates actual load to check the performance and behavior of a...

Getting Started with Apache JMeter

1. Introduction As a developer, you may have tested the functionality of your code hundreds of times during development. This is known as functional testing....

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...

Desired Capabilities in Selenium Web Driver

1. Desired Capabilities in Selenium The performance of a Web application may vary according to different browsers and operating systems. Hence to ship out a...

OTHER TUTORIALS

Project Contributors

For Projects hosted on external hosting services like CodePlex. Google Code or Cloud Forge, always use the hosted...
- Advertisement -spot_img