Using Single @DataProvider Method To Store All WebDriver Test Case Data

We have learnt how to create and store multiple @DataProvider methods In single class file called dataProvider_Repository.java and then we can use those data In different test case execution as described In THIS PAGE. Using this, We can manage data of all test cases very easily In single file and If you wants to modify test data of any test case, You have to modify only single file.

Now, Instead of creating separate @DataProvider method for each test case, Is It possible to store all test cases data In single @DataProvider method? Yes we can do It by passing method name In @DataProvider method as shown In bellow example.

Create bellow given class files under TestNG_Advanced package.

LogInCase.java
package TestNG_Advanced;

import java.util.concurrent.TimeUnit;
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.Test;

public class LogInCase {
private static WebDriver driver;
 
    @BeforeTest
    public void setUp() {
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.get("http://only-testing-blog.blogspot.com/2014/05/login.html");
 }
 
    //This LogIn method will access data from dataProvider_Repository class file where dataProvider name Is BothData.
    @Test(dataProviderClass=dataProvider_Repository.class,dataProvider="BothData")
    public static void LogIn(String UID, String PASS) {
  driver.findElement(By.xpath("//input[@name='userid']")).sendKeys(UID);
  driver.findElement(By.xpath("//input[@name='pswrd']")).sendKeys(PASS);;
  driver.findElement(By.xpath("//input[@type='submit']")).click();
  driver.switchTo().alert().accept();   
 }
}

FormSubmit.java
package TestNG_Advanced;

import java.util.concurrent.TimeUnit;

public class FormSubmit {
private static WebDriver driver;
 
    @BeforeTest
    public void setUp() {
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.get("http://only-testing-blog.blogspot.com/2014/05/form.html");
 }
 
    //This Submit method will access data from dataProvider_Repository class file where dataProvider name Is BothData.
    @Test(dataProviderClass=dataProvider_Repository.class,dataProvider="BothData")
    public static void Submit(String fname, String lname, 
            String email, String mobile, 
            String company) {
  
  driver.findElement(By.xpath("//input[@name='FirstName']")).sendKeys(fname);
  driver.findElement(By.xpath("//input[@name='LastName']")).sendKeys(lname);
  driver.findElement(By.xpath("//input[@name='EmailID']")).sendKeys(email);
  driver.findElement(By.xpath("//input[@name='MobNo']")).sendKeys(mobile);
  driver.findElement(By.xpath("//input[@name='Company']")).sendKeys(company);
  driver.findElement(By.xpath("//input[@type='submit']")).click();
  driver.switchTo().alert().accept();   
 }
}

dataProvider_Repository.java
package TestNG_Advanced;

import java.lang.reflect.Method;

import org.testng.annotations.DataProvider;

public class dataProvider_Repository { 
 
 //Pass Method name In BothCaseData method.
 @DataProvider(name="BothData")
 public static Object[][] BothCaseData(Method mtd){
  
  Object detail[][] = null;
  
  //If method name = LogIn, Use this data.
  if(mtd.getName().equalsIgnoreCase("LogIn")){
   
   detail = new Object[3][2];
   detail[0][0]="UserName1";
   detail[0][1]="Password1";
  
   detail[1][0]="UserName2";
   detail[1][1]="Password2";
  
   detail[2][0]="UserName3";
   detail[2][1]="Password3";
  }
  //If method name = Submit, Use this data.
  else if(mtd.getName().equalsIgnoreCase("Submit")){
   
   detail = new Object[3][5];
   detail[0][0]="fName1";
   detail[0][1]="lname1";
   detail[0][2]="email1@youraccount.com";
   detail[0][3]="mobno1";
   detail[0][4]="company1";  
   
   detail[1][0]="fName2";
   detail[1][1]="lname2";
   detail[1][2]="email2@youraccount.com";
   detail[1][3]="mobno2";
   detail[1][4]="company2";
   
   detail[2][0]="fName3";
   detail[2][1]="lname3";
   detail[2][2]="email3@youraccount.com";
   detail[2][3]="mobno3";
   detail[2][4]="company3";
  }
  
  return detail;
 } 
}

Test cases and test data are same as described In PREVIOUS EXAMPLE. But way of storing test data and accessing them In test cases Is different, Previously we have created separate @DataProvider method for both test cases. But now we have created single @DataProvider method to store data of both test cases. If else condition will check test method's name and based on that test data will be provided.

Now you can run both test cases one by one to see results.

No comments:

Post a Comment