MainMenu

Home Java Overview Maven Tutorials

Saturday 14 January 2017

Data Driven Framework in Selenium



Data Driven Framework:

For Video : CLICK HERE For Part1



For Video : CLICK HERE For Part2



1). A framework which is driven by data known as datadriven framework.
2). Mainly used when a test case need multiple data as input.
Method 1: Using 2D Array & By DataProvider annotation :-

package com.testDDF;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class DDFW
{
@Test(dataProvider = "chandan")
public void f(String uid, String pwd)
{
WebDriver driver = new FirefoxDriver();
driver.get("http://facebook.com");
driver.findElement(By.id("email")).sendKeys(uid);
driver.findElement(By.id("pass")).sendKeys(pwd);
driver.findElement(By.xpath("/html/body/div/div[1]/div/div/div/div/div[2]/form/table/tbody/tr[2]/td[3]/label/input")).click();
}
@DataProvider(name = "chandan")
public Object[][] getdata()
{
Object[][] mydata = new Object[1][2];
mydata[0][0] = "cchauhan707";
mydata[0][1] = "test@123";
return mydata;
}
}

Read the data from excel file : Befor starting with DataDriven framework with Excel file one should know how to interact with .xls file :
First of all , we have download ZAR file(Apache POI) by below link:
Download Apache POI link : https://poi.apache.org/download.html
Download the zip file for windows OS.
Then we have unzip it & should add in out project :

Sample code to access excel file :



package readexcel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Readexcel
{
public static void main(String args[]) throws Exception
{
File src = new File("C:\\Selenium Practice\\Readxl\\ExcelData\\TestData.xlsx");
FileInputStream fis = new FileInputStream(src);
XSSFWorkbook xb = new XSSFWorkbook(fis); //HSSFWorkbook will be used for .xls file
XSSFSheet ws = xb.getSheetAt(0);
String Data1 = ws.getRow(0).getCell(0).getStringCellValue();
System.out.println(Data1);
}
}

Method 2: By .xls File :-
1). It will support only .xls file as input.
2).It will use Apache POI API to read data from .xls files and write results In .xls files.
Here using below code we will login to facebook and login credential will be fetched from excel file :
first of all we will create a class and a constructor to read the file, then we create a method to get last row number & a method to get data of specific cell in excel :

package selTest;
import java.io.FileInputStream;
import java.io.File;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelDataConfig
{
XSSFWorkbook wb;
XSSFSheet sheet1;
public ExcelDataConfig(String excelpath)
{
try
{
File src = new File(excelpath);
FileInputStream fis = new FileInputStream(src);
wb = new XSSFWorkbook(fis);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
public String exceldata(int sheetnum,int rownum, int column)
{
sheet1 = wb.getSheetAt(sheetnum);
String data = sheet1.getRow(rownum).getCell(column).getStringCellValue();
return data;
}
public int getrowcount(int sheetIndex)
{
int row = wb.getSheetAt(sheetIndex).getLastRowNum();
row = row+1;
return row;
}
}

Here we move to web application & then get the data from excel as input



package selTest;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class DDE
{
WebDriver driver;
@BeforeTest
public void e()
{
driver = new FirefoxDriver();
driver.get("http://facebook.com");
}
@Test(dataProvider = "chandan")
public void f(String username, String password)
{
driver.findElement(By.id("email")).sendKeys(username);
driver.findElement(By.id("pass")).sendKeys(password);
driver.findElement(By.xpath("/html/body/div/div[1]/div/div/div/div/div[2]/form/table/tbody/tr[2]/td[3]/label/input")).click();
}
@DataProvider(name = "chandan")
public Object[][] inputdata()
{
ExcelDataConfig object = new ExcelDataConfig("C:\\Users\\CCHAUHAN\\workspace\\CSel\\src\\selTest\\TestData\\Inputdata.xlsx");
int t = object.getrowcount(0);
Object[][] edata = new Object[t][2];
for(int i = 0; i<=t; i++)
{
edata[i][0] = object.exceldata(0,i,0);
edata[i][1] = object.exceldata(0,i,1);
}
return edata;
}
}
Another Example : Here we will take data from excel and enter in google.com search box.

package dDEF;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
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.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
public class Goog
{
XSSFWorkbook wb;
XSSFSheet sheet;
FileInputStream fis;
WebDriver driver;
WebElement search;
@Test
public void beforeTest() throws Exception
{
try {
File src = new File("D:\\study material\\Selenium Final complete\\Selenium Test data\\Inputdatas.xlsx");
fis = new FileInputStream(src);
wb = new XSSFWorkbook(fis);
sheet = wb.getSheetAt(0);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
for(int i = 0; i<sheet.getLastRowNum(); i++)
{
String data = sheet.getRow(0).getCell(0).getStringCellValue();
search.sendKeys(data);
search.submit();
}
}
@BeforeTest
public void f()
{
driver = new FirefoxDriver();
driver.get("http://google.com");
search = driver.findElement(By.xpath("/html/body/div/div[3]/form/div[2]/div[2]/div[1]/div[1]/div[3]/div/div[3]/div/input[1]"));
}
@AfterTest
public void afterTest() throws IOException
{
wb.close();
fis.close();
}
}

Tags :

Data driven framework in selenium

How to design data driven framework in selenium using java

Example of data driven framework

Data driven framework using data provider annotation

No comments:

Post a Comment