Friday, 15 August 2014

Data driven testing in TestNG (Using @DataProvider annotaion)





-> @DataProvider annotations in TestNG is important for data driven framework in selenium.

-> @DataProvider Annotation of testng framework provides us a facility of storing and preparing data set In method.

-> Task for @DataProvider Annotation is supplying data for test method. That means we can configure data with in the @DataProvider annotations and use those data inside the @Test methods.

-> @dataprovider method should return an Object[][] with data.

-> we have to create two dimensional array in @DataProvider annotation data to stored the data.



Example:


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.



public class Sample_Login {

 WebDriver driver = new FirefoxDriver();


 @BeforeTest
    public void setup() throws Exception {
         driver.manage().window().maximize();
         driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
         driver.get("http://only-testing-blog.blogspot.in/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>




After run the above code, the user id and password entered one by one in above URL.

1 comment:

  1. A nice article here with some useful tips for those who are not used-to comment that frequently. Thanks for this helpful information I agree with all points you have given to us. I will follow all of them.
    Selenium Training in Velachery |
    Best Selenium Training Institute in Chennai

    ReplyDelete