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

Elevating Test Automation: A Deep Dive into Data-Driven Testing Techniques 


DataDrivenTesting


DataDrivenTesting plays a crucial role in Software Testing world. Because same scenario will run multiple times with multiple sets of data. we keep the test data in files like Excel Files, Text Files, CSV Files,YAML files  or any database. Let's say you have written an automation script to  fill the student information and enrolled courses ,batches. Here comes Data Driven Framework into the picture and makes the test scripts work properly for different sets of test data.So we use the Data Driven Framework when we have to execute the same script with multiple sets of test data, where test data is stored outside of our script or code. Therefore any changes made to the test data will not impact the code.

Data Files may be in various formats but most commonly used formats are:

  • CSV format (*.csv files)

  • MS Excel Workbook (*.xls or *.xlsx files)

  • XML Format (*.xml files)

  • JSON format(*.json files)

Parameterization in Selenium is a process to parameterize the test scripts in order to pass multiple data to the application at runtime. It is a strategy of execution which automatically runs test cases multiple times using different values. The concept achieved by parameterizing the test scripts is called Data 



Driven Testing.


Steps to achieve data driven testing:


Step 1: Using data-driven testing, decide which test cases you want to automate. These test cases ought to work with various sets of input data.

Step 2: Get the test data for your test cases. And decide in which format  you want to be stored(JSON,Excel,CSV,YAML).

Step 3:Select the Automation testing framework by requirement of your project whether it’s a Data Driven or Keyword Driven or Hybrid framework.

Step 4: Write test scripts to received at a from the test data sources though  parameterization or using data providers.

Step 5: To read or write Test Data from  test data source into the script , we need a connection . For this we might need to add Dependencies or Libraries.

Step 6: User test data  from source into your script as per the requirement.Whether we need to multiple iterations or not.

Step 7:  Excute the script

Step 8: Get the reports or Test Results of each iteration.



Data Driven Testing Framework using CSV file


<dependency>

    <groupId>com.opencsv</groupId>

    <artifactId>opencsv</artifactId>

    <version>3.3</version>

</dependency>


CODE:




package datadriven;

import java.io.FileReader;

import java.io.IOException;

import java.io.Reader;

import java.util.List;

import com.opencsv.CSVReader;

public class dataDrivenCsv {

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


String path = "/Users/vijayjagabathula/Documents/csvtest1.csv";

Reader reader = new FileReader(path);

    CSVReader csvReader = new CSVReader(reader);

    List<String[]> data = csvReader.readAll();

     for(String[] d : data) {


          for(String c : d) {

        

          System.out.println(c);

}

}

}

}


At first, we define the path of the CSV file and using FileReader class we capture all the contents of the CSV format. Next, we create the instance of the CSVReader class and pass an object of the FileReader as an argument inside CSVReader. Further, we create List of the String array in which all the test data are stored. We print the values of the data through iteration.



Data Driven Testing Framework using  XML file :


We need to add Apche POI dependecies to POM.XML file



<dependency>

    <groupId>org.apache.poi</groupId>

    <artifactId>poi-ooxml</artifactId>

    <version>5.2.5</version>

</dependency>


<dependency>

    <groupId>org.apache.poi</groupId>

    <artifactId>poi-ooxml-schemas</artifactId>

    <version>4.1.2</version>

</dependency>



And the code for creating Excel sheet and writing to it and reading from the excel sheet is as follows:


package datadriven;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Iterator;

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

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

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

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

