Sunday, December 9, 2018

TestNG Parameterization Using DataProviders

It is a TestNG annotation which is used to parameterize the test cases.

There are two ways to parameterize the test cases in TestNG.

1. Using testing.xml
2. Using DataProvider


 Here we will discuss how to parameterize the test cases using dataprovider.

   In TestNG, DataProvider is a method annotated by @DataProvider annotation.
   DataProvider returns 2D (two dimensional) array of objects.


Test Cases Parameterization:-
      To parameterize the test cases, the user needs to follow the below mentioned steps:-

      1. Create the dataprovider method with @Dataprovider annotations.(Methods returns      type must be 2D arrays).
      2. Declare the DataProvider name after the @Test annotation like i.e

        @Test (dataProvider=”dataprovider method name”) 


     3Pass the arguments in the test method, what you want to use from the dataprovider.


Below is the code of DataProvider:-

package seleniumPkg;


import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;


public class DataProvidersTest {
           
            @Test(dataProvider="getData")
            public void loginTest(String username,String pwd)
            {
                        System.out.println(username);
            }
           
           
            @DataProvider
            public Object [][] getData()
            {
                        Object[][] dataSet = new Object[4][2];
                       
                        dataSet[0][0] = "John";
                        dataSet[0][1] = "John123";
                       
                        dataSet[1][0] = "Ashish";
                        dataSet[1][1] = "Ashish123";
                       
                        dataSet[2][0] = "Emiy";
                        dataSet[2][1] = "Emiy123";
                       
                        dataSet[3][0] = "Alex";
                        dataSet[3][1] = "Alex123";
                       
                        return dataSet;
            }

}








No comments:

Post a Comment