TestNG Parameters and DataProvider
来源:互联网 发布:vr怎么看片 知乎 编辑:程序博客网 时间:2024/06/10 00:08
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 an automated 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.
3) The parameter would be passed values from testng.xml which we will see in the next step.
Now, run the testng.xml, which will run the parameterTest method. TestNG will try to find a parameter named sUsername & sPassword.
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 annotated with @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 Resultwindow.
Note: As the test data is provided two times, the above test executed two times completely.
- TestNG Parameters and DataProvider
- TestNG同时使用DataProvider和Parameters
- TestNG - @Parameters 传递外部参数 对比 @Dataprovider
- testNG的常用用法Parameters,DataProvider读取excel
- TestNG dataProvider
- TestNG-DataProvider
- TestNG中的DataProvider
- 学习:TestNg@DataProvider
- TestNG DataProvider 使用Demo
- multi dataprovider testng
- testng浅析-dataProvider
- testng @dataprovider使用方法
- TestNG DataProvider的使用
- TestNG中DataProvider的用法
- TestNG中的数据源DataProvider概述
- TestNG参数化之DataProvider
- TestNG中DataProvider的用法
- TestNG入门——@DataProvider
- Jmeter--google plugin插件监控被测系统资源方法
- oc基础的纪录
- Android实战技巧:ViewStub的应用
- linux下tomcat内存溢出
- C# 学习笔记一 参数数组
- TestNG Parameters and DataProvider
- opencv for android 教程(环境搭建篇)
- 排序专题
- 欢迎使用CSDN-markdown编辑器
- 一个自定义服务以执行脚本实现类属性的快速整齐地排列
- Redis及其应用
- MySQL存储过程
- android studio百度地图无法定位问题4.9E324
- 图形脚本语言sikuli