TestNG provides a way to get the data from external sources like from excel/textfile/arrays which helps to prepare data driven framework in automation.
@Parameters - pass parameters from the testNG.xml
@dataProvider - Pass the parameters as a 2D array.
Let's see the code implementation
@Parameters
-------------------------------------------------------
and the testng.xml looks like:
the @Test method will run number of times depends on the number of data sets provided in the object array.
@Parameters - pass parameters from the testNG.xml
@dataProvider - Pass the parameters as a 2D array.
Let's see the code implementation
@Parameters
-------------------------------------------------------
package annotationpkg;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class parameters {
@Test
//@Parameters("url")
@Parameters({"url", "userName", "password"})
public void parameterTest(String url, String userName, String password) {
System.out.println("url under test : " + url);
System.out.println("userName used : " + userName);
System.out.println("password used : " + password);
}
}
and the testng.xml looks like:
xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="MySuite" parallel="none">
<test name="MyTest1">
<parameter name="url" value="www.mylogin.com"/>
<parameter name="userName" value="user1234"/>
<parameter name="password" value="12345"/>
<classes>
<class name="annotationpkg.parameters"/>
</classes>
</test>
</suite>
---------------------------------------------------------------
so we have 3 parameters to pass to our method, so we have given 3 parameter tags in testNG.xml,
even we can send any type of data type as parameter value like integer, character, string, boolean etc.
@dataProvider
This is another way of passing 2 dimensional array as parameter from a method of same or from different class, let's see one by one.
the @Test method will run number of times depends on the number of data sets provided in the object array.
-----------------From same class------------------
package annotationpkg;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class dataprovider {
@DataProvider(name = "data-provider")
public Object[][] dataProviderArr() {
//Create 2 diemensional array
//can even take the data from an excel sheet or from a Map
//and put it into this array variable
String object[][] = { { "user1", "1234" }, { "user2", "5678" } };
return object;
}
@Test(dataProvider = "data-provider")
public void fn_Credentials(String userName, String password) {
System.out.print("UserName is: " + userName);
System.out.print(" | ");
System.out.println("Password is: "+ password);
}
}
Let's say we have the public Object[][] dataProviderArr() method is defined in another class
(dataProviderArray.class), then we can write:
-------------------From another class-----------------------------------
public class dataProviderArray{
@DataProvider(name = "data-provider")
public Object[][] dataProviderArr() {
//Create 2 diemensional array
//can even take the data from an excel sheet or from a Map
//and put it into this array variable
Object[][] obj= { { "user1", "1234" }, { "user2", "5678" } };
return obj;
}
------------------------------------------------------------------------------------------
package annotationpkg;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class dataprovider {
@Test(dataProvider = "data-provider", dataProviderClass = dataProviderArray.class)
public void fn_Credentials(String userName, String password) {
System.out.print("UserName is: " + userName);
System.out.print(" | ");
System.out.println("Password is: "+ password);
}
}
------------------------------------------------------------
The TestNG.xml looks like (for from same and another class)
<xml version="1.0" encoding="utf-8"?>
<Suite name="MySuite" parallel="none">
<test name="Test1">
<CLasses>
<class name="annotationpkg.dataprovider"/>
</classes>
</test>
</suite>
No comments:
Post a Comment