How to do Regression testing with Selenium WebDriver?

programming

While testing a web application, it is always essential to select the correct tool for testing. This helps us to reduce the rework and also speed up the test execution. The chosen tool, along with the automation framework, should always be helpful in carrying out the regression tests effectively.

Regression testing is usually carried out on previously tested modules due to changes or modifications in an existing module, enhancements in functionalities, changes in configuration or any other minor or significant upgrades related to the functionalities. It might also be performed whenever any fix has been given for the bugs raised.

When to carry out retesting and regression testing?

Retesting is carried out to ensure that the developer’s fix for an existing defect has been working as expected.

Regression testing is performed on the previously tested modules to ensure that new defects aren’t raised as a result of any modification due to code enhancement, defect fix for existing defects or any upgrades in the configuration.

Is it really required to do regression?

Let us see an example to justify the above point. Let us consider a scenario where an apparel brand has launched a website for online shopping and has implemented credit card as the mode of payment. This has been successfully developed and deployed for testing. The testers perform the functional and non-functional testing on this build and have raised defects if there are any discrepancies in the behavior. The defects have been fixed and the build is redeployed and testing has been successfully carried out and this functionality has gone live. After a few weeks, another new functionality has been designed, which allows the users to pay through debit cards. This new module has been developed and deployed for testing. The new module has been tested successfully. Now, what happens if regression is not performed on the previous module and the code is deployed in production? There are chances where the developer might have deployed some other version of code that doesn’t have the defect fixes or the developer might have fixed one or many defects which has broken some other functionality thus, causing the existing working functionality to break.

This would cause a huge loss for the company. Hence it is always recommended to perform regression testing to ensure that the addition of any new functionality or any modification done for the defects doesn’t break the previous working module.

Thus, regression occupies a crucial role in testing and it is performed throughout the testing lifecycle.

What are the benefits of regression testing?

1.Regression testing ensures that there are no errors produced due to modification in code

2.It ensures the correctness of the functionality

3.It ensures that the fixed defects don’t occur again because of any code change

4.It helps to identify bugs in early stage of development

What is Selenium?

Selenium has been widely used for testing of web applications. As it is an open-source automation tool, it has been popularly used among the testing community. Selenium comprises of four major components that are designed for different purposes and the selection of these components depends upon the testing requirement. The four components are:

  • Selenium IDE
  • Selenium RC
  • Selenium WebDriver
  • Selenium Grid

Selenium WebDriver is a browser specific driver that provides an interface to write and execute the browser automation scripts for testing different web applications. It provides binding for all popular languages like Java, Python, Ruby etc. It supports all the major browsers like Chrome, Firefox, Opera, IE, Edge etc. It can be integrated with various frameworks, like Maven or ANT, for compiling the source code. For application testing and reporting, it can also be integrated with testing frameworks like TestNG to ease automation testing. It can be integrated with Jenkins for Continuous Integration or Continuous Delivery automated build and deployment.

Let us see how Selenium WebDriver can be used effectively to perform regression testing.

How to do regression testing using Selenium WebDriver?

A good automation framework should be designed and developed in such a way that it contains lots of reusable code or modules covering the major features or functionalities of the application. There is no such automation tool dedicated entirely to regression testing. The automation framework should be implemented in such a way that it supports regression testing effectively. Selenium is an automation tool that can be used for testing the web applications in different browsers. When designing the framework with Selenium, it must contain multiple methods that can be reused and also the addition of any new code in any part of the framework should be done easily.

How do to design a Selenium framework for performing regression tests?

I have come up with few approaches which would be really helpful in running regression tests using Selenium WebDriver.

 

 

1.Parameterisation

Consider a scenario where a company launches a website that has core functionalities that are supported only in Chrome browser. Once it is built, deployed and tested the website goes live. After some days, the company plans to extend the browser support in Firefox. Once the browser support has been extended, the new build is deployed for testing. If the existing framework hasn’t been designed for making changes to support regression testing, then it would be a time-consuming task. To handle this, we can use parameterization to add different values and execute regression tests with minimal effort as well as minimal code change in the Selenium framework. This parameterization can be done in two easy ways.

1.Using Parameter annotation in TestNG

2.Defining the parameter and reading it from the Properties file

Let us implement the code using the above two methods which would be very helpful to perform regression

How to use Parameter annotation for performing regression testing?

This annotation is used to pass the values inside the methods by defining its values and the name of the parameter in testing.xml file.

Are you wondering how this will be helpful in performing regression using Selenium Webdriver?

Lets us see the implementation below.

As discussed in the introduction part, when the company wants to extend its browser support to another browser, it would be easy to define the values in the parameter field in testing.xml file.

