简体   繁体   English

无法通过来自数据提供商的 TestNg 运行并行测试

[英]Unable to run parallel test via TestNg from DATA provider

Below is my XML piece.下面是我的 XML 片。

<?xml version="1.0" encoding="UTF-8"?>
<suite name='Automation' threadCount="5" parallel="methods">
    <tests>
        <parameter name='clientName' value='Five' />    
         <test name='PA'>

            <classes>
                <class name='TC_INC_1'>
                </class>
            </classes>
        </test> 

So I am loading the required data from excel through DATA PROVIDER in TestNg.因此,我通过 TestNg 中的 DATA PROVIDER 从 excel 加载所需的数据。 What I wanted to achieve is to run each row in different threads.我想要实现的是在不同的线程中运行每一行。 Lets say I had 5 rows of data假设我有 5 行数据

1- Go to Google.com
2- Go to Facebook.com
3- Go to Dollarama.com
4- Go to Walmart.com
5- Go to KegSteak.com

And say I am running two thread means two browsers.并说我正在运行两个线程意味着两个浏览器。 I want both browsers run parallelly executing each of the row in any sequence.我希望两个浏览器以任何顺序并行运行每一行。

Thread 1 - 1- Go to Google.com Thread 2- 2- Go to Facebook.com First test done - browser closed and opens again. Thread 1 - 1- Go to Google.com Thread 2- 2- Go to Facebook.com First test done - browser closed and opens again.

Now it should pick the 3 and fourth row.现在它应该选择第 3 行和第 4 行。 Thread 1 - 3- Go to Dollarama.com Thread 2- 4- Go to Walmart.com线程1-3- Z5F075AE3E1F9D0382BB8C4632991F96F96FZ to dollarama.z4d236d9a2d9a2d9a2d102c56ad1c50da4bec50da4bec50z thread 2- 4- 4-

browser closed and opens again.浏览器关闭并再次打开。 Thread 1 - 5- Go to KegSteak.com螺纹 1 - 5- Go 到 KegSteak.com

[![testdata][1]][1] [![测试数据][1]][1]

What I actually see is two browsers open and one of the browser runs the url and the other just becomes static after launching chrome.我实际看到的是打开了两个浏览器,其中一个浏览器运行 url,另一个在启动 chrome 后变成 static。

Any fixes?有什么修复吗?

With Local WebDriver variable使用本地 WebDriver 变量

Make sure, you launch and tear-down your WebDriver within a test method:确保在测试方法中启动和拆除 WebDriver:

    @Test(dataProvider = "provideUrls")
    public void navigateByUrlTest(String url){
        WebDriver driver = ...
        driver.get(url);
        // do something
        driver.quit();
    }

    //I know this implemented to get data from Excel, but just for example..
    @DataProvider(parallel = true)
    public Object[][] provideUrls() {
        return new Object [][] {
                {"https://google.com"},
                {"https://facebook.com"},
                {"https://dollarama.com"},
                {"https://walmart.com"},
                {"https://kegSteak.com"}
        };
    }

With Global Thread-Safe WebDriver variable使用全局线程安全 WebDriver 变量

WebDriver configuration can be moved to @BeforeMethod/@AfterMethod methods. WebDriver 配置可以移动到@BeforeMethod/@AfterMethod方法。

NOTE : ThreadLocal wrapper should be used for WebDriver instance in this case.注意:在这种情况下,应将ThreadLocal包装器用于WebDriver实例。 This ensures we will keep separate WebDriver instances per each thread.这确保我们将为每个线程保留单独的 WebDriver 实例。


    protected ThreadLocal<WebDriver> driverThreadSafe = new ThreadLocal<WebDriver>();

    @BeforeMethod
    public void launchDriver() {
        driverThreadSafe.set(new ChromeDriver());
    }

    @AfterMethod
    public void quitDriver() {
        driverThreadSafe.get().quit();
    }

    @Test(dataProvider = "provideUrls")
    public void test(String url){
        WebDriver driver = driverThreadSafe.get();
        driver.get(url);
        // do something, but do not quit the driver
    }

Configure Threads Count配置线程数

<suite name='Automation' threadCount="5" - this will not work for DaraProvider parallelism. <suite name='Automation' threadCount="5" - 这不适用于DaraProvider并行性。

You have to pass dataproviderthreadcount testNG argument with thread count for data-provider.您必须将dataproviderthreadcount testNG 参数与数据提供者的线程计数一起传递。

eg programmatically, add this method to the current class (or parent base test class)例如,以编程方式,将此方法添加到当前 class(或父基测试类)

    @BeforeSuite
    public void setDataProviderThreadCount(ITestContext context) {
        context.getCurrentXmlTest().getSuite().setDataProviderThreadCount(5);
    }

References参考

TestNG parallel Execution with DataProvider TestNG 与 DataProvider 并行执行

https://testng.org/doc/documentation-main.html#running-testng https://testng.org/doc/documentation-main.html#running-testng

https://www.baeldung.com/java-threadlocal https://www.baeldung.com/java-threadlocal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用testng并行运行数据提供者测试 - Run data provider tests in parallel using testng 无法通过 testNG 运行并行测试 - Unable to run parallel tests via testNG 使用 TestNg 中的数据提供程序运行依赖于不同参数的并行测试方法 - Run parallel test methods which are dependent with different parameter using Data Provider in TestNg 在Testng中使用数据提供程序和并行方法,如何在给定测试的同一线程中在方法之前,方法之后和测试中运行? - Using data-provider and parallel in Testng, how to run before method, after method and test in same thread for a given test.? 如何使用数据提供程序在 testNG 上并行运行测试? - How to run tests in parallel on testNG using data provider? TestNG并行运行单线程数据提供程序 - TestNG parallel runs with a single threaded data provider 使用testNG并行运行测试 - Make test run in parallel using testNG 如何在TestNG中并行运行测试用例? - How to run test cases in parallel in TestNG? 无法在Cucumber&Testng中运行并行测试 - Unable to run parallel tests in Cucumber &Testng 当测试类作为“ TestNG Test”运行时,无法从testng.xml中检索参数值 - Unable to retrieve value of parameter from testng.xml when test class is run as 'TestNG Test'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM