使用 dataprovider 执行并行 TestNG selenium 测试的驱动程序行为 [英] Driver behavior executing parallel TestNG selenium tests with dataprovider

查看:30
本文介绍了使用 dataprovider 执行并行 TestNG selenium 测试的驱动程序行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 TestNg 中使用 @dataprovider 并行运行 selenium 测试.理想情况下,测试是按方法并行的(一个测试=一种方法),而不是浏览器的简单套件并行性.我在某处读到一次可以控制大约 5 个 ChromeDriver 实例,所以我认为这应该是可能的.后来我打算搬到grid2.对于开发,我正在使用 IntelliJ Idea 测试运行程序通过右键单击 + 在 XML 配置文件上运行来运行.

I want to run selenium tests in TestNg in parallel that use the @dataprovider. Ideally tests are parallel by method (one test = one method) and not simple suite parallelism by browser. I have read somewhere that about 5 instances of ChromeDriver can be controlled at a time so I thought this should be possible. Later I plan to move to grid2. For developement I'm running things with IntelliJ Idea test runner by right-click + run on the XML config file.

我在并行运行测试时遇到了问题(在 grid2 和本地),所以我创建了一个或多或少我想做的样本.

I had problems running my tests in parallel (on grid2 and locally) so I created a sample of more or less what I want to do.

这是我的测试课

package tests;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.*;
import java.util.concurrent.TimeUnit;
import static org.testng.Assert.assertNotNull;

public class ParallelTest {
    public static final String SEARCH_TERMS = "search-terms";
    private WebDriver driver;

    @BeforeMethod
    @Parameters({"browser"})
    public void beforeMethod(@Optional("chrome") String browser){
        driver = getBrowser(browser);
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    private WebDriver getBrowser(String browser) {
        if(browser.equals("chrome")){
            System.setProperty("webdriver.chrome.driver", "webdrivers\chromedriver.exe");
            return new ChromeDriver();
        }
        return new FirefoxDriver();
    }

    @AfterMethod
    public void afterMethod(){
        driver.quit();
    }

    @Test(description = "Check parallel selenium works.",
          dataProvider = SEARCH_TERMS)
    public void parallelSeleniumTest(String searchTerm){
        driver.get("http://google.com");
        WebElement search = driver.findElement(By.id("gbqfq"));
        new Actions(driver)
                .sendKeys(search, searchTerm)
                .sendKeys(search, Keys.ENTER)
                .perform();
        String firstResult = driver.findElements(By.className("r")).get(0).getText();
        assertNotNull(firstResult);
        System.out.println(firstResult);
    }

    @DataProvider(name = SEARCH_TERMS, parallel = true)
    public Object[][] getSearchTerms(){
        return new Object[][]{
                {"google"},
                {"microsoft"},
                {"facebook"},
                {"amazon"},
                {"apple"},
                {"oracle"},
                {"yahoo"},
                {"jetbrains"},
                {"intellij idea"},
                {"selenium"},
                {"java"},
                {"testng"},
                {"code"}
        };
    }
}

由于我在测试套件中大量使用它们,因此我加入了一些原生事件.

I threw in some native events since I use them heavily in my test suite.

这里是 TestNg xml 配置文件

And here is the TestNg xml config file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="4" name="vfr6-ui-tests" parallel="methods">
    <test name="parallel-test-firefox">
        <parameter name="browser" value="firefox"/>
        <classes>
            <class name="tests.ParallelTest"/>
        </classes>
    </test>
    <test name="parallel-test-chrome">
        <parameter name="browser" value="chrome"/>
        <classes>
            <class name="tests.ParallelTest"/>
        </classes>
    </test>
</suite>

我读到每个测试实例化一个驱动程序往往是最易于维护的.问题是 Firefox 测试串行运行,而 chrome 测试将所有数据点作为测试用例吐出,尝试打开大量浏览器实例,然后一切都失败了.我的测试将有 10-25 或 300-500 个数据点(在客户端或客户端 x 产品之间循环).

I read instantiating one driver per test tends to be the most maintainable. The problem is that the firefox test runs in serial while the chrome test spits out all of the data points as test cases, attempts to open a trove of browser instances, then everything fails. My tests will have either 10-25 or 300-500 data points (cycling between either clients or clients x products).

设置驱动程序、数据提供程序和测试运行程序以在运行测试中实现最佳并行度的最佳方法是什么?

What is the best way to set up the driver, dataprovider, and test runner to achieve the best parallelism in running tests?

推荐答案

我对dataProvider也有同样的经历.就我而言,我使用了 dataProvider 的 (parallel=true) 属性.您的问题有两种解决方案.

I had the same experience about dataProvider. In my case I used dataProvider's (parallel=true) attribute though. There are two solutions to your problem.

  1. 在测试类中使用 dataProvider 并为您的构造函数使用工厂注解.在工厂注解的属性中,使用 dataProvider="Your dataProvider's name".在 testng.xml 中,使用 parallel=instances 代替 parallel=methods.

  1. Use dataProvider and in test class and use factory annotation for your constructor. In the factory annotation's attribute, use dataProvider="Your dataProvider's name". In the testng.xml, instead of parallel=methods, use parallel=instances.

上述方法的缺点是当你得到报告时;可能是 maven-surefire,testng Eclipse 报告或 reportNG 报告,你看不到参数传递了正面.为了克服这个问题,您可以使用以下方法.

The drawback of the above approach is that when you get the report; may be it is maven- surefire, testng Eclipse report or reportNG report, you do not see parameters passed up front. To overcome this, you can use the following approach.

创建一个工厂类并在工厂方法中使用for 循环.(从 0 开始 for 循环.)在测试类中定义一个构造函数,它从工厂类接收参数.在这个测试类中定义一个 dataProvider可以使用构造函数中接收到的参数(数据点).定义一个 BeforeMethod或 BeforeClass 可以使用该参数或数据点,您的测试方法应该具有指向所需数据提供者的dataProvider"属性.再次,在testng.xml 使用 parallel="instances".

Create a factory class and instantiate your test class in the factory method using a for loop. (Start for loop from 0.) In the test class define a constructor which receives a parameter from factory class. Define a dataProvider in this test class which can use the parameter (data-point) received in the constructor. Define a BeforeMethod or BeforeClass which can use that parameter or data point and Your test methods should have the "dataProvider" attribute pointing to the desired dataProvider. Again, in testng.xml use parallel="instances".

此外,使用 try/catch 块来实例化驱动程序对象并关闭浏览器.这将帮助您避免由于 tearDown 方法的设置失败而导致的跳过.

Also, use try/catch block for instantiating driver object and closing the browser. This will help you in avoiding skips due to failure of setUp of tearDown method.

这篇关于使用 dataprovider 执行并行 TestNG selenium 测试的驱动程序行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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