Home Create Data Driven Script-Eclipse

Create Data Driven Script-Eclipse

I have created data driven with TestNG so please create project with TestNG. Create a project first so please visit on the page (http://qatestingtips.com/create-project-in-eclipse-with-testng-class/).

Now take an example to login account by using data driven technique (Please use any login module of your choice). For this we have to create a data file like xlsx or xls.

Create an xls file like below and save it.

For now use below code example

package drivenpack;

import java.io.File;

import java.io.FileInputStream;

import org.openqa.selenium.chrome.ChromeDriver;

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.testng.annotations.Test;

public class DrivenClass {

@Test

public void login() throws Exception {

// TODO Auto-generated method stub

System.setProperty(“webdriver.chrome.driver”,”C:\\path\\chromedriver.exe”);

WebDriver driver =new ChromeDriver();

File obj=new File(“C:\\path\\testlogin.xlsx”);

// Load the file.

FileInputStream f1=new FileInputStream(obj);

XSSFWorkbook w1=new XSSFWorkbook(f1); //for .xlsx files

XSSFSheet sheet1=w1.getSheetAt(0); //select the sheet

int a= sheet1.getPhysicalNumberOfRows(); //last row count

System.out.println(“last row no =”+a);

for(int j=0;j<a;j++)

{System.out.println(j);

String data=sheet1.getRow(j).getCell(0).getStringCellValue();

System.out.print(“data=”+data);

driver.get(“https://domain url”); //write your domain ( go to any login form what would like to test or for practice)

driver.findElement(By.name(“email_address”)).sendKeys(sheet1.getRow(j).getCell(0).getStringCellValue()); // give element location

driver.findElement(By.name(“password”)).sendKeys(sheet1.getRow(j).getCell(1).getStringCellValue()); // give element location

driver.findElement(By.name(“submit”)).click(); // give element location

//}

//System.out.println(“\n”);

}

}

}

Now time to add supported jar files. Add selenium java and POI libraries.

Right click on package>>Build Path >>Configure build path

Click on libraries then click on add external jar files

Now add selenium java libraries. Select libraries and use same process to add libraries from its libs folder then click on open.

Now add POI libraries file (If not downloaded yet download then download and extract it) and click open then same process to add all libraries from different different folders wherever getting and finally click ok.

 

Note:- Don’t need to go inside doc folder if it is there. Add from other folders.

Like below screen will appear without any error.

Now run with TestNG script and get data execution result from data driven technique.