Friday, January 31, 2020

Data Provider

TestNG Data Provider


- Testing the same piece of software, we can't be unfair to test it with just one set of data.
- we need to verify that our system is taking all set of combinations which it expected to support.
- So comes Parameterization in the picture.
- To pass multiple data to the application at runtime, we need to parameterize our test scripts.
- This concept which we achieve by parameterization is called Data Driven Testing.

We will go through the parameterization options in Selenium Webdriver - TestNG.

There are two ways by which we can achieve parameterization in TestNG
  1. With the help of Parameters annotation and TestNG XML file.
    TestNG: Parameterization using XML & DataProvider in Selenium
  2. With the help of DataProvider annotation.
    TestNG: Parameterization using XML & DataProvider in Selenium

TestNG Data Providers


package DataProviderfromprog;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Reporter;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class Dataproviderprac {

WebDriver driver;
WebElement uname;
WebElement pass;
WebElement btnlogin;

@BeforeMethod
public void setup() {
System.setProperty("Webdriver.chrome.driver", "C:\\Users\\Administrator\\Downloads\\chromedriver_win32 (1)");
driver = new ChromeDriver();
driver.get("http://demo.cs-cart.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);

}
@DataProvider(name="LoginData")
public Object[][] getLoginData(){
Object[][] data=new Object[2][2];
data[0][0]= "Ram";
data[0][1]="Ram123";
data[1][0]= "Sham";
data[1][1]="sham123";
return data;
}
@Test(dataProvider="LoginData")
public void singinwithdifferntusernamesAndPasswords(String username, String password) {
driver.findElement(By.xpath("//html//body//div[2]//div[4]//div[1]//div//div//div[3]//div//div[1]//a//i[2]" + "")).click();
Reporter.log("Clicked on My Account <br>", true);
//driver.findElement(By.xpath("//a[@class='cm-dialog-opener cm-dialog-auto-size ty-btn ty-btn__secondary']")).click();
//Reporter.log("Clicked on Sign In Button");
driver.findElement(By.xpath("//*[@id=\"login_main_login\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"login_main_login\"]")).sendKeys(username);
Reporter.log("Entered username <br>", true);

driver.findElement(By.xpath("//*[@id=\"psw_main_login\"]")).clear();
driver.findElement(By.xpath("//*[@id=\"psw_main_login\"]")).sendKeys(password);
Reporter.log("E <br>", true);

driver.findElement(By.xpath("/html/body/div[2]/div[4]/div[3]/div/div[2]/div[1]/div/div/div/form/div[3]/div[1]/button")).click();
}
}


Data Provider using XLSX file:

Dependencies required to access data from xlsx file are : 


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


package DataProviderfromprog;
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.chrome.ChromeDriver;
import org.testng.Reporter;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DataProviderfromxls {
WebDriver driver;

WebElement uname;
WebElement pass;
WebElement btnlogin;
String filepath = "C:\\Users\\Administrator\\eclipse-workspace\\VisionDataprovider\\userpwd.xls";

@BeforeMethod
public void setup() {
System.setProperty("Webdriver.chrome.driver", "C:\\Users\\Administrator\\Downloads\\chromedriver_win32 (1)");
driver = new ChromeDriver();

driver.get("http://demo.cs-cart.com/");
driver.manage().window().maximize();
// driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

}

@Test(dataProvider = "logindataSupplier")
public void checkloginScenario(String username, String password) {

driver.findElement(
By.xpath("//html//body//div[2]//div[4]//div[1]//div//div//div[3]//div//div[1]//a//i[2]" + "")).click();
Reporter.log("Clicked on My Account <br>", true);

driver.findElement(By.xpath("//*[@id=\"login_main_login\"]")).clear();
uname = driver.findElement(By.xpath("//*[@id=\"login_main_login\"]"));
uname.sendKeys(username);
Reporter.log("Entered username <br>", true);

driver.findElement(By.xpath("//*[@id=\"psw_main_login\"]")).clear();
pass = driver.findElement(By.xpath("//*[@id=\"psw_main_login\"]"));
pass.sendKeys(password);
Reporter.log("E <br>", true);

driver.findElement(
By.xpath("/html/body/div[2]/div[4]/div[3]/div/div[2]/div[1]/div/div/div/form/div[3]/div[1]/button"))
.click();
}

@DataProvider(name = "logindataSupplier")
public Object[][] getlogintestdata() throws IOException {

FileInputStream fis = new FileInputStream(filepath);

XSSFWorkbook workbook = new XSSFWorkbook(fis);

XSSFSheet sheet = workbook.getSheetAt(0);

int rowcount = sheet.getLastRowNum();
System.out.println("rowcoun : " + rowcount);

int cellcount = sheet.getRow(0).getLastCellNum();
System.out.println("no of cells" + cellcount);

Object[][] testdata = new Object[rowcount][cellcount];

for (int i = 0; i < rowcount; i++) {
for (int j = 0; j < cellcount; j++) {

testdata[i][j] = sheet.getRow(i + 1).getCell(j).toString();

}

}

return testdata;

}
}



Summary:
  • Parameterization is require to create Data Driven Testing.
  • TestNG support two kinds of parameterization, using @Parameter+TestNG.xml and using@DataProvider
  • In @Parameter+TestNG.xml parameters can be placed in suite level and test level. If
    The Same parameter name is declared in both places; test level parameter will get preference over suit level parameter.
  • using @Parameter+TestNG.xml only one value can be set at a time, but @DataProvider return an 2d array of Object.
  • If DataProvider is present in the different class then the class where the test method resides,DataProvider should be static method.
  • There are two parameters supported by DataProvider are Method and ITestContext.

No comments:

Post a Comment

String

  Java String  class provides a lot of methods to perform operations on strings such as compare(), concat(), equals(), split(), length(), re...