TestNG Parameters and DataProvider
2016-04-18 15:46
471 查看
TestNG Parameters
Everybody knows the importance of Parameterization in testing and in automation testing. It allows us to automatically run a test case multiple times with different input and validation values. As Selenium Webdriver is more anautomated testing framework than a ready-to-use tool, you will have to put in some effort to support data driven testing in your automated tests. I usually prefer to use Microsoft Excel as the format for storing my parameters but so many of my followers have
requested to write an article on TestNG Data Provider.
TestNG again gives us another interesting feature called TestNG Parameters. TestNG lets you pass parameters directly to your test methods with your testng.xml.
How to do it…
Let me take a very simple example of LogIn application, where the username and password is required to clear the authentication.
1) Create a test on my demo
OnlineStore application to perform LogIn which takes the two string argument as username & password.
2) Provide Username & Password as parameter using TestNG Annotation.
package automationFramework;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.annotations.Parameters;
public class TestngParameters {
private static WebDriver driver;
@Test
@Parameters({ "sUsername", "sPassword" })
public void test(String sUsername, String sPassword) {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.store.demoqa.com");
driver.findElement(By.xpath(".//*[@id='account']/a")).click();
driver.findElement(By.id("log")).sendKeys(sUsername);
driver.findElement(By.id("pwd")).sendKeys(sPassword);
driver.findElement(By.id("login")).click();
driver.findElement(By.xpath(".//*[@id='account_logout']/a")).click();
driver.quit();
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | packageautomationFramework; importjava.util.concurrent.TimeUnit; importorg.openqa.selenium.By; importorg.openqa.selenium.WebDriver; importorg.openqa.selenium.firefox.FirefoxDriver; importorg.testng.annotations.Test; importorg.testng.annotations.Parameters; publicclass TestngParameters{ privatestatic WebDriverdriver; @Test @Parameters({"sUsername","sPassword" }) publicvoid test(StringsUsername,String sPassword){ driver= newFirefoxDriver(); driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS); driver.get("http://www.store.demoqa.com"); driver.findElement(By.xpath(".//*[@id='account']/a")).click(); driver.findElement(By.id("log")).sendKeys(sUsername); driver.findElement(By.id("pwd")).sendKeys(sPassword); driver.findElement(By.id("login")).click(); driver.findElement(By.xpath(".//*[@id='account_logout']/a")).click(); driver.quit(); } } |
<suite name="Suite">
<test name="ToolsQA">
<parameter name="sUsername" value="testuser_1"/>
<parameter name="sPassword" value="Test@123"/>
<classes>
<class name="automationFramework.TestngParameters" />
</classes>
</test>
</suite>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <suitename="Suite"> <testname="ToolsQA"> <parametername="sUsername"value="testuser_1"/> <parametername="sPassword"value="Test@123"/> <classes> <classname="automationFramework.TestngParameters" /> </classes> </test> </suite> |
TestNG DataProviders
When you need to pass complex parameters or parameters that need to be created from Java (complex objects, objects read from a property file or a database, etc…), in such cases parameters can be passed using Dataproviders. A Data Provider is a method annotatedwith @DataProvider. A Data Provider returns an array of objects.
Let us check out the same Sign In examples using Dataproviders.
How to do it…
1) Define the method credentials() which is defined as a Dataprovider using the annotation. This method returns array of object array.
2) Add a method test() to your DataProviderTest class. This method takes two strings as input parameters.
3) Add the annotation @Test(dataProvider = “Authentication”) to this method. The attribute dataProvider is mapped to “Authentication”.
Test will look like this:
package automationFramework; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class DataProviderTest { <span style="white-space:pre"> </span>private static WebDriver driver; @DataProvider(name = "Authentication") public static Object[][] credentials() { 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.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://www.store.demoqa.com"); driver.findElement(By.xpath(".//*[@id='account']/a")).click(); driver.findElement(By.id("log")).sendKeys(sUsername); driver.findElement(By.id("pwd")).sendKeys(sPassword); driver.findElement(By.id("login")).click(); driver.findElement(By.xpath(".//*[@id='account_logout']/a")).click(); driver.quit(); } }
Run the test by right click on the test case script and select Run As >TestNG Test. Give it few minutes to complete the execution, once it is finished the results will look like this in theTestNg Result
window.
Note: As the test data is provided two times, the above test executed two times completely.
相关文章推荐
- linux命令详解——top
- linux命令详解——top
- 分享一下学习css,js心得
- LeftBarbuttonItem左滑返回&UINavigationBar透明
- 各种文字特效
- 欢迎使用CSDN-markdown编辑器
- iOS 面试题小总结
- 排序专题
- opencv for android 教程(环境搭建篇)
- Android Studio你不知道的调试技巧
- 50个最常用的UNIX/Linux命令
- Java---16---多线程---死锁
- C# 学习笔记一 参数数组
- linux下tomcat内存溢出
- js定时器避免闭包
- theano tutotial(二)
- EventBus使用
- 程序员需谨记的8条团队开发原则
- Android Studio 入门
- JAVA多线程计算ExecutorService