Dataprovider Annotation in TestNG

In this section let’s understand what are the Different ways of Passing the Parameters in TestNG and what are DataProviders in TestNG.

There are Two Ways to Pass Parameters in TestNG

1. Parameter annotation.

Using the XML file, They can be used inside or outside of the tag. The parameters are applied inside the tag if we wish to apply them to all of the test cases.

you can refer tutorial on how to pass Parameters using Parameter annotation.

2. DataProvider annotation.

DataProvider in TestNG is another way to Pass Parameters in Testcase Function.DataProvider is Declared or Denoted by @DataProviderAnnotation.

Test Case

  • Create DataProviderExample Class
  • Search Element of Search bar on Google
  • Pass Element value which Needs to be searched on google
  • Pass  “CodeBun”, “what is the weather is like today“, and “what is the current time in India” Parameters in the Search Bar of Google.com From the Dataprovider.

Code

package DataPro;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DataProviderExample {
    @DataProvider (name = "data-provider")
    public Object[][] DataMethod(){
     return new Object[][] {{"q","codebun"},{"q","what is the weather is like today"},{"q","what is the current time in india"}};
    }
    
   @Test (dataProvider = "data-provider")
   public void myTest (String Element,String Value) throws InterruptedException {
       System.setProperty("webdriver.chrome.driver", "E:/CodeBun/project1/chromedriver.exe");//initialize Chrome Web Driver
        WebDriver driverChrome = new ChromeDriver();// Create Instance for Chrome Web Driver
        driverChrome.manage().window().maximize();// Maximize Window for Chrome Instance
        driverChrome.get("https://www.google.com");//Url Request Form Chrome Driver
        WebElement Search = driverChrome.findElement(By.name(Element)); //Search element on google.com for which the parameter value is passed from DataProvider.
        Search.sendKeys(Value);//pass value parameter From DataProvider annotation.
        Thread.sleep(5000);//Wait 5 Seconds to Show Output
        driverChrome.quit();// this would close all the window of web driver
   }
}

@DataProvider (name = "data-provider")Define DataProvider annotation and assign a name to it so it can be called during execution.in Our case, we have set the name as “data-provider”.

public Object[][] DataMethod(){ return new Object[][] {{"q","codebun"},{"q","what is the weather is like today"},{"q","what is the current time in india"}};

In the Above Example, we Have Created a 2-Dimensional Array with Three Sets of Parameters and two inputs in each set.

{"q","codebun"} in this “q “is the Element Name to be searched on Google and the second parameter is the value that will be passed into the search bar of Google.

WebElement Search = driverChrome.findElement(By.name(Element));This is Used for Finding the Element on Google.com and Element Parameter is Passed using DataProvider Annotation.

Search.sendKeys(Value);it is Used for Sending the value to Web Element and The parameter is passed using DataProvider Annotation.

Thread.sleep(5000);Pause Execution For 5 Seconds to Show Output and Before Quitting the WebDriver.

driverChrome.quit();This Would Close all the Windows Open by Web Driver.

Output

In Above Program all Three Sets of parameters are Passed From DataProvider to Testcase.

We Have Successfully Demonstrated the Use of DataProvider Annotation to Pass Parameter to Testcase In TestNG.