Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

1 like 0 dislike
404 views
by The go-to Tester (344 points)
What is the purpose of @Dataprovider in testNG and how can we use it in our tests

1 Answer

1 like 0 dislike
by The go-to Tester (473 points)
selected by
 
Best answer

When we have multiple data for the same field for differnet different scenario then instead of writing same script for the same field multiple time, just write the single java  method and then pass different different set of data on diferent different scenario.

Here we create object of data provider and save multiple data of same type in array of objects. And then we call it (data) on the method in which we need to use this data using parameters.

 
by The go-to Tester (473 points)
Ex. @DataProvider(name = "Authentication")

  public static Object[][] credentials() {

        // The number of times data is repeated, test will be executed the same no. of times
        // Here it will execute two times
        return new Object[][] { { "testuser_1", "Test@123" }, { "testuser_1", "Test@123" }};

  }
// Here we are calling the Data Provider object with its Name
  @Test(dataProvider = "Authentication")
  public void test(String sUsername, String sPassword) {
      driver = new FirefoxDriver();
driver.get("http://www.store.demoqa.com");
driver.findElement(By.id("log")).sendKeys(sUsername);
      driver.findElement(By.id("pwd")).sendKeys(sPassword);
}


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

...