- Testing the same piece of software, we can't be unfair to test it with just one set of data.
- we need to verify that our system is taking all set of combinations which it expected to support.
- So comes Parameterization in the picture.
- To pass multiple data to the application at runtime, we need to parameterize our test scripts.
- This concept which we achieve by parameterization is called Data Driven Testing.
We will go through the parameterization options in Selenium Webdriver - TestNG.
There are two ways by which we can achieve parameterization in TestNG
With the help of Parameters annotation and TestNG XML file.
With the help of Parameters annotation and TestNG XML file.
With the help of DataProvider annotation.
Parameters from Testng.xml can be suite or test level
Parameter from DataProvider can take Method and ITestContext as the parameter.
Parameters annotation with Testng.xml
Select parameterization using annotations when you do want to deal with complexity & the number of input combinations are less.
Let see how this works
Test Scenario
Step 1) Launch browser & go to Google.com
Step 2) Enter a search keyword
Step 3) Verify the inputted value is same as that provided by our test data
Step 4) Repeat 2 & 3 until all values are inputted
package parameters;
import org.testng.annotations.Test;
import org.testng.AssertJUnit;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class NoParameterWithTestNGXML {
String driverPath = "C:\\geckodriver.exe";
WebDriver driver;
@Test
public void testNoParameter() throws InterruptedException{
String author = "guru99";
String searchKey = "india";
System.setProperty("webdriver.gecko.driver", driverPath);
driver= new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://google.com");
WebElement searchText = driver.findElement(By.name("q"));
//Searching text in google text box
searchText.sendKeys(searchKey);
System.out.println("Welcome ->"+author+" Your search key is->"+searchKey);
System.out.println("Thread will sleep now");
Thread.sleep(3000);
System.out.println("Value in Google Search Box = "+searchText.getAttribute("value") +" ::: Value given by input = "+searchKey);
//verifying the value in google search box
AssertJUnit.assertTrue(searchText.getAttribute("value").equalsIgnoreCase(searchKey));
}
}
Now, let's parameterize this using TestNG
To do so, you will need to
Here is the complete code
Test Level TestNG.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestSuite" thread-count="3" >
<parameter name="author" value="Guru99" />
<parameter name="searchKey" value="India" />
<test name="testGuru">
<parameter name="searchKey" value="UK" />
<classes>
<class name="parameters.ParameterWithTestNGXML">
</class>
</classes>
</test>
</suite>
ParameterWithTestNGXML.java File
package parameters;
import org.testng.AssertJUnit;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class ParameterWithTestNGXML {
String driverPath = "C:\\geckodriver.exe";
WebDriver driver;
@Test
@Parameters({"author","searchKey"})
public void testParameterWithXML( @Optional("Abc") String author,String searchKey) throws InterruptedException{
System.setProperty("webdriver.gecko.driver", driverPath);
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://google.com");
WebElement searchText = driver.findElement(By.name("q"));
//Searching text in google text box
searchText.sendKeys(searchKey);
System.out.println("Welcome ->"+author+" Your search key is->"+searchKey);
System.out.println("Thread will sleep now");
Thread.sleep(3000);
System.out.println("Value in Google Search Box = "+searchText.getAttribute("value") +" ::: Value given by input = "+searchKey);
//verifying the value in google search box
AssertJUnit.assertTrue(searchText.getAttribute("value").equalsIgnoreCase(searchKey));
}
}
Note: To run the script, select the XML file and Run as Test NG Suite.
Now, parameters can be defined at 2 levels
- Suite level – The parameters inside the <suite> tag of TestNG XML file will be a suite level parameter.
- Test Level -- The parameters inside the <Test> tag of testing XML file will be a Test level parameter.
Here is the same test with suite level parameters
Parameters using Dataprovider
@Parameters annotation is easy but to test with multiple sets of data we need to use Data Provider.
To fill thousand's of web forms using our testing framework we need a different methodology which can give us a very large dataset in a single execution flow.
This data driven concept is achieved by @DataProvider annotation in TestNG.
It has only one attribute 'name'. If you do not specify the name attribute then the DataProvider's name will be same as the corresponding method name.
Data provider returns a two-dimensional JAVA object to the test method and the test method, will invoke M times in a M*N type of object array. For example, if the DataProvider returns an array of 2*3 objects, the corresponding testcase will be invoked 2 times with 3 parameters each time.
Complete Example
package parameters;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class ParameterByDataprovider {
WebDriver driver;
String driverPath = "C:\\geckodriver.exe";
@BeforeTest
public void setup(){
//Create firefox driver object
System.setProperty("webdriver.gecko.driver", driverPath);
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://google.com");
}
/** Test case to verify google search box
* @param author
* @param searchKey
* @throws InterruptedException
*/
@Test(dataProvider="SearchProvider")
public void testMethod(String author,String searchKey) throws InterruptedException{
{
WebElement searchText = driver.findElement(By.name("q"));
//search value in google searchbox
searchText.sendKeys(searchKey);
System.out.println("Welcome ->"+author+" Your search key is->"+searchKey);
Thread.sleep(3000);
String testValue = searchText.getAttribute("value");
System.out.println(testValue +"::::"+searchKey);
searchText.clear();
//Verify if the value in google search box is correct
Assert.assertTrue(testValue.equalsIgnoreCase(searchKey));
}
}
/**
* @return Object[][] where first column contains 'author'
* and second column contains 'searchKey'
*/
@DataProvider(name="SearchProvider")
public Object[][] getDataFromDataprovider(){
return new Object[][]
{
{ "Guru99", "India" },
{ "Krishna", "UK" },
{ "Bhupesh", "USA" }
};
}
}
Invoke DataProvider from different class
By default, DataProvider resides in the same class where test method is or its base class. To put it in some other class we need to make data provider method as static and in test method we need to add an attribute dataProviderClass in @Test annotation.
Code Example
TestClass ParameterDataproviderWithClassLevel.java
package parameters;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class ParameterDataproviderWithClassLevel {
WebDriver driver;
String driverPath = "C:\\geckodriver.exe";
@BeforeTest
public void setup(){
System.setProperty("webdriver.gecko.driver", driverPath);
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://google.com");
}
@Test(dataProvider="SearchProvider",dataProviderClass=DataproviderClass.class)
public void testMethod(String author,String searchKey) throws InterruptedException{
WebElement searchText = driver.findElement(By.name("q"));
//Search text in google text box
searchText.sendKeys(searchKey);
System.out.println("Welcome ->"+author+" Your search key is->"+searchKey);
Thread.sleep(3000);
//get text from search box
String testValue = searchText.getAttribute("value");
System.out.println(testValue +"::::"+searchKey);
searchText.clear();
//verify if search box has correct value
Assert.assertTrue(testValue.equalsIgnoreCase(searchKey));
}
}
DataproviderClass.java
package parameters;
import org.testng.annotations.DataProvider;
public class DataproviderClass {
@DataProvider(name="SearchProvider")
public static Object[][] getDataFromDataprovider(){
return new Object[][] {
{ "Guru99", "India" },
{ "Krishna", "UK" },
{ "Bhupesh", "USA" }
};
}}
Types of Parameters in Dataprovider
There are two type of parameters supported by DataProvider method.
Method- If the SAME DataProvider should behave differently with different test method , use Method parameter.
In the following example ,
- We check if method name is testMethodA.
- If yes return one set of value
- Else return another set of value
package parameters;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class ParameterByMethodInDataprovider{
WebDriver driver;
String driverPath = "C:\\geckodriver.exe";
@BeforeTest
public void setup(){
System.setProperty("webdriver.gecko.driver", driverPath);
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://google.com");
}
@Test(dataProvider="SearchProvider")
public void testMethodA(String author,String searchKey) throws InterruptedException{
WebElement searchText = driver.findElement(By.name("q"));
//Search text in search box
searchText.sendKeys(searchKey);
//Print author and search string
System.out.println("Welcome ->"+author+" Your search key is->"+searchKey);
Thread.sleep(3000);
String testValue = searchText.getAttribute("value");
System.out.println(testValue +"::::"+searchKey);
searchText.clear();
//Verify if google text box is showing correct value
Assert.assertTrue(testValue.equalsIgnoreCase(searchKey));
}
@Test(dataProvider="SearchProvider")
public void testMethodB(String searchKey) throws InterruptedException{
{
WebElement searchText = driver.findElement(By.name("q"));
//Search text in search box
searchText.sendKeys(searchKey);
//Print only search string
System.out.println("Welcome ->Unknown user Your search key is->"+searchKey);
Thread.sleep(3000);
String testValue = searchText.getAttribute("value");
System.out.println(testValue +"::::"+searchKey);
searchText.clear();
//Verify if google text box is showing correct value
Assert.assertTrue(testValue.equalsIgnoreCase(searchKey));
}
}
/**
* Here DataProvider returning value on the basis of test method name
* @param m
* @return
**/
@DataProvider(name="SearchProvider")
public Object[][] getDataFromDataprovider(Method m){
if(m.getName().equalsIgnoreCase("testMethodA")){
return new Object[][] {
{ "Guru99", "India" },
{ "Krishna", "UK" },
{ "Bhupesh", "USA" }
};}
else{
return new Object[][] {
{ "Canada" },
{ "Russia" },
{ "Japan" }
};}
}
}
Here is the output
ITestContext- It can use to create different parameters for test cases based on groups.
In real-life, you can use ITestContext to vary parameter values based on Test Methods, hosts, configurations of the test.
In the following code example
- We have 2 groups A & B
- Each test method is assigned to a group
- If value of group is A, a particular data set is returned
- If value of group is B, another data set is returned
package parameters;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.ITestContext;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class ParameterByITestContextInDataprovider {
WebDriver driver;
String driverPath = "C:\\geckodriver.exe";
@BeforeTest(groups={"A","B"})
public void setup(){
System.setProperty("webdriver.gecko.driver", driverPath);
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://google.com");
}
@Test(dataProvider="SearchProvider",groups="A")
public void testMethodA(String author,String searchKey) throws InterruptedException{
{
//search google textbox
WebElement searchText = driver.findElement(By.name("q"));
//search a value on it
searchText.sendKeys(searchKey);
System.out.println("Welcome ->"+author+" Your search key is->"+searchKey);
Thread.sleep(3000);
String testValue = searchText.getAttribute("value");
System.out.println(testValue +"::::"+searchKey);
searchText.clear();
//verify correct value in searchbox
Assert.assertTrue(testValue.equalsIgnoreCase(searchKey));
}
}
@Test(dataProvider="SearchProvider",groups="B")
public void testMethodB(String searchKey) throws InterruptedException{
{
//find google search box
WebElement searchText = driver.findElement(By.name("q"));
//search a value on it
searchText.sendKeys(searchKey);
System.out.println("Welcome ->Unknown user Your search key is->"+searchKey);
Thread.sleep(3000);
String testValue = searchText.getAttribute("value");
System.out.println(testValue +"::::"+searchKey);
searchText.clear();
//verify correct value in searchbox
Assert.assertTrue(testValue.equalsIgnoreCase(searchKey));
}
}
/**
* Here the DAtaProvider will provide Object array on the basis on ITestContext
* @param c
* @return
*/
@DataProvider(name="SearchProvider")
public Object[][] getDataFromDataprovider(ITestContext c){
Object[][] groupArray = null;
for (String group : c.getIncludedGroups()) {
if(group.equalsIgnoreCase("A")){
groupArray = new Object[][] {
{ "Guru99", "India" },
{ "Krishna", "UK" },
{ "Bhupesh", "USA" }
};
break;
}
else if(group.equalsIgnoreCase("B"))
{
groupArray = new Object[][] {
{ "Canada" },
{ "Russia" },
{ "Japan" }
};
}
break;
}
return groupArray;
}
}
Note: If you directly run your testng class, it will first call dataprovider which can't get groups information as groups are not available. But instead if you call this class via testng.xml, it will have groups info available with ITestContext. Use the following XML to call the test
<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
<suite name="test-parameter">
<test name="example1">
<groups>
<run>
<include name="A" />
</run>
</groups>
<classes>
<class
name="parameters.ParameterByITestContextInDataprovider" />
</classes>
</test>
<test name="example2">
<groups>
<run>
<include name="B" />
</run>
</groups>
<classes>
<class
name="parameters.ParameterByITestContextInDataprovider" />
</classes>
</test>
</suite>
Summary:
- Parameterization is require to create Data Driven Testing.
- TestNG support two kinds of parameterization, using @Parameter+TestNG.xml and using@DataProvider
In @Parameter+TestNG.xml parameters can be placed in suite level and test level. If
The Same parameter name is declared in both places; test level parameter will get preference over suit level parameter.
- using @Parameter+TestNG.xml only one value can be set at a time, but @DataProvider return an 2d array of Object.
- If DataProvider is present in the different class then the class where the test method resides,DataProvider should be static method.
- There are two parameters supported by DataProvider are Method and ITestContext.