What is data driven testing?

Data Driven Testing Framework is used to separate the test script from the test data. You can test the same script with multiple sets of data.


ExcelFileReader.java file is necessary for connect to excel file.

ExcelFileReader.java


Option 1:

package Utility;


import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

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

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

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

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


public class ExcelFileReader {


XSSFWorkbook wb;

XSSFSheet sheet;

File file;

String filePath;

FileInputStream fi;

public ExcelFileReader(String filePath) {


this.filePath=filePath;

try {

file = new File(filePath);

fi = new FileInputStream(file);

wb = new XSSFWorkbook(fi);


} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


}


public int totalRow(String sheetName) {


sheet = wb.getSheet(sheetName);


int rows = sheet.getLastRowNum()+1;

return rows;


}


public int totalColumn(String sheetName) {

sheet = wb.getSheet(sheetName);

int cols = sheet.getRow(0).getLastCellNum();

//cols = cols - 1;   

// cols = cols + 1;

return cols;

}

public String getData(String sheetName, int rowNum, int columnNum) {


XSSFCell cell = wb.getSheet(sheetName).getRow(rowNum).getCell(columnNum);

cell.setCellType(CellType.STRING);

String data = wb.getSheet(sheetName).getRow(rowNum).getCell(columnNum).getStringCellValue();

return data;

}

public void setData(String sheetName, int rowNum, int columnNum, String data) {

System.out.println(sheetName);

System.out.println(rowNum);

System.out.println(columnNum);

System.out.println(data);

try {

file = new File(filePath);

fi = new FileInputStream(file);

wb = new XSSFWorkbook(fi);


} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println(filePath);

wb.getSheet(sheetName).getRow(rowNum).createCell(columnNum).setCellValue(data);

try {

FileOutputStream fo = new FileOutputStream(new File(filePath));

wb.write(fo);

wb.close();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}



BaseInit.java


package BaseClass;


import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.Properties;

import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.Test;

import Utility.ExcelFileReader;


public class BaseInit {


public static WebDriver driver;

public static Properties sitedata;

public static Properties objdata;

public static ExcelFileReader suite;

public static Logger log;

@Test

public static void startup() throws IOException {

sitedata = new Properties();

FileInputStream fi = new FileInputStream(System.getProperty("user.dir")+"/src/main/resources/Properties/sitedata.properties");

sitedata.load(fi);

objdata = new Properties();

FileInputStream fo = new FileInputStream(System.getProperty("user.dir")+"/src/main/resources/Properties/ObjectData.properties");

objdata.load(fo);

if (sitedata.getProperty("browser").equalsIgnoreCase("chrome")) {

System.setProperty("webdriver.chrome.driver", "/Users/uss/Downloads/chromedriver");

driver=new ChromeDriver();

}

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

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

suite = new ExcelFileReader(System.getProperty("user.dir")+"/src/main/resources/ExcelSheet/RxValet.xlsx");

}

public static WebElement epresent(String propKey) {

try {

if(propKey.contains("xpath")) {

return driver.findElement(By.xpath(objdata.getProperty(propKey)));

}else if(propKey.contains("id")) {

return driver.findElement(By.id(objdata.getProperty(propKey)));

}if(propKey.contains("name")) {

return driver.findElement(By.name(objdata.getProperty(propKey)));

}if(propKey.contains("linkText")) {

return driver.findElement(By.linkText(objdata.getProperty(propKey)));

}else {

System.out.println("Element not found in the properties file");

return null;

}

}catch(Exception e) {

System.out.println("Element not found in the properties file");


}

return null;

}

}



SignInDdt.java


package MyMethods;


import org.testng.annotations.DataProvider;

import org.testng.annotations.Test;


import BaseClass.BaseInit;

import Utility.ExcelFileReader;


public class SignInDdt extends BaseInit {


@Test(dataProvider="getdataXls")

public static void login(String email, String pass) throws InterruptedException {

driver.get(sitedata.getProperty("url"));

Thread.sleep(5000);

try{

epresent("Login_id").click();

epresent("email_id").sendKeys(email);

epresent("pass_id").sendKeys(pass);

epresent("login_button_id").click();

}catch(Exception e) {

System.out.println("Invalid credentials");

}


}


@DataProvider

public static Object[][] getdataXls() {

return getTestData(suite, "Login");

}


public static Object[][] getTestData(ExcelFileReader data, String sheetname) {


int rows = data.totalRow(sheetname);

int cols = data.totalColumn(sheetname);


Object[][] myData = new Object[rows - 1][cols];


for (int row = 1; row < rows; row++) {


for (int col = 0; col < cols; col++) {


myData[row - 1][col] = data.getData(sheetname, row, col);

}

}


return myData;

}


}



Option 2:

package module1;


import java.util.concurrent.TimeUnit;


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.firefox.FirefoxDriver;

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

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.DataProvider;

import org.testng.annotations.Test;


public class DDTLoginTest {


WebDriver driver;

@Test(dataProvider="getTestData")

public void testHTMLElement(String email, String pwd) {

driver.get("http://localhost/uth/gadgetsgallery/catalog");


driver.findElement(By.linkText("log yourself in")).click();

driver.findElement(By.name("email_address")).sendKeys(email);


driver.findElement(By.name("password")).sendKeys(pwd);


driver.findElement(By.id("tdb1")).click();


driver.findElement(By.xpath("//*[@id=\"top_menu\"]/span[6]/a")).click();

driver.findElement(By.id("tdb1")).click();


driver.findElement(By.linkText("create an account")).click();

driver.findElement(By.xpath("//*[@id=\"content\"]/div[2]/form/div/div[2]/table/tbody/tr[1]/td[2]/input[1]")).click();



//driver.findElement(By.xpath("//html/body/div[2]/div/div/div/div[1]/form/div[1]/div/div/div/span[2]/svg"))

WebElement country = driver.findElement(By.name("country"));

Select sel = new Select(country);

sel.selectByVisibleText("India");

driver.findElement(By.name("newsletter")).click();


}

@DataProvider

public Object[][] getTestData() {

Object[][] data = new Object[3][2];

data[0][0] = "demo@unicodetechnologies.in";

data[0][1] = "unicode";


data[1][0] = "demo1@unicodetechnologies.in";

data[1][1] = "unicode1";

data[2][0] = "demo2@unicodetechnologies.in";

data[2][1] = "unicode2";

return data;

}

}


}


Comments

Popular posts from this blog

For Get Dynamic data & USe it into Postman Body parameters

API Automation Testing Script to check Response

Login through Data Driven Testing in selenium

Which dependencies needed in pom.xml while doing automation through selenium?

Download Java(JDK) & Eclipse IDE for selenium

How to extract all the links in selenium?