top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Selenium Data Driven Framework

What Is Data driven in selenium  

A data driven framework is the one in which the test cases are segregated from the data set. Also, it gives the provision to run the same test case against multiple sets of data. Since the test code implementation and test data are independent of each other, the data set can be modified without impacting the implementation logic. It enables testers to build both positive and negative test cases into a single test. A data driven framework is mostly used to create test cases which are dependent on data available from an external source. This external source can be any file having extensions with .txt, properties, .xlsx, .xls, .csv, and with the help of data providers. 


1.Data driven with Excel. 

Prerequisite for Data driven with Excel. 

1.Add Apache POI Common dependencies and Apache POI API Based On OPC and OOXML Schemas:


2. Below Screenshot, you can see the application whereas Username and password are the fields which we need to give the values in the Excel sheets , the values may be positive or negative. 

3. Excel sheet to add the data.

4.Implementation of code to read the file and execute the script in selenium java .

Below is the Java class with selenium -java code to implement the excel file as input data


package exceljava;

import org.apache.poi.xssf.usermodel.XSSFCell;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.support.FindBy;


import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.time.Duration;

import java.util.concurrent.TimeUnit;


public class ExcelReadWrite {

public static void main(String args[]) throws IOException {

File f = new File("src/test/resources/Book 6.xlsx");

FileInputStream i = new FileInputStream(f);


XSSFWorkbook w = new XSSFWorkbook(i);

XSSFSheet s = w .getSheet("Sheet1");


// handle total rows in XSSFSheet

int r = s.getLastRowNum() - s.getFirstRowNum();


WebDriver driver = new ChromeDriver();


driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));

driver.get("https://dsportalapp.herokuapp.com");


WebElement getstartedbtn = driver.findElement(By.xpath("//button[@class='btn']"));

WebElement signInLink = driver.findElement(By.xpath ("//a[text()='Sign in']"));

signInLink.click();

WebElement username = driver.findElement(By.id("id_username"));

WebElement pass = driver.findElement(By.id("id_password"));

for(int j = 1; j <= r; j++) {

username.sendKeys(s.getRow(j).getCell(0).getStringCellValue());

pass.sendKeys(s.getRow(j).getCell(1).getStringCellValue());


}

WebElement loginBtn = driver.findElement(By.xpath("//input[@type='submit']"));

loginBtn.click();

w.close();


// Quitting browser

driver.quit();

}

}

Code explanation:  

File f = new File ("src/test/resources/Book 6.xlsx"); 

Creates a File object representing the Excel file location. 

 

FileInputStream i = new FileInputStream(f); 

Opens an input stream to read the Excel file. 

 

XSSFWorkbook w = new XSSFWorkbook(i); 

 

XSSFSheet s = w .getSheet("Sheet1"); 

The XSSFWorkbook constructor takes the FileInputStream object i and reads the content of the Excel file into the XSSFWorkbook instance w 

 

int r = s.getLastRowNum() - s.getFirstRowNum(); 

Handling the data in excel sheets. 

 

After that Selenium java code is added to launch the browser to the specified link. 

With specific web elements such as username, password, sign link, login button user will successfully log in with excel input data. 


2. Data Driven with TestNG Data Provider 

1.Add the TestNG dependencies from the link below:  

2.Code implementation with TestNg data provider.  

In this example, we had input the data in the login page from the data available in the Data Provider .


package testng;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.support.ui.WebDriverWait;

import org.testng.Assert;

import org.testng.annotations.*;


import java.time.Duration;

import java.util.concurrent.TimeUnit;


public class DataTest {

WebDriver driver;

@BeforeTest

public void setup() throws Exception {


driver = new ChromeDriver();

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

driver.get("https://dsportalapp.herokuapp.com");

}


@Test(dataProvider = "login-data")

public void userRegistration

(String username1, String password1) {


//Identify elements for login

WebElement getsartedbtn = driver.findElement(By.xpath("//button[@class='btn']"));

getsartedbtn.click();

WebElement signInLink = driver.findElement(By.xpath ("//a[text()='Sign in']"));

signInLink.click();

WebElement username = driver.findElement(By.id("id_username"));

WebElement password = driver.findElement(By.id("id_password"));

username.clear();

password.clear();

username.sendKeys(username1);

password.sendKeys(password1);

WebElement loginBtn = driver.findElement(By.xpath("//input[@type='submit']"));

loginBtn.click();

WebElement successMessageElement = driver.findElement(By.xpath("//div[@class='alert alert-primary']"));

String successMessage = successMessageElement.getText();


Assert.assertTrue(successMessage.contains("You are logged in"), "Login was not successful. Expected message not found.");


}

@DataProvider (name = "login-data")

public Object[][] loginData(){

return new Object[][]

{ {"sneha2024", "test@2024"} };

}

@AfterTest

public void teardown() {

driver.quit();

}

}

Test Result after running the Script.

3 .Data driven with Excel reader class. 


In this example the java excel reader class is created in which some data is added in excel sheet and the file is saved in project and the path of file is provided.

As Dependencies will be same as above only, we need to add java class for excel reader to read the data from excel

Below is the Excel reader class hoe to read the excel data from file .

package utilities;


import java.io.FileInputStream;

import java.io.IOException;

import java.util.ArrayList;


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

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

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

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

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

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

import java.io.File;



public class ReadElliminationList {


public void main(String[] args) throws IOException {

String filename = "/src/test/resources/LCHFEllimanator (1).xlsx";



ArrayList<String> ingredients = readColumnFromExcel(filename, 0); // read column 1

System.out.println(ingredients);

}


public static ArrayList<String> readColumnFromExcel(String filename, int columnNum) throws IOException {

ArrayList<String> columnData = new ArrayList<>();

FileInputStream inputStream = new FileInputStream(new File(filename));

Workbook workbook = WorkbookFactory.create(inputStream);

Sheet sheet = workbook.getSheetAt(0);


int rowIndex = 0;

for (Row row : sheet) {

if (rowIndex >= 2)

{

Cell cell = row.getCell(columnNum);

if (cell != null && cell.getCellType() == CellType.STRING) {

columnData.add(cell.getStringCellValue());

}

}

rowIndex++;

}


workbook.close();

inputStream.close();


return columnData;

}


}


Excel reader class with excel path added to read the data so the code is to read the data from excel sheet.so the excel reader class Opens the Excel file and Reads data from the specified column. 

 In the main java class Calls the readColumnFromExcel method from ReadEliminationList to get the data. 

In this way we can achieve Data driven testing in automation framework. Hope it is helpful for you. Happy Testing. 


20 views0 comments

Kommentarer

Betygsatt till 0 av 5 stjärnor.
Inga omdömen ännu

Lägg till ett betyg
bottom of page