WebDriver Test Data Driven Testing Using TestNG @DataProvider Annotation

Data driven testing Is most Important topic for all software testing automation tools because you need to provide different set of data In your tests. If you are using selenium IDE software automation testing tool and you wants to perform data driven software testing in your software test then THESE POSTS will helps you. For Selenium Webdriver, Data driven testing using excel file Is very easy. For that you need support of Java Excel API and It Is explained very clearly In THIS POST. Now If you are using TestNG framework for selenium webdriver software testing tool then there Is one another way to perform data driven testing.

TestNG @DataProvider Annotation
@DataProvider Is TestNG annotation. @DataProvider Annotation of testng framework provides us a facility of storing and preparing data set In method. Task of @DataProvider annotated method Is supplying data for a test method. Means you can configure data set In that method and then use that data In your test method. @DataProvider annotated method must return an Object[][] with data.

Let us see simple example of data driven software automation testing using @DataProvider annotation. We have to create 2 dimensional array In @DataProvider annotated method to store data as shown In bellow given example. You can VIEW THIS POST to know more about two dimensional array In java.
Bellow given example will retrieve userid and password one by one from @DataProvider annotated method and will feed them In LogIn_Test(String Usedid, String Pass) one by one. Run this example In your eclipse and observe result.

package Testng_Pack;

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

public class Sample_Login {
 
 WebDriver driver = null;
 
 @BeforeTest
    public void setup() throws Exception { 
         System.setProperty("webdriver.gecko.driver", "D:\\Selenium Files\\geckodriver.exe");
         driver = new FirefoxDriver();
         driver.manage().window().maximize();
         driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
         driver.get("http://only-testing-blog.blogspot.com/2014/05/login.html"); 
    } 
 
  @AfterTest
 public void tearDown() throws Exception { 
   driver.quit();
     } 
 
 //This method will return two dimensional array.
 //This method behaves as data provider for LogIn_Test method.
 @DataProvider
 public Object[][] LoginCredentials(){
  //Created two dimensional array with 4 rows and 2 columns.
  //4 rows represents test has to run 4 times.
  //2 columns represents 2 data parameters.
  Object[][] Cred = new Object[4][2];
  
  Cred[0][0] = "UserId1";
  Cred[0][1] = "Pass1";
  
  Cred[1][0] = "UserId2";
  Cred[1][1] = "Pass2";
  
  Cred[2][0] = "UserId3";
  Cred[2][1] = "Pass3";
  
  Cred[3][0] = "UserId4";
  Cred[3][1] = "Pass4";
  return Cred; //Returned Cred
 }
 
 //Give data provider method name as data provider.
 //Passed 2 string parameters as LoginCredentials() returns 2 parameters In object.
 @Test(dataProvider="LoginCredentials")
 public void LogIn_Test(String Usedid, String Pass){
   driver.findElement(By.xpath("//input[@name='userid']")).clear();
   driver.findElement(By.xpath("//input[@name='pswrd']")).clear();
   driver.findElement(By.xpath("//input[@name='userid']")).sendKeys(Usedid);
   driver.findElement(By.xpath("//input[@name='pswrd']")).sendKeys(Pass);
   driver.findElement(By.xpath("//input[@value='Login']")).click();
   String alrt = driver.switchTo().alert().getText();
   driver.switchTo().alert().accept();
   System.out.println(alrt);
  }
}

testng.xml file to run this example Is as bellow.
<suite name="Simple Suite">
 <test name="Simple Skip Test">
  <classes>
   <class name = "Testng_Pack.Sample_Login"/>
  </classes>
 </test> 
</suite>

When you will run above example In eclipse, It will enter UserID and passwords one by one In UserId and password text box of software web page. TestNG result report will looks like bellow.



4 comments:

  1. Hi Arivind :
    you used wrong url , what ever url you gave the test is failed.
    i tried using this url , http://only-testing-blog.blogspot.in/2014/05/login.html then test is passed
    pls let me know if i am wrong.
    Thanks,
    Sushma.

    ReplyDelete
  2. Hi Arivind ,

    you passed default user id and password (Usedid,Pass) , but we have four user logins how it will take dynamically can you explain

    ReplyDelete
  3. Hello Sir,

    Could you please tell me how can I handle more than one test cases in same class?

    ReplyDelete
  4. could you please tell me how can we save tha output value in same excel sheet

    ReplyDelete