selenium - dataProvider 与@Before testng

标签 selenium testing jenkins testng

我正在与 SauceLab 和 Jenkins 一起使用一个 TestNG 脚本。我遇到了一个问题。当我从 Jenkins 运行我的项目时,我将从那里选择浏览器,因此我可以将它与“dataProvider”一起使用,但 dataProvider 仅使用@Test 注释,我想将 dataProvider 与@before 一起使用。

步骤:

  1. @Before 将初始化驱动程序(Webdriver)对象。
  2. @Test 用驱动程序对象执行第一个测试用例。
  3. @Test (2nd) 使用相同的驱动程序对象执行第二个测试用例。
public class test
{
    Webdriver driver;

    // Over here I want to use @Before
    @Test(dataProvider = "dynamicParameters", priority = 0, alwaysRun = true)
    public void init(String browser, String version, String os, Method method) throws Exception
    {
        System.out.println("Init Method");
        String BASE_URL = System.getProperty("baseUrl");
        PCRUtils pcrUtils = new PCRUtils();
        driver = pcrUtils.createDriver(browser, version, os, method.getName());

        driver.get(BASE_URL);
        driver.manage().window().maximize();
        Thread.sleep(50000);
     }

     @Test(priority = 1)
     public void verifyTitle() throws InterruptedException
     {
        AccountPage accountPage = new AccountPage();
        accountPage.verifyTitle(driver);
     }
}

最佳答案

TestNG“@before”方法不能直接与@DataProvider一起使用.

A @BeforeMethod可以访问参数列表(TestNG - 5.18.1 - Native dependency injection):

Any @BeforeMethod can declare a parameter of type Object[]. This parameter will receive the list of parameters that are about to be fed to the upcoming test method, which could be either injected by TestNG, such as java.lang.reflect.Method or come from a @DataProvider

但是 @BeforeMethod“将在每个测试方法之前运行”,而您想要的更像是 @BeforeClass其中“将在调用当前类中的第一个测试方法之前运行”(TestNG - 2 - Annotations)。不幸的是,@BeforeClass 无法像 @BeforeMethod 那样通过 TestNG 的 native 依赖注入(inject)访问参数列表。

A @Factory但是可用于通过 @DataProvider 完成初始的、数据驱动的设置。例如:

public class test
{
    WebDriver driver;

    @Factory(dataProvider = "dynamicParameters")
    public test(String browser, String version, String os, Method method) throws Exception
    {
        System.out.println("Init Method");
        String BASE_URL = System.getProperty("baseUrl");
        PCRUtils pcrUtils = new PCRUtils();
        driver = pcrUtils.createDriver(browser, version, os, method.getName());

        driver.get(BASE_URL);
        driver.manage().window().maximize();
        Thread.sleep(50000);
    }

    @Test(priority = 1)
    public void verifyTitle() throws InterruptedException
    {
        AccountPage accountPage = new AccountPage();
        accountPage.verifyTitle(driver);
    }

    @Test(priority = 2)
    public void verifySomethingElse() throws InterruptedException
    {
        // execute second test case with same driver object.
    }
}

参见 TestNG - 5.8 - Factories了解更多详情。

关于selenium - dataProvider 与@Before testng,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35893294/

相关文章:

python-3.x - 如何从不断刷新网页元素的网页中循环元素

正则表达式 : "Any word" pattern

java - 如何模拟用于测试的数据库(Java)?

java - 测试 JavaFX 多点触控

jenkins - 每天在 8 :30 AM except on Friday 安排 Jenkins 作业

python - Selenium 文件上传 url "path is not absolute"

javascript - Selenium 关于 loadTimeout 的屏幕截图

javascript - Rselenium 无法点击所有单选按钮(仅限其中一些)

deployment - 如何使用 Jenkins 将更新部署到 OpenShift Mono 应用程序?

jenkins - Ansible 是否可以替代 Hudson/Jenkins 等 CI 工具?