使用Selenium DataProvider运行多个测试 [英] Run multiple tests with Selenium DataProvider

查看:103
本文介绍了使用Selenium DataProvider运行多个测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用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文件,每一行-一个数据集
  • 对每个数据集进行测试

你能做什么:

  1. 创建@DataProvider,并随数据集一起返回Iterator<Object[]>,其中每个Object []是Excel中的行. (最简单的方法)
  2. 使用@Factory手动遍历数据集并调用测试方法.
  3. 使用@DataProvider@Factory提供数据,然后执行上述操作. 第二个和第三个选项很复杂,但是如果您有除数据集以外的其他参数来运行测试,则第二个和第三个选项会有所好处.
  1. Create @DataProvider which return Iterator<Object[]> with your datasets where each Object[] is your row from excel. (the easiest one)
  2. Use @Factory to manually iterate through your datasets and call test methods.
  3. 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运行多个测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