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

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

问题描述

我目前正在使用 testng + selenium 来自动化我的测试,我有以下场景:

I am currently using testng + selenium to automate my tests, and I have the following scenario:

我需要从 excel 文件中读取数据,转换对象中的每一行并为每个行运行 1 次测试.我正在尝试使用注释@DataProvider 来返回一个对象数组,但是它只能返回迭代器和对象 [][].是否有任何解决方法可用于从 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:客户类:

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[] 是您在 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天全站免登陆