package com.LoginPage;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.opera.OperaDriver;

import org.testng.annotations.AfterClass;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Parameters;

import org.testng.annotations.Test;

public class LoginTest {

public WebDriver driver;

@BeforeTest

@Parameters(“browser”)

public void setUp(String browser) {

if(browser.equalsIgnoreCase(“Firefox”)) {

System.setProperty(“WebDriver.gecko.driver”, “C:\\Users\\shalini\\Downloads\\geckodriver-v0.26.0-win64\\geckodriver.exe”);

driver = new FirefoxDriver();

}

else if (browser.equalsIgnoreCase(“GoogleChrome”)) {

System.setProperty(“WebDriver.chrome.driver”, “C:\\Users\\shalini\\Downloads\\chromedriver\\chromedriver.exe”);

driver = new ChromeDriver();

}

}

@Test

public void Login() {

driver.get(“https://opensource-demo.orangehrmlive.com/”);

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

String pageTitle = driver.getTitle();

System.out.println(“The title of this page is ===> ” +pageTitle);

driver.findElement(By.id(“txtUsername”)).clear();

driver.findElement(By.id(“txtUsername”)).sendKeys(“Admin”);

driver.findElement(By.id(“txtPassword”)).clear();

driver.findElement(By.id(“txtPassword”)).sendKeys(“admin123”);

driver.findElement(By.id(“btnLogin”)).click();

}

@AfterTest

public void tearDown() {

driver.quit();

}

}

TestNG.xml file

<?xml version=”1.0″ encoding=”UTF-8″?>

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd”>

<suite name=”TestSuite”>

<test name=”ChromeBrowserTest”>

<parameter name=”browser” value=”GoogleChrome”/>

<classes>

<class name=”com.LoginPage.LoginTest”>

</class>

</classes>

</test>

<test name=”FirefoxBrowserTest”>

<parameter name=”browser” value=”Firefox” />

<classes>

<class name=”com.LoginPage.LoginTest”>

</class>

</classes>

</test>

</suite>

In the above code, when the application supports only Firefox and Chrome browsers, we have parameterized the tests with these two browsers using parameter name and value in the testNG.xml file . Now the application has been tested successfully and the code is deployed in production.

What if in the next release, the company plans to extend the browser support in Opera?

The answer is pretty simple. Just add another browser value in the parameter tag in the TestNG.xml like below.

<test name=”OperaBrowserTest”>

<parameter name=”browser” value=”Opera” />

<classes>

<class name=”com.LoginPage.LoginTest”>

</class>

</classes>

</test>

After modifying the Testng.xml file extend the function in the main code as below

if(browser.equalsIgnoreCase(“Opera”)) { System.setProperty(“WebDriver.opera.driver”,”C:\\Users\\shalini\\Downloads\\operadriver\\operadriver_win64\\operadriver.exe”);

driver = new OperaDriver();

}

By doing so, the new build can be tested with minor changes in the implementation that already exists and also you can carry out regression tests for the previous release. I have used only login functionality in the application for demo purposes. When we have multiple modules to test this approach will be very easy to carry out regression testing. Not only browser types you can also define other parameters as well in the parameter section to carry out the regression as per your requirements.

How to define the parameters in properties file for performing regression testing?

The Properties file is one of the main feature in an automation framework which helps in performing regression testing effectively.

We can define different keys and values in the properties file which can then be used in the main automation script. All the modules of the web application can be integrated together and the keys and values for various modules can be defined When any change is brought in a release, the properties file can be modified to accommodate the changes. This approach is very straightforward, easy to implement and organized.

Below is a simple code to define the properties in a properties file and read the required property in the main code.

package Pages;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.Properties;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;

public class Demo {

WebDriver driver;

@BeforeTest

public void setUp() {

System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\Shalini\\Downloads\\chrom86_driver\\chromedriver.exe”);

driver = new ChromeDriver();

}

@Test

public void login() throws IOException {

Properties prop = new Properties();

FileInputStream input = new FileInputStream(“D:\\eclipse-workspace\\SeleniumRegression\\src\\Properties\\webpage.properties”);

prop.load(input);

//read the value of login URL from the properties file

String login_url =prop.getProperty(“loginURL”);

driver.get(login_url);

//read the value of username from the properties file

String username = prop.getProperty(“username”);

//read the value of password from the properties file

String password = prop.getProperty(“password”);

driver.findElement(By.id(“txtUsername”)).sendKeys(username);

driver.findElement(By.id(“txtPassword”)).sendKeys(password);

System.out.println(driver.getTitle());

}

@AfterTest

public void tearDown() {

driver.quit();

}

}

Webpage.properties

########This property contains the URL of the web application##############

