簡體   English   中英

無法通過來自數據提供商的 TestNg 運行並行測試

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

下面是我的 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> 

因此,我通過 TestNg 中的 DATA PROVIDER 從 excel 加載所需的數據。 我想要實現的是在不同的線程中運行每一行。 假設我有 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

並說我正在運行兩個線程意味着兩個瀏覽器。 我希望兩個瀏覽器以任何順序並行運行每一行。

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

現在它應該選擇第 3 行和第 4 行。 線程1-3- Z5F075AE3E1F9D0382BB8C4632991F96F96FZ to dollarama.z4d236d9a2d9a2d9a2d102c56ad1c50da4bec50da4bec50z thread 2- 4- 4-

瀏覽器關閉並再次打開。 螺紋 1 - 5- Go 到 KegSteak.com

[![測試數據][1]][1]

我實際看到的是打開了兩個瀏覽器,其中一個瀏覽器運行 url,另一個在啟動 chrome 后變成 static。

有什么修復嗎?

使用本地 WebDriver 變量

確保在測試方法中啟動和拆除 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"}
        };
    }

使用全局線程安全 WebDriver 變量

WebDriver 配置可以移動到@BeforeMethod/@AfterMethod方法。

注意:在這種情況下,應將ThreadLocal包裝器用於WebDriver實例。 這確保我們將為每個線程保留單獨的 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
    }

配置線程數

<suite name='Automation' threadCount="5" - 這不適用於DaraProvider並行性。

您必須將dataproviderthreadcount testNG 參數與數據提供者的線程計數一起傳遞。

例如,以編程方式,將此方法添加到當前 class(或父基測試類)

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

參考

TestNG 與 DataProvider 並行執行

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM