This concept is used as we may need to run a single test case with different kind of data. Login page can be tested against various types of data like Blank, invalid, special characters etc.
If there is no data provider concept, then we may need to write many test cases again and again for every kind of data.
Sample example where I have created data, in future will be creating data in excel sheet.
package com.javabykiran;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class DataProviderEx {
@Test(dataProvider="login-data")
public void LoginTestCases(String tcId,String tcDescr,String un- ame,String passwd,String expResult){
System.out.println(tcId+" tcDescr >> "+tcDescr+" uname "+uname+" passwd "+passwd+" expResult "+expResult);
}
@DataProvider(name="login-data")
public Object[][] loginData(){ return new Object[][]{
new Object[]{"TC01","blankUser"," "," ","please enter email" }, new Object[]{"TC02","InvalidUser","hellokiran "," kiran","please enter email"},
new Object[]{"TC03","ValidPassword","kiran@gmail.com "," 123456","Home page title"},};
}
}
When we observe the output window after running this,we will see that our test cases ran three times for different kinds of dataas shown in the above picture:
Data provider method may not be in same class, it can be in different class.
If data coming from different classes, then we need to write that class name here in dataProviderClass attribute in the @test annotation as below:
Sample example where I have created data, in future will be creating data in excel sheet.
@Test(dataProvider = "login-data", dataProviderClass = DataProviderEx.class)
Try doing the below example and perform it for all the 3 test cases and observe every method.
Sample example where I have created data, in future will be creating data in excel sheet.
package com.javabykiran;
import org.testng.annotations.BeforeClass;
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.AfterSuite;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class DataProviderEx {
WebDriver driver; WebElement weUserName;
WebElement wePassword;
WebElement weSignInButton;
WebElement weErrorMessage;
@BeforeSuite
public void setupEnvironMent() {
driver = new FirefoxDriver();
driver.get("http://www.javabykiran.com/selenium/demo");
}
@BeforeClass
public void locateAllWebElements(){
weUserName = driver.findElement(By.xpath("//*[@ id=\"email\"]"));
wePassword = driver.findElement(By.xpath("//*[@ id=\"password\"]"));
weSignInButton = driver.findElement(By.xpath("//*[@id=\"form\"]/div[3]/div/button"));
}
@BeforeMethod
public void clearAllTextBoxes(){
weUserName.clear(); wePassword.clear();
}
@Test(dataProvider = "login-data")
public void LoginTestCases(String tcId, String tcDescr, String uname, String passwd, String expResult) {
// printing all data we got for each test case.
System.out.println(tcId + "tcDescr>>" + tcDescr + " uname " + uname + " passwd " + passwd + " expResult " + expResult);
// we start putting data in text boxes.
weUserName.sendKeys(uname);
wePassword.sendKeys(passwd);
// click on signIn button for getting error message.
weSignInButton.click();
// Now we will start testing error messages.
if(!"validuser".equals(tcDescr)){
weErrorMessage=driver.findElement(By.xpath("//*[@ id=\"email_error\"]"));
String actualMessage=weErrorMessage.getText();
Assert.assertEquals(actualMessage, expResult);
}else{
String actualTitle=driver.getTitle();
Assert.assertEquals(actualTitle, expResult);
}
}
@DataProvider(name = "login-data")
public Object[][] loginData() {
return new Object[][] {
new Object[] { "TC01", "blankUser", "", "", "Please enter email." },
new Object[] { "TC02", "invaliduser", "hellokiran", "kiranpassword", "Please enter email as kiran@gmail.com" },
new Object[] { "TC03", "validuser", "kiran@gmail.com", "123456", "AdminLTE 2 | Dashboard" }, };
}
@AfterSuite
public void closeBrowser(){
driver.close();
}
}
Above program remains same, the only difference is that parameters will be sent through xml.
Below given is the “testng.xml” file in which we need to pass the parameter values for the test method.
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parameterization Test Suite">
<test name="Testing Parameterization">
<parameter name="browser" value="Firefox"/>
<parameter name="username" value="testuser"/>
<parameter name="password" value="testpassword"/>
<classes>
<class name="com.javabykiran.TestParameters" />
</classes>
</test>
</suite>
package com.javabykiran;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestParameters {
@Parameters({ "browser" })
@Test
public void testCaseOne(String browser) {
System.out.println("browser passed as :- " + browser);
}
@Parameters({ "username", "password" })
@Test
public void testCaseTwo(String username, String password) {
System.out.println("Parameter User Name as:-"+username);
System.out.println("Parameter Password as :- " + password);
}
}
We can try below example as well where we sent parameters to different groups.
In the following code example:
We have 2 groups A & B
Each test method is assigned to a group
If the value of group is A, a particular data set is returned
If the value of group is B, another data set is returned
package com.javabykiran;
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 {
public static WebDriver driver=null;
@BeforeTest(groups = { "A", "B" })
public void setup() {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("http://javabykiran.com/javabykiranfiles/");
}
@Test(dataProvider = "EnterEmail", groups = "A",priority=1)
public void testMethodA(String author, String email) throws InterruptedException{
WebElement emailField = driver.findElement(By.id("log- in_form_user"));
// send value in email field
emailField.sendKeys(email);
System.out.println("Welcome ->"+author+"Your emailAd dress is->"+email);
Thread.sleep(3000);
String testValue = emailField.getAttribute("value");
System.out.println(testValue + "::::"+ email);
emailField.clear();
// verify correct value in email field
Assert.assertTrue(testValue.equalsIgnoreCase(email));
}
@Test(dataProvider="EnterEmail",groups="B",priority=2)
public void testMethodB(String email) throws InterruptedException{
//find email field
WebElement emailField = driver.findElement(By.id("log- in_form_user"));
//send value in email field
emailField.sendKeys(email);
System.out.println("Welcome ->Your emailAddress is->"+email);
Thread.sleep(3000);
String testValue = emailField.getAttribute("value");
System.out.println(testValue +"::::"+email);
emailField.clear();
//verify correct value in email field
Assert.assertTrue(testValue.equalsIgnoreCase(email));
///Here the DataProvider will provide Object array on the basis on ITestContext
//@param c
//@return
}
@DataProvider(name="EnterEmail")
public Object[][] getDataFromDataprovider(ITestContext c){
Object[][] groupArray=new Object[0][0];
for(String group : c.getIncludedGroups()){
if(group.equalsIgnoreCase("A"))
{
groupArray = new Object[][]
{
{ "JBK", "aa@aa.com" },
{ "JBK1", "bb@bb.com" },
{ "JBK2", "cc@cc.com" }
};
break;
}
else if(group.equalsIgnoreCase("B"))
{
groupArray = new Object[][]
{
{ "dd@dd.com" },
{ "ee@ee.com" },
{ "ff@ff.com" }
};
}
break;
}
return groupArray;
}
}
Below is the xml file format:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="group A Test">
<groups>
<run>
<include name="A"/>
</run>
</groups>
<classes>
<class name="com.javabykiran.ParameterByITestContextIn- Dataprovider"/>
</classes>
</test> <!-- Test -->
<test name="group B Test">
<groups>
<run>
<include name="B"/>
</run>
</groups>
<classes>
<class name="com.javabykiran.ParameterByITestContextIn- Dataprovider"/>
</classes>
</test>
<!-- Test -->
</suite> <!-- Suite -->
Parameterization is required 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 2 dimensional array of Object.
If DataProvider is present in the different class then the class where the test method resides needs to provide the name of the class where DataProvider is mentioned.
There are two parameters supported by DataProvider are Method and ITestContext see in above program we have method getDataFromDataprovider,observe parameters.