Saturday, July 20, 2019

Data driven framework example in selenium

What is Data driven framework in selenium?

-in simple terms as per as name "Data driven" framework means a framework which is run by data.

Suppose if we are testing login feature of any site, that time we are dealing with multiple data combination i.e ( valid data , invalid data ). so that time dealing with those data become complex.

so if we make a framework which is data driven it will save a lot of time in testing, also it will reduce the complexity

so let me show you what i mean actually...



this is dummy file which have fake login credential.
if you dont have time to create dummy data than you can use this site

here the sheet name is "Sheet1"
now, i will write one Excel uttils class which will handle all the Excel related operations.

package utills;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Excelutills {
 static XSSFWorkbook workbook;
 static XSSFSheet sheet;
 
 static String path=System.getProperty("user.dir");
public Excelutills(String wbook, String sheetname) throws IOException
{
 workbook=new XSSFWorkbook(wbook);
 sheet=workbook.getSheet(sheetname);
}
 

 public static String getdata(int row, int cell) throws IOException{
  
   String cellvalue = sheet.getRow(row).getCell(cell).getStringCellValue();
     // System.out.println("cell value is:"+cellvalue);
  return cellvalue;
  
 }
 public int rows_count() {
  int rows = sheet.getPhysicalNumberOfRows();
 
  return rows;
 }
 
 public int col_count(){
  
  int cellnum =sheet.getRow(0).getPhysicalNumberOfCells();
  //System.out.println("total number of cells are:"+cellnum);
  return cellnum;
  
 }

}



now, i will use this class's constructor so that i can use all the functions of this class into any another class.

You can also copy this code and use it in your use.


now i have created another class in which i will use this Excellutill class's method to create a data driven framework.



package utills;

import java.io.IOException;

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.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class Data2 {
 WebDriver driver = null;
 
 public Object[][] testdata() throws IOException {
  
  String path=System.getProperty("user.dir");
  Excelutills data = new Excelutills(path+"/Excel/login.xlsx", "Sheet1");
  int row=data.rows_count();
  int col=data.col_count();
   
  
  Object[][] data2 = new Object[row-1][col];

  for(int i=1;i<row;i++)
   
  {
   for(int j=0;j<col;j++)
    
   {
    String values = Excelutills.getdata(i, j);
    
    data2[i-1][j]=values;
   }
   
   
  }
  
  return data2;
  
  
 }
 @DataProvider(name="logindata")
 public Object[][] test() throws IOException
 
 {
 
    Object[][] testdata= testdata();
 return testdata;       
 }
 
 @Test(dataProvider="logindata")
 public void logindata(String username, String password)
 {
   System.setProperty("webdriver.chrome.driver","C:\\Users\\5558\\eclipse-workspace\\chromedriver.exe");
  driver =  new ChromeDriver();
  driver.get("https://www.makemytrip.com");
  
  driver.manage().window().maximize();
  WebElement login = driver.findElement(By.xpath("(//li[contains(@class,'makeFlex hrtlCenter')])[5]"));
  login.click();
  WebElement username1 = driver.findElement(By.id("username"));
  WebElement password1 = driver.findElement(By.id("password"));
  username1.sendKeys(username);
  password1.sendKeys(password);
  
 }

 
}


Guys i hope i helped you to explain this data driven framework 😄
this is my first ever post on bloggers so you might not like the presentation but i will keep posting new blogs about selenium webdriver,  as i am also learner like you :)


thank you for read this blog and if you like this blog please share it
-Thanks




No comments:

Post a Comment