When
you need to pass complex parameters or parameters that need to be created from
Java (complex objects, objects read from a property file or a database, etc…),
in such cases parameters can be passed using Dataproviders. A Data Provider is
a method annotated with @DataProvider. A Data Provider returns an array
of objects.
Let us
check out the Login example using Dataproviders.
Steps
to follow:
1)
Define the method credentials() which is defined as a DataProvider using
the annotation. This method returns array of object array.
2) Add
a method testLogin() to your DataProviderTest class. This method takes two
strings as input parameters.
3) Add
the annotation @Test(dataProvider = “Authentication”) to this method.
The attribute dataProvider is mapped to “Authentication”.
Test
will look like this:
package automationFramework;
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.DataProvider;
import
org.testng.annotations.Test;
public class DataProviderTest {
private
static WebDriver driver;
@DataProvider(name = "Authentication")
public static Object[][] credentials() {
return new Object[][] { {
"testuser_1", "Test@123" }, { "testuser_2",
"Test@111" }};
}
// Here we are calling the Data Provider object with its Name
@Test(dataProvider = "Authentication")
public void testLogin(String sUsername, String sPassword) {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.gmail.com");
driver.findElement(By.id("txtUsername")).sendKeys(sUsername);
driver.findElement(By.id("txtPassword")).sendKeys(sPassword);
driver.findElement(By.id("btnLogin")).click();
driver.findElement(By.xpath(".//*[@id='account_logout']/a")).click();
driver.quit();
}
}
Run the test by right click on
the test case script and select Run As > TestNG Test. Give it few
minutes to complete the execution and as the test data is provided two times,
the above test will be executed two times completely.
No comments:
Post a Comment