0

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 ?

JNo
  • 9
  • 6

1 Answers1

0

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.

e.g. 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

Max Daroshchanka
  • 2,698
  • 2
  • 10
  • 14