Tuesday, 3 July 2018

TestNG Data Provider

When you need to pass complex parameters for example 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 same Search examples using Dataproviders.

How to do it ?

1)  Define the method searchValue() which is defined as a Dataprovider using the annotation. This method returns array of object array.

2) Add a method test() to your DataProviderTest class. This method takes two strings as input parameters.

3) Add the annotation @Test(dataProvider = “search”) to this method. The attribute dataProvider is mapped to “search”.

4) Run the below code as testNG test case then the test will execute two times as we are passing two Data Values.

package test;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class TestNGDataProvider {
 private static WebDriver driver;
  @DataProvider(name = "search")
  public static Object[][] searchValue() {
        return new Object[][] { { "appium" }, { "selenium" }};
  // Here we are calling the Data Provider object with its Name
  @Test(dataProvider = "search")
  public void test(String searchtext) throws Exception {
   System.setProperty("webdriver.chrome.driver", "C:\\Softwares\\chromedriver_win32\\chromedriver.exe");
   driver=new ChromeDriver();


Post a Comment