如何使用 DataProvider 从多个 Excel 工作表中读取值并将其传递给多个 @test? [英] How to read values from multiple excel sheets using DataProvider and pass it to multiple @test?

查看:32
本文介绍了如何使用 DataProvider 从多个 Excel 工作表中读取值并将其传递给多个 @test?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Selenium/Java/TestNG 自动化一个 Web 应用程序.该应用程序在主页中有数据,并且正在被@dataProvider 从 Excel 表中读取.根据主页面中的一些条件,我想从同一个excel文件中的其他工作表中获取数据,并将其传递给同一个类中的相应@test.尝试了很多选择,但找不到合适的解决方案.

I am automating a web application with Selenium/Java/TestNG. The application has data in the main page and is being read from excel sheet by @dataProvider. Based on some criteria in the main page,I want to get data from other sheets in the same excel file and pass it into corresponding @test in the same class. Tried many options but not able to find a proper solution.

非常感谢,刷新

推荐答案

这是实现此目的的一种方法.

Here's one way in which this can be done.

您基本上将 Excel 电子表格的工作表"名称注入当前 <test> 标签的上下文,即 ITestContext 作为属性,然后从@DataProvider 注释数据提供者,您基本上是通过读取此属性来决定必须读取哪个工作表.

You basically inject the "sheet" name of your excel spreadsheet into the current <test> tag's context viz., ITestContext as an attribute and then from within the @DataProvider annotated data provider, you basically read this attribute to decide which sheet has to be read.

下面的示例演示了执行此操作的两个 @Test 方法,其中第一个 @Test 方法注入此属性作为其执行流程的一部分,第二个 @Test 方法(它必须依赖于第一个),现在由 动态 数据提供者提供支持,它只使用数据.

The below sample demonstrates two @Test methods doing this, wherein the first @Test method injects this attribute as part of it doing a flow and the second @Test method (it would have to depend on the first one) which is now powered by a dynamic data provider just consumes the data.

import org.testng.ITestContext;
import org.testng.Reporter;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class SampleTestClass {
    /**
     * This method is simulating the master test method which would interact with
     * the web app and bring the application to a state wherein the next test method
     * should take over and interact.
     */
    @Test
    public void testLogin() {
        //Simulating toggling between multiple flows.
        int whichFlow = Integer.parseInt(System.getProperty("flow", "1"));
        Reporter.getCurrentTestResult().getTestContext().setAttribute("flow", whichFlow);
    }

    /**
     * This method is intentionally dependent on "testLogin" because its "testLogin" that will
     * first interact with the web application and bring the application to the place from where
     * further interaction is required, but which will vary based on some "x" criteria
     * The "x" criteria is now available as an attribute in the current &lt;test&gt; tag's
     * context.
     */
    @Test(dependsOnMethods = "testLogin", dataProvider = "getData")
    public void testSomethingElse(int a, String b) {
        //Real test method logic goes here.
        System.out.println(a + ":" + b);

    }

    @DataProvider
    public Object[][] getData(ITestContext context) {
        int whichFlow = Integer.parseInt(context.getAttribute("flow").toString());
        switch (whichFlow) {
            case 1:
                return new Object[][]{
                        {1, "Login"},
                        {2, "Signup"}
                };
            case 2:
                return new Object[][]{
                        {100, "Fees"},
                        {200, "Charges"}
                };

            case 3:
            default:
                return new Object[][]{
                        {900, "Logout"},
                        {1000, "Random"}
                };

        }
    }
}

这篇关于如何使用 DataProvider 从多个 Excel 工作表中读取值并将其传递给多个 @test?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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