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.
For example, we can achieve Parallel execution using TestNG,
in that case browsers are passed as the parameters.
There are two ways to parameterize in TestNG:
1.Using
Parameters Annotation and TestNG XML
Scenario 1:-
Fill the username and Password values using Parameters
approach
Solution:-
We need to work with TestNG.xml file and the @parameter Annotation.
Solution:-
We need to work with TestNG.xml file and the @parameter Annotation.
TestNG.XML:-
In XML file we will create parameters as name/value pairs
and using its tag.
Our xml file will look like this :-
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="Suite"> <test name="uftHelp"> <parameter name="userName1" value="Test1"></parameter> <parameter name="pwd1" value="Pwd1"></parameter> <classes> <class name="srcTest.DataParameterization"/> </classes> </test> </suite>
Parameter annotation:-
We will map our XML file parameters with our main code, so
that we can utilize its values, it is done using @parameter annotations.
Our parameters code will look like this:-
@Parameters ({“userName1", “pwd1" })
Code:-
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(); } }
Scenario 2:-
We would utilize the above approach to implement the running
of same test case on various browsers (say Firefox, chrome)
Note:-
- @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.XML file, 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
2.Using
Dataprovider Annotation
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.
Approach:-
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.
Example:-
Example:-
@DataProvider(name = “TestMe”)
@Test (dataProvider =”TestME”)
Note:-
Data Provider is a unique feature in TestNG; it is not
available in JUnit. Many prefer TestNG because of its effective
parameterization results.