public class ExcelSheetHandling {


//creating a excel sheet to write data on to it

public static void writeExcelSheet() throws IOException {

XSSFWorkbook workbook = new XSSFWorkbook();

XSSFSheet worksheet = workbook.createSheet("sheet 1");


int rowNum = 0;


for(int i = 1;i<=10;i++) {


Row row = worksheet.createRow(rowNum++);

int colNum = 0;


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

Cell cell =row.createCell(colNum++);

cell.setCellValue("Row "+ i + " Column "+ j);


}

}


//Giving the path where Excel sheet should be created

String path = System.getProperty("user.dir")+"/src/test/resources/TestData/Demoexcel.xlsx";

File Excelfile = new File(path);

FileOutputStream Fos = null;


try {

Fos = new FileOutputStream(Excelfile);  //creating excel sheet to write

workbook.write(Fos);

workbook.close();    //closing the excel sheet



} catch (FileNotFoundException e) {

e.printStackTrace();

}

finally {

Fos.close();

}

}


//Reading the excel sheet which is alteady created

public static void ReadExcelSheet() throws IOException {

String path = System.getProperty("user.dir")+"/src/test/resources/TestData/Demoexcel.xlsx";

File Excelfile = new File(path);

FileInputStream Fis = new FileInputStream(Excelfile);

XSSFWorkbook workbook = new XSSFWorkbook(Fis);

XSSFSheet sheet = workbook.getSheet("Sheet 1");


Iterator<Row> row = sheet.rowIterator();


while(row.hasNext()) {

Row currRow = row.next();

Iterator<Cell> cell = currRow.cellIterator();

while(cell.hasNext()) {

Cell currCell = cell.next();

System.out.print(currCell.getStringCellValue()+" ~ ");

}

System.out.println();

}

//Addin more rows to already created excel sheet

Row newRow = sheet.createRow(12);

Cell newCell = newRow.createCell(13);

newCell.setCellValue("Malli");

FileOutputStream Fos = new FileOutputStream(Excelfile);

workbook.write(Fos);

workbook.close();

}

//Calling functions in the main methpd

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

//writeExcelSheet();

ReadExcelSheet();


}

}



Data Driven Testing using YAML Files :


One popular format for such configuration files is YAML (YAML Ain’t Markup Language). YAML provides a human-readable and easy-to-write syntax, making it an ideal choice for storing test data and settings. In this article, we will explore how to use a YAML file in Java Selenium automation. • YAML is a very simple, text-based, human-readable language used to exchange data between people and computers.

  • YAML is a data serialization language similar to XML or JSON. However, it is much more human-readable and concise.



Step 1: Dependencies to add in POM.XML


To start using YAML files in your Java Selenium project, you will need to add the appropriate dependency to your build configuration. You can use libraries such as SnakeYAML or Jackson YAML. For this example, let’s use SnakeYAML.


<dependency>

    <groupId>org.yaml</groupId>

    <artifactId>snakeyaml</artifactId>

    <version>2.2</version>

</dependency>


Step 2: create config.yml file in testtesources folder


username: malli

password: me123



Step 3: Loading YAML File


After adding dependencies writing scripts to load YAML file using java code


package datadriven;


import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.Map;


import org.yaml.snakeyaml.Yaml;


public class YAMLDemo {


public static void main(String[] args) {



Yaml yaml = new Yaml();

try(InputStream inputstream = new FileInputStream("config.yml")){

Map<String, Object> data = yaml.load(inputstream);

String username = (String) data.get("username");

String password = (String) data.get("password");

System.out.println("username" + username);

System.out.println("password" + password);

}catch(IOException e) {

e.printStackTrace();

}


}

}



Advantages of Using YAML Files

Using YAML files in Java Selenium automation offers several benefits:

  • Readable Syntax: YAML provides a human-readable and intuitive syntax, making it easy to write and understand test data and configuration settings.

  • Separation of Concerns: Storing test data and configuration settings in a separate YAML file promotes separation of concerns. Test code can focus on test logic, while YAML files handle data and configuration.

  • Flexibility: YAML supports nested structures, arrays, and key-value pairs, providing flexibility in structuring and organizing test data.

  • Easy Maintenance: With YAML files, it is straightforward to update and modify test data and configuration settings without changing the test code.




Hope you understand the nuances Data-Driven Testing Techniques.


Happy Learning!!!!!!!




















58 views0 comments

Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page