0% found this document useful (0 votes)
17 views4 pages

DataDriven (Advance Selenium)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

DataDriven (Advance Selenium)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Data Driven testing

1. What is Data driven testing?


Read the data from external resource & run the test is called Data driven testing
(parameterization)

2. Why Data Driven testing?


As per the rule of the automation data shouldn’t not hardcoded(fixed) with in a
test scripts, because data modification & maintenance is tedious job when you
want to run the test with different data, instead we should get the data from
external resource like xlsx, .properties file , db , XML, JSON, CMD Line Data

3. What is Advantages of Data driven testing


1. Maintenance of the test data is easy
2. Modification of the test data in external recourse is easy
3. Cross browser /platform testing is easy (means change the browser in property
File)
4. Running test scripts in different Environment is easy
5. Running test scripts in different credentials is easy
6. We can create the test data prior the Suite execution (we can also get the
data from testData team)
7. Rerunning same test Script with multiple time with different data is easy

Data driven testing from Properties File

1. What is Properties File?


Properties is java feature file where we can store the data in from of key & values
pair,
Key & value data type should be always string .

2. Why Property file ?


Property file is light weight & faster to read the data compare to any other
file , & java as own Class to read the data from property
3. How to read data from properties File?
• Get the java representation Object of the Physical file using “FileInputSteam
• Create a Object of “Properties” class & load all the keys
• Read the data using getProperty(“Key”)
Note : properties file light weight & faster in execution compare to Excel

import java.io.FileInputStream;
import java.util.Properties;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;

public class ReadDataFromPropertiesFile {

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

WebDriverManager.edgedriver().setup();
WebDriver driver=new EdgeDriver();

FileInputStream fis = new


FileInputStream("./src/test/resources/CommonData2.properties");
//FileInputStream fis=new FileInputStream("C:\\Users\\Shobha\\Downloads\\
CommonData2.properties");

Properties pro = new Properties();


pro.load(fis);
String URL = pro.getProperty("url");
String UERNAME = pro.getProperty("username");
String PASSWORD = pro.getProperty("password");

driver.get(URL);
driver.findElement(By.id("user-name")).sendKeys(UERNAME);
driver.findElement(By.id("password")).sendKeys(PASSWORD);
driver.findElement(By.id("login-button")).click();

}
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------------

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Properties;

public class InsertDataToProperties_File {

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

Properties pro = new Properties();


pro.setProperty("username", "standard_user");
pro.setProperty("password", "secret_sauce");
pro.setProperty("url", "https://www.saucedemo.com/v1/");

FileOutputStream fos = new


FileOutputStream("./src/test/resources/Shobha.properties");
pro.store(fos, "COMMONDATA");
System.out.println("data written successfully");

}
-----------------------------------------------------------------------------------
-------------------------------------------------------------------------------

Data driven testing from Excel File

Apache Poi is the open source libraries used to get & write data from all
Microsoft documents like Excel , docx , ppt etc
In real time most the company preferred the keep the test script data in Excel,
because data will be in well-organized manner, so that modification & maintenance
is easier.

Installation steps:
1. Go the Maven Project
2. Edit POM.xml file
3. Go to https://mavenrepository.com
4. Search for apache-POI
5. Copy the dependency
6. Add dependency inside the <dependencies> in POM.xml

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>

program:-

import java.io.FileInputStream;
import org.apache.poi.ss.usermodel.Cell;
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;

public class FetchSingleDataFromExcel {

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

//Step1:-Set the path


//FileInputStream fes = new
FileInputStream("./src/test/resources/MyExcelData.xlsx");
FileInputStream fes = new FileInputStream("C:\\Users\\Shobha\\Desktop\\
MyExcelData.xlsx");

//step2:-Open WorkBook in read mode


Workbook book = WorkbookFactory.create(fes);

//step3:-get control to the sheet


Sheet sheet = book.getSheet("FirstData");

//step4:-get control on row


Row row = sheet.getRow(1);

//step5:-get control on cell


Cell cel = row.getCell(1);

String ExcelDAta = cel.getStringCellValue();

System.out.println(ExcelDAta);
}

}
-----------------------------------------------------------------------------------
----------------------------------------------------------------------------------

mport java.io.FileInputStream;

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.Cell;
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;

public class InsertSingleDataToExcel {

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


//Step1:-Set the path
FileInputStream fes = new FileInputStream("./src/test/resources/MyExcelData.xlsx");
//FileInputStream fes = new FileInputStream("C:\\Users\\Shobha\\Desktop\\
Book11.xlsx");

//step2:-Open WorkBook in read mode


Workbook book = WorkbookFactory.create(fes);

//step3:-get control to the sheet


Sheet sheet = book.getSheet("FirstData");

Row row = sheet.createRow(0);

Cell cel1 = row.createCell(0);

cel1.setCellValue("TestYantra");

FileOutputStream fos = new


FileOutputStream("./src/test/resources/MyExcelData.xlsx");
book.write(fos);
book.close();

}
-----------------------------------------------------------------------------------
------------------------------------------------------------------------------

You might also like