问题描述
我目前正在使用testng +硒来自动化测试,并且出现以下情况:
I am currently using testng + selenium to automate my tests, and I have the following scenario:
我需要读取一个Excel文件,转换对象中的每一行,并为它们中的每一项运行1个测试.我正在尝试使用注释@DataProvider返回对象数组,但是它只能返回Iterators和Objects [] [].有什么解决方法可以用来从DataProvider返回Cliente对象的数组?我尝试了以下代码,但是它仅打印Client2中的数据:
I need to read from an excel file, transform each row in an object and run 1 test for each of them. I am trying to use annotation @DataProvider to return an Array of objects, however it is only able to return Iterators and Objects[][]. Is there any workaround I can use to return an array of Cliente objects from the DataProvider? I have tried the following code, however it only prints the data from Client2:
public class TestDataProvider
{
Cliente cliente;
@DataProvider(name = "test1")
public static Object[][] dataMethod() {
return new Object[][] { { new Cliente("Client1", "1111111111") },
{ new Cliente("Client2", "2222222222") }};
}
@Test(dataProvider = "test1")
public void testMethod(Cliente cliente) {
System.out.println(cliente.getNome() + " " + cliente.getCartao());
}
}
Edit1:客户端类:
Cliente class:
public class Cliente {
private static String name;
private static String card;
//Construtor method
public Cliente(String name, String card){
setname(name);
setCartao(card);
}
public String getName() {
return name;
}
public void setName(String name) {
Cliente.name = name;
}
public String getCard() {
return card;
}
public void setCard(String card) {
Cliente.card = card;
}
}
在控制台中打印的值:
Client2 2222222222
Client2 2222222222
推荐答案
所以...
您的先决条件:
- excel文件,每一行-一个数据集
- 对每个数据集进行测试
你能做什么:
- 创建
@DataProvider
,并随数据集一起返回Iterator<Object[]>
,其中每个Object []是Excel中的行. (最简单的方法) - 使用
@Factory
手动遍历数据集并调用测试方法. - 使用
@DataProvider
为@Factory
提供数据,然后执行上述操作.第二个和第三个选项很复杂,但是如果您有除数据集以外的其他参数来运行测试,则第二个和第三个选项会有所好处.
- Create
@DataProvider
which returnIterator<Object[]>
with your datasets where each Object[] is your row from excel. (the easiest one) - Use
@Factory
to manually iterate through your datasets and call test methods. - Use
@DataProvider
to provide data for@Factory
and do as above.The 2nd and 3rd options are complicated, but have some benefits if you has other parameters, except datasets, to run tests.
这篇关于使用Selenium DataProvider运行多个测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!