SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

Store And Access All DataProviders From Single File For WebDriver Test Cases

We have already learnt how to create and use testng data providers In selenium webdriver test cases In THIS POST. Also we have used testng data providers In Selenium WebDriver Data Driven Framework creation so all you are now very familiar with testng data providers and how to use them In our automation test case creation.

Now supposing you have multiple test cases and each test case has Its own data provider method to store required data for that test case. There Is not any Issue to use data providers In this manner but It Is not good manner. It Is very hard to manage data In this way because If you wants to modify data of few test cases then you have to open all those test cases files one by one and then you can modify them.

Best way to manage and access test data of all test cases very easily Is : Store all data provider method In single file. So whenever you wants to modify test data of one or multiple test cases, You need to open only single file.

I have created simple example to store test data of two different test cases In single file. First test Is LogIn test(LogInCase.java) and second test Is form submission test(FormSubmit.java). Both test cases need different test data to execute test case and I have created separate file (dataProvider_Repository.java) to store data provider methods of both test cases as bellow.

Create bellow give three class file under TestNG_Advanced package In eclipse.

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.in/2014/05/login.html");
 }
 
    //This LogIn method will access data from dataProvider_Repository.java class file where dataProvider name Is LogInData.
    @Test(dataProviderClass=dataProvider_Repository.class,dataProvider="LogInData")
    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;
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 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.in/2014/05/form.html");
 }
 
 //This Submit method will access data from dataProvider_Repository.java class file where dataProvider name Is FormData.
 @Test(dataProviderClass=dataProvider_Repository.class,dataProvider="FormData")
    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 org.testng.annotations.DataProvider;

public class dataProvider_Repository {
 
 //Test data to use In LogInCase.java file.
 @DataProvider(name="LogInData")
 public static Object[][] LogInCaseData(){
  
  Object 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";
  
  return detail;
 }
 
 //Test data to use In FormSubmit.java file.
 @DataProvider(name="FormData")
 public static Object[][] FormSubmitData(){
  
  Object 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;
 }
}

You can see that we have created two different @DataProvider methods In dataProvider_Repository.java file and we have accessed them In LogInCase.java and FormSubmit.java files using syntax like bellow.
@Test(dataProviderClass=dataProvider_Repository.class,dataProvider="LogInData")
@Test(dataProviderClass=dataProvider_Repository.class,dataProvider="FormData")

Now If you will run LogInCase.java test then It will access data from LogInData @dataprivider method to execute test and testng out put will looks like bellow at the end of test execution.


Same way, When you run FormSubmit.java file, It will access data from FormData @dataprivider method and test execution result will looks like bellow.


You can save more @dataprivider methods In dataProvider_Repository.java file If you have more such test cases.

2 comments:

  1. Thanks for this Nice Article Arvind. I Executed the code working Fine.

    But suggest that how to increase the speed of the execution using Chrome Browser. When working in FF it starts quickly but Chrome is little slow...

    ReplyDelete
    Replies
    1. we can not control execution speed..

      Delete