Data driven testing in selenium webdriver using excel - Blogs
X
16Feb

Data driven testing in selenium webdriver using excel

Data-driven testing (DDT) is taking a test, parameterizing it and then running that test with varying data. This allows you to run the same test case with many varying inputs, therefore increasing coverage from a single test. In addition to increasing test coverage, data driven testing allows the ability to build both positive and negative test cases into a single test. Data-driven testing allows you to test the form with a different set of input values to be sure that the application works as expected.

It is convenient to keep data for automated tests in special storages that support sequential access to a set of data, for example, Excel sheets, database tables, arrays, and so on. Often data is stored either in a text file and are separated by commas or in Excel files and are presented as a table. If you need to add more data, you simply modify the file either in any text editor or in Microsoft Excel (in case of hard-coded values, you should modify both data and code).

Data-driven test includes the following operations performed in a loop:

•           Retrieving input data from storage

•           Entering data in an application form

•           Verifying the results

•           Continuing with the next set of input data

 

Data Driven Testing can be understood by the following diagram:

test1

Pre-requisites:

  1. Java JDK 1.5 or above
  2. Apache POI library v3.8 or above
  3. Eclipse 3.2 above
  4. Selenium server-standalone-2.47.x.jar
  5. TestNG-6.9

 

Data Excel

Scenario -Open the application and login with different username and password. This data will be coming from excel sheet

Step 1: The first and the foremost step is to create the test data with which we would be executing the test scripts. Download JAR files of Apache POI and Add Jars to your project library.  Let us create an excel file and save it as “Credentials.xlsx” and place it in the created package location.

We will use the data excel file and the file looks as below

 

test25

 

Step 2: Create a POM class file under com.coe.pom name it as “Loginpage.java”. Inside a login page we write code to identify the webelements of login page using @FindBy annotation. To initialize the web elements we use initelements of page factory class. We utilize the elements by writing a method to it.

Step 3:  Create a ‘New Class‘file, by right click on the package com.coe.script and select New > Class and name it as “SuperClass.java”, and create a new class file with a name “ValidLoginLogout.java”.

Step 4:  Create some test data in excel that we will pass to script. For demo purpose I have taken username and password in excel.

Step 5:  Copy and paste the below mentioned code under com.coe.pom package class.

 

Superclass: SuperClass.java

 

package com.coe.scripts;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.AfterClass;

import org.testng.annotations.BeforeClass;

 

public class SuperClass {

            public WebDriver driver;

            @BeforeClass

            public void preCondition()

            {

//initialize the Firefox driver

                        driver = new FirefoxDriver();

// code is to open the URL in browser

                        driver.get("http://XYZ:3241/");

//code is to maximize the Firefox Driver instance

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

//code is to wait for 30 seconds

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

            }

            @AfterClass

            public void postCondition() throws InterruptedException

            {

                        Thread.sleep(5000);

//code is to close the browser

                        driver.quit();

            }

}

Step 6:  Copy and paste the below mentioned code under Generic package class and this code is to read the data from excel sheet.

 

Excelclass: Excel.java

 

package Generics;

import java.io.FileInputStream;

import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.Workbook;

import org.apache.poi.ss.usermodel.WorkbookFactory;

 

public class Excel

{

    public static String getCellValue(String xl, String Sheet, int r, int c)

      {

          try

                {

                             FileInputStream fis = new FileInputStream(xl);

                             Workbook wb = WorkbookFactory.create(fis);

                             Cell cell = wb.getSheet(Sheet).getRow(r).getCell(c);

                              return cell.getStringCellValue();

                                }

                                catch (Exception e)

                                {

                                                return "";

                                }

                }

                public static int getRowCount (String xl, String Sheet)

                {

                    try

                         {

                             FileInputStream fis = new FileInputStream(xl);

                             Workbook wb = WorkbookFactory.create(fis);

                             return wb.getSheet(Sheet).getLastRowNum();

                                }

                                catch (Exception e)

                                {

                                  return 0;

                                }

                }

}

 

Classname: LoginPage.java

 

package com.coe.pom;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.support.FindBy;

import org.openqa.selenium.support.PageFactory;

import org.testng.Assert;

import com.coe.scripts.SuperClass;

 

public class LoginPage extends SuperClass {

            //Locating Login button

 

            @FindBy (xpath="//a[contains(text(),'Login')]")

            private WebElement loginBtn;

            //Locating Email id Text box

