Data driven testing using java webdriver TestNg framework

In this post I implemented that how to perform data driven testing using java Webdriver  TestNg framework. As we know testNg provide @DataProvider annotation, using this I can pass a set of data into our Webdriver test method.  I have created a sample test script for Wikipedia where I use a set of country data, with Wikipedia search functionality and verify searched country.

package com.test;

import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DataDrivenTest {

  private WebDriver driver;
  private String baseUrl;
  @DataProvider(name = "myTest")
  public Object[][] createData1() {
          return new Object[][] {
            { "India"},
            { "Brazil"},
            { "Canada"},
            { "Sri Lanka"},
            { "England"},
            { "UK"},
            { "United States"},           
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

  @Test(dataProvider = "myTest")
  public void testSearchCountry(String country) throws Exception {
    driver.get(baseUrl + "/wiki/Main_Page");
    String str = driver.findElement(By.cssSelector("span")).getText();

  public void tearDown() throws Exception {

Above test script executed seven times for each set of data used in @DataProvider annotation. Any failed test does not impact other set of execution.

After execution of above test script TestNg report should be generated as below:
Below are the execution log of above test:

PASSED: testSearchCountry("India")
PASSED: testSearchCountry("Brazil")
PASSED: testSearchCountry("Canada")
PASSED: testSearchCountry("Sri Lanka")
PASSED: testSearchCountry("England")
PASSED: testSearchCountry("United States")
FAILED: testSearchCountry("UK")

Above test failed for the value “UK” as searched result is “United Kingdom” not “UK”

Hope above test will help you for data driven testing. Comment here if you have any query.


  1. Good post of TestNg data driven......Can we perform similar way is WebDriver with Ruby

  2. how to do same using XL sheet rather providing data in the script

  3. Go through below link one by one for XL file.

    1. Thank you very much...

  4. how can i implement this with login page.. there we have to provide username and password too na. wat r the changes we hav to do?

  5. santhipazhanivel13 March 2014 at 11:31

    Thank you, good post.

  6. Nice poѕt. I learn something new and challenging on sites I stumbleupon every Ԁay.

    It's always interesting to read through сontent frօm other writers and pгactice
    a little somеthing from theiг web sites.

    My webpage - firma budowlana

  7. Instead of launching 7 times and provide each criteria, how to make it stay in the page and provide different search.. ?

    1. Use BeforeSuite or BeforeClass TestNG annotation

    2. Also use corresponding AfterSuite or AfterClass Test annotation

  8. My testng results for a Data Driven test shows all executed tests with same name. ie. if i have one method running through 10 set of data, i see all 10 executed tests showing with same Test Case Name.

    Is there a way i can allocate unique meaningful names to each of the test case that show up in the report.
    Let me know in case my question is not clear.


  9. Hi, If I want to run 10 methods sequentially and repeat that for 5 times.. how should I do?

  10. Useful post, Doubt cleared here. Bookmarked


Leave your comments, queries, suggestion I will try to provide solution