loginURL = https://opensource-demo.orangehrmlive.com/

username = Admin

password = admin123

TestNG.xml

<?xml version=“1.0” encoding=“UTF-8”?>

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd”>

<suite name=“TestSuite”>

<test name=“LoginTest”>

<classes>

<class name=“Pages.Demo”>

</class>

</classes>

</test>

</suite>

2.Running regression tests using groups

Using groups to run the regression tests is one of the most popularly used approaches. TestNG provides a feature called “groups” which allows the testers to bundle the tests in certain groups with a unique name and thereafter executing only a specific group or groups together. By this feature, one can create a group for the regression tests and execute them when required.

Below is a simple code containing various tests for testing a web application that includes tests that are grouped under the “RegressionTest” group to run the tests for regression testing specifically.

package Pages;

import static org.testng.Assert.assertEquals;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;

public class LoginPage {

WebDriver driver;

@BeforeTest

public void setUp() {

System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\Shalini\\Downloads\\chrom86_driver\\chromedriver.exe”);

driver = new ChromeDriver();

}

@Test(groups =”RegressionTest”)

public void login() {

String login_url = “https://opensource-demo.orangehrmlive.com/”;

driver.get(login_url);

driver.manage().window().maximize();

driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);

driver.findElement(By.id(“txtUsername”)).sendKeys(“Admin”);

driver.findElement(By.id(“txtPassword”)).sendKeys(“admin123”);

System.out.println(driver.getTitle());

}

@Test

public void dashboard() {

driver.findElement(By.id(“menu_dashboard_index”)).click();

String textPresent = driver.findElement(By.xpath(“//*[@id=\”content\”]/div/div[1]/h1″)).getText();

String textToBePresent = “DashBoard”;

assertEquals(textPresent, textToBePresent);

}

@Test

public void test3() {

System.out.println(“This is test 3”);

}

@Test(groups = “RegressionTest”)

public void test4() {

System.out.println(“This is test 4”);

}

@Test

public void test5() {

System.out.println(“This is test 5”);

}

@Test(groups = “RegressionTest”)

public void test6() {

System.out.println(“This is test 6”);

}

@Test

public void test7() {

System.out.println(“This is test 7”);

}

@AfterTest

public void tearDown() {

driver.quit();

}

}

<?xml version=“1.0” encoding=“UTF-8”?>

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd”>

<suite name=“TestSuite”>

<test name=“LoginTest”>

<groups>

<run>

<include name=“RegressionTest”/>

</run>

</groups>

<classes>

<class name=“Pages.LoginPage”>

</class>

</classes>

</test>

</suite>

By implementing this approach in our framework we can also group the tests in different ways like group for running smoke tests, group for running sanity tests etc. In the TestNG.xml file we have to mention the name of the group (RegressionTest in our case) to execute the tests specific to that group.

3.Using tags for carrying out regression tests

Cucumber framework provides a special function to group tests based on certain criteria and executes them whenever need. This unique function called “tags” are defined in the TestRunner file of the Cucumber framework. All the tests that are defined with a specific tag can be executed at a time that becomes the easiest and most straightforward approach to perform regression testing for the test cases in the web application using Selenium Webdriver.

Feature file

Feature: To test the login functionality of the web application

@RegressionTest

Scenario: Verify if the user can login to his account successfully

Given The user navigates to the webpage

When The user enter “Admin” and “admin123”

Then The user should be successfully logged in

Scenario: Verify if the user is able to add products in the cart

Given The user is directed to the homepage

When The user selects and adds product

Then The selected product has to be found in the cart

@RegressionTest

Scenario: Verify whether the user is able to update his details in My account

Given The user clicks My account button

When The user updates his address details

Then The new details has to be saved and updated in user information page

TestRunner File

package test;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;

import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)

@CucumberOptions(features = “D:\\eclipse-workspace\\CucumberDemo\\src\\main\\resources\\Features\\Login.Feature”,

glue = “Steps”,

plugin = {“pretty”, “html:target/cucumber”},

monochrome = true,

tags = {“RegressionTest”}

)

public class TestRunner {

}

As per the above code, the test cases that are grouped under “RegressionTest” would be executed. Thus, using tags to perform regression testing is one of the efficient approaches that can be implemented in a Selenium automation framework.

Wrapping up…!

An automation framework should be always designed in such a way any change could be easily made in the framework. Keeping regression testing in mind while designing a framework will sweep away most of the rework involved. So far, I have shown you few approaches to carry out regression testing while testing the web application with Selenium WebDriver. Let us know the other effective approaches that you have handled while performing regression testing using Selenium Webdriver in the comment section below. Please do share this article with your colleagues and friends and share your knowledge. Happy Testing …!