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.
After execution of above test script TestNg report should be generated as below:
Above test failed for the value “UK” as searched result is “United Kingdom” not “UK”
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")
Hope above test will help you for data driven testing. Comment here if you have any query.
Good post of TestNg data driven......Can we perform similar way is WebDriver with Ruby
ReplyDeletehow to do same using XL sheet rather providing data in the script
ReplyDeleteGo through below link one by one for XL file.
ReplyDeletehttp://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
Thank you very much...
Deletehow 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?
ReplyDeleteThank you, good post.
ReplyDeleteNice poѕt. I learn something new and challenging on sites I stumbleupon every Ԁay.
ReplyDeleteIt'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
Instead of launching 7 times and provide each criteria, how to make it stay in the page and provide different search.. ?
ReplyDeleteUse BeforeSuite or BeforeClass TestNG annotation
DeleteAlso use corresponding AfterSuite or AfterClass Test annotation
DeleteMy 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.
ReplyDeleteIs 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
Hi, If I want to run 10 methods sequentially and repeat that for 5 times.. how should I do?
ReplyDeleteUseful post, Doubt cleared here. Bookmarked
ReplyDelete