简体   繁体   中英

Unable to run parallel test via TestNg from DATA provider

Below is my XML piece.

<?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. What I wanted to achieve is to run each row in different threads. Lets say I had 5 rows of data

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.

Now it should pick the 3 and fourth row. Thread 1 - 3- Go to Dollarama.com Thread 2- 4- Go to Walmart.com

browser closed and opens again. Thread 1 - 5- Go to KegSteak.com

[![testdata][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.

Any fixes?

With Local WebDriver variable

Make sure, you launch and tear-down your WebDriver within a test method:

    @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 configuration can be moved to @BeforeMethod/@AfterMethod methods.

NOTE : ThreadLocal wrapper should be used for WebDriver instance in this case. This ensures we will keep separate WebDriver instances per each thread.


    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.

You have to pass dataproviderthreadcount testNG argument with thread count for data-provider.

eg programmatically, add this method to the current class (or parent base test class)

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

References

TestNG parallel Execution with DataProvider

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM