Data Parameterization in TestNG – User friendly Tech help
Parameterization using TestNG plays a crucial role while creating the framework and automation scripts.It assists in running multiple iteration with different set of data.
n
n
For example, we can achieve Parallel execution using TestNG, in that case browsers are passed as the parameters.
n
n
There are two ways to parameterize in TestNG:
n
n
1.Using Parameters Annotation and TestNG XML
n
n
Scenario 1:-
n
Fill the username and Password values using Parameters approach
n
nSolution:-
nWe need to work with TestNG.xml file and the @parameter Annotation.
n
n
TestNG.XML:–
n
In XML file we will create parameters as name/value pairs and using its tag.
n
nOur xml file will look like this :-
n
n
n
Parameter annotation:-
n
We will map our XML file parameters with our main code, so that we can utilize its values, it is done using @parameter annotations.
n
n
Our parameters code will look like this:-
n
n
@Parameters ({“userName1″, “pwd1″ })
n
nCode:-
n
n
package srcTest;
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.Parameters;
import org.testng.annotations.Test;
public class DataParameterization {
private static WebDriver driver;
//Declaring the Parameters to receive values from TestNG.xml @Test
@Parameters({"sUserName","sPwd"})
public void login(String sUserName,String sPwd) {
driver = new FirefoxDriver();
//Adding Implicit wait driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//Maximize browser driver.manage().window().maximize();
//Open the Login Application
driver.get("http://www.ufthelp.com/p/testpage.html");
//Fill the UserName and Password fields
driver.findElement(By.id("userName")).sendKeys(sUserName);
driver.findElement(By.id("password")).sendKeys(sPwd);
//Click the Sign in Button
driver.findElement(By.id("SignIn")).click();
//Clicking on the alert message driver.switchTo().alert().accept();
//Destroying the object driver.quit(); }
}
n
Scenario 2:-
n
Execute our test on different browsers, based on the parameters.
n
n
We would utilize the above approach to implement the running of same test case on various browsers (say Firefox, chrome)
n
Note:-
n
- n
- @Parameter is applied On any method that already has a @Test, @Before/After or @Factory annotation, it can be applied atmost on one constructor of the test class.
- Incase @Parameters do not have a corresponding value in testing.xml.We can set @optional annotation value in the code.
- Remember @parameters can be placed in suite level and test level in our .xml file. But If same parameter name is declared in both places, test level parameter will get preference over suit level parameter.
- We cannot have duplicate name value pairs in the TestNG.XMLfile, every parameter name should be unique.Thus to test multiple set of data for the same name, we need to implement DataProviders.
- XML parameters should be mapped to the Java parameters in the code in the same order as they are found in the annotation, else TestNG will generate an mismatch error
n
n
n
n
n
n
2.Using Dataprovider Annotation
n
n
A Data Provider is used in case we need to pass complex parameters in the Test method. A Data Provider is simply a method annotated with @DataProvider; here, the Data Provider itself acts as a data source. An array of objects with parameters can also be drawn from an Excel, CSV, or Database file using third-party APIs such as JXL or Apache POI.
n
n
Approach:-
n
The @DataProvider method supplies the parameters to the @Test method. In order to receive the data from the @dataProvider method, the name of the Data Provider must match on both annotations.
nExample:-
n
@DataProvider(name = “TestMe”)
n
@Test (dataProvider =”TestME”)
n
Note:-
n
Data Provider is a unique feature in TestNG; it is not available in JUnit. Many prefer TestNG because of its effective parameterization results.
n