【问题标题】:TestNG: How to rerun testng suite based on number of rows in Excel fileTestNG:如何根据 Excel 文件中的行数重新运行 testng 套件
【发布时间】:2016-03-19 06:43:14
【问题描述】:

我有一个由 3 个 java 类组成的 Testng 套件,用于自动化移动应用程序的 3 个不同页面:

Class 1(这是关于注册&将数据从excelsheet导入String数组,然后在应用程序的第1页输入数据。AndroidDriver也在这个类中初始化。)

Class 2 扩展了 Class 1(这是关于付款的)

第 3 类(关于审核的最后一页。有一个按钮可以返回第 1 页注册以开始新订单)

现在我要做的是根据 excelsheet 中填充的行数继续运行套件。

执行顺序应该是:

  1. 从 XLS 导入第 1 行 => 1 类 -> 2 类 -> 3 类(如果另一个 行存在)
  2. 从 XLS 导入第 2 行 => 1 类 -> 2 类 -> 第 3 类(如果存在另一行)
  3. 从 XLS => 类导入第 3 行 1 -> Class 2 -> Class 3(结束测试,因为没有更多的行)

我怎样才能做到这一点?我一直在阅读有关 DataProvider 和工厂的信息,但我从未使用过它们,所有其他帖子似乎都表明这个问题只能通过使用它们来解决。我正在使用由 2 类和 3 类继承的单个驱动程序实例。我在测试中合并了 Apache POI 库,以便从 XLSX 文件中读取数据,并将其成功存储在数组中。

更新 这段代码正确吗?

public class WebTestFactory {
  @Factory
  public Object[] createInstances() {
   Object[] result = new Object[number of rows]; 
   for (int m = 0; m < number of rows; m++) {
      result[m] = m;
    }
    return result;
  }
}

【问题讨论】:

    标签: java excel testng test-suite


    【解决方案1】:

    您可以执行以下操作:

    @Factory
    public static Object[] createTests() {
        List<Object> tests = new ArrayList<>();
        Rows rows = // your POI rows
        for (Row row : rows) {
            tests.add(new Class1Test(row));
            tests.add(new Class2Test(row));
            tests.add(new Class3Test(row));
        }
        return tests.toArray();
    }
    

    这将为每一行创建所需的测试类实例并将它们排队以供 TestNG 执行。

    您也可以将工厂与数据提供者一起使用。详情请见http://testng.org/doc/documentation-main.html#factories

    【讨论】:

    • 我已经用代码更新了我的原始帖子。你能检查一下吗?我主要关心的不是传递整行而是传递“行数”。例如,如果 excelsheet 中有 5 行,则 i = 5。
    猜你喜欢
    • 2020-08-30
    • 2019-01-08
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多