PARAMETERIZATION IN TestNG: TestNG
allows the user to pass values to test methods as arguments by using parameter
annotations through testng.xml file.
Some times
it may be required for us to pass values to test methods during run time. Like
we can pass user name and password through testng.xml instead of hard coding it
in testmethods. or we can pass browser name as parameter to execute in specific
browser.
Let us now
try to understand parameterization with a basic example.
package com.parameterization;
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 for User Name passed as :- " + username);
System.out.println(
"Parameter for Password passed as :- " + password);
}
}
In the above
class, for Test Method 'testCaseOne', we are passing two parameters 'username'
and 'password' as input to test method.
The below is
the testng.xml file, in which we need to pass the parameter values for the test
method
The
below 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.parameterization.TestParameters" />
</classes>
</test>
</suite>
In the above
testng.xml file, we have two attributes for parameter tag, the name attribute
which defines name of the parameter, and the value attribute defines the value
of the parameter.
No comments:
Post a Comment