            @FindBy (id="MemberEmail")

            private WebElement emailIdField;

           

            //Locating password Text box

            @FindBy (id ="Password")

            private WebElement psswd;

           

            //Locating Login button

            @FindBy (id="btnSubmit")

            private WebElement loginBtn;

 

            //Initializing the Objects

            public LoginPage(WebDriver driver)

            {

                        PageFactory.initElements(driver, this);

            }

//Clicking on Login button

            public void clickLoginBtn()

            {

                        loginBtn.click();

            }

 

            //Specifiying email and psswd

            public void Login (String UN,String PWD)

            {

                        emailIdField.sendKeys(UN);

                        psswd.sendKeys(PWD);

            }

            //Clicking on Login button

            public void clickLoginBtn()

            {

                        loginBtn.click();

            }

}

Step 7:  Copy and paste the below mentioned code under com.coe.scripts packge class.

Note- In below program we have 2 rows so only test case will execute 2 time with different data

 

Classname: ValidLoginLogout.java

 

package com.coe.scripts;

 

import org.testng.annotations.Test;

import Generics.Excel;

import com.coe.pom.HomePage;

import com.coe.pom.LoginPage;

 

public class ValidLoginLogout extends SuperClass{

           

@Test

public void verifyLoginWithValidCred () throws InterruptedException

{

// Clicking on Login button

            LoginPage clickLoginBtn = new LoginPage(driver);

            clickLoginBtn.clickLoginBtn();

 

// Reading the data from excel file by the specified path

           

            String xl = "./excelFiles/Credentials.xlsx";

            String Sheet = "Sheet3";

            int rowCount = Excel.getRowCount(xl, Sheet);

            for (int i=1;i<=rowCount;i++)

            {

                        String UserName=Excel.getCellValue(xl, Sheet, i, 0);

                        String Pwd=Excel.getCellValue(xl, Sheet, i, 1);

                       

                        //Passing Username and password as parameters

                        LoginPage login = new LoginPage(driver);

                        login.Login(UserName,Pwd);

 

//Submitting the data by clicking on login button                   

                        LoginPage clckLoginBtn = new LoginPage(driver);

                        clckLoginBtn.clickLoginBtn();

 

//Clicking on setting icon in home page                                 

                        HomePage clckSettingsIcon = new HomePage(driver);

                        clckSettingsIcon.clickSettingIcon();

 

                        //Clicking on Logout link in home page                      

                        HomePage clckLogoutLnk = new HomePage(driver);

                        clckLogoutLnk.clickLogoutLink();

                        Thread.sleep(1000);

 

// Again clicking on Login button to continue the loop

                        clickLoginBtn.clickLoginBtn();

                       

                        }

            }                     

 

Result:

test26

 

 

 

 

 

 

 

 

Related

What is Defect? Defect Life Cycle in Software Testing.

Defect is an unexpected behavior of the software application flow against the requirement specificat...

Read More >

What are Frames? How to handle frames in Selenium WebDriver with C#?

IFrame (FullForm: Inline Frame) is an HTML document that is included in another HTML document and is...

Read More >

What is Synchronization? Handling Synchronization in Selenium WebDriver using C#:

Synchronization meaning: when two or more components involved to perform any action, we expect these...

Read More >

Sending Test reports by Email using Office 365, Gmail

Wouldn’t it be great if Test Report are sent automatically across team as soon the Test Execut...

Read More >

Extent Reports in Selenium CSharp (C#)

Reports play a fundamental role when it comes to TESTING. Tester can now  know the real-time r...

Read More >

How to Set Up Selenium WebDriver in Visual Studio Enterprise 2015?

Pre-requisite : Visual Studio Enterprise 2015 application should be installed into the system.Create...

Read More >

Assertions in Coded UI

Let us discuss how to add assertions in coded UI. Assertions are checkpoints/benchmarks to UI c...

Read More >

Analysis of Load Test Results

Analysis of Performance DataAfter you capture and consolidate your results, analyze the captured dat...

Read More >

Parallel Execution using Selenium Webdriver and TestNG

While testing a web application it is very important to test the application on different browsers. ...

Read More >

Share

Try DevOpSmartBoard Ultimate complete Azure DevOps End-to end reporting tool

Sign Up

  • Recent
  • Popular
  • Tag
Tags
Monthly Archive
Subscribe
Name

Text/HTML
Contact Us
  • *
  • *