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"},           
          };
  }
 
  @BeforeMethod
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://www.wikipedia.org/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test(dataProvider = "myTest")
  public void testSearchCountry(String country) throws Exception {
    driver.get(baseUrl + "/wiki/Main_Page");
    driver.findElement(By.id("searchInput")).clear();
    driver.findElement(By.id("searchInput")).sendKeys(country);
    driver.findElement(By.id("searchButton")).click();
    String str = driver.findElement(By.cssSelector("span")).getText();
    Assert.assertTrue(country.equals(str.trim()));
  }

  @AfterMethod
  public void tearDown() throws Exception {
    driver.quit();   
  } 
}

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.

13 comments:

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

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

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

    http://roadtoautomation.blogspot.in/2013/06/road-to-data-driven-testing-in.html

    http://roadtoautomation.blogspot.in/2013/06/road-to-data-driven-testing-in_17.html?showComment=1376581810632#c1247775426101337156

    ReplyDelete
  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?

    ReplyDelete
  5. santhipazhanivel13 March 2014 at 11:31

    Thank you, good post.

    ReplyDelete
  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

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

    ReplyDelete
    Replies
    1. Use BeforeSuite or BeforeClass TestNG annotation

      Delete
    2. Also use corresponding AfterSuite or AfterClass Test annotation

      Delete
  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.

    Cheers

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

    ReplyDelete
  10. Useful post, Doubt cleared here. Bookmarked

    ReplyDelete

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