JackhammersForWeeks
JackhammersForWeeks

Reputation: 967

Selenium grid, tests start starting on the same thread when run in parallel

Using selenium grid with TestNg running parallel tests, the @BeforeTest method seems to start two tests on the same thread, any thoughts why? I am trying to ensure that each test class starts on its own thread.

@BeforeTest(alwaysRun = true)
@Parameters({ "selenium.OS", "selenium.browser","selenium.testClassNameHere" })
public void beforeTest(String OS, String browser, String testClassNameHere) {
    Out.trace("Starting beforeTest for "+testClassNameHere+" on thread:  " + Thread.currentThread().hashCode());
    //create driver here
    Out.trace("beforeTest has finished");
}

However, here is the log, showing that two of the test classes started on the same thread, and only two drivers get created, which causes all sorts of headaches with listeners and reporting.

TRACE: Starting beforeTest for AcceptanceTests.ErrorReportingTests on thread:  777376239
TRACE: Starting beforeTest for AcceptanceTests.PopupTests on thread:  777376239
TRACE: Starting beforeTest for AcceptanceTests.LoginPageTests on thread:  1235740568
TRACE: Finished making driver.
TRACE: Finished making driver.
TRACE: beforeTest has finished
TRACE: beforeTest has finished

Here is the ANT xml file showing three test classes...

<?xml version="1.0"?>
<suite name="Debugging QA Tests" parallel="tests" thread-count="10">
   <test name="Popup Tests" preserve-order="true" >
        <parameter name="selenium.OS" value="localhost" />
        <parameter name="selenium.browser" value="chrome" />
        <parameter name="selenium.testClassNameHere" value="AcceptanceTests.PopupTests" />
        <classes>
            <class name="qaautomation.AcceptanceTests.PopupTests" />
        </classes>
    </test>
  <test name="Login Page Tests" preserve-order="true">
        <parameter name="selenium.OS" value="localhost" />
        <parameter name="selenium.browser" value="chrome" />
        <parameter name="selenium.testClassNameHere" value="AcceptanceTests.LoginPageTests" />
        <classes>
            <class name="qaautomation.AcceptanceTests.LoginPageTests" />
        </classes>
    </test>
    <test name="Error Reporting Tests" preserve-order="true">
        <parameter name="selenium.OS" value="localhost" />
        <parameter name="selenium.browser" value="chrome" />
       <parameter name="selenium.testClassNameHere" value="AcceptanceTests.ErrorReportingTests" />
         <classes>
            <class name="qaautomation.AcceptanceTests.ErrorReportingTests" />
        </classes>
    </test>
</suite> 

Upvotes: 1

Views: 199

Answers (1)

Amit Bhoraniya
Amit Bhoraniya

Reputation: 621

In which class have you defined beforeTest method. I have created same structure and following code is working for me on different thread.

public class TestCase {

    @BeforeTest(alwaysRun = true)
    @Parameters({ "selenium.OS", "selenium.browser","selenium.testClassNameHere" })
    public void beforeTest(String OS, String browser, String testClassNameHere) {
        System.out.println("Starting beforeTest for "+testClassNameHere+" on thread:  " + Thread.currentThread().hashCode());
        //create driver here
        System.out.println("beforeTest has finished");
    }
}

public class PopupTests extends TestCase{

    @Test(description="popup reporting")
    public void teszt()
    {
        System.out.println("popup");
    }
}
public class ErrorReportingTests extends TestCase{

    @Test(description="error reporting")
    public void teszt()
    {
        System.out.println("Error");
    }
}

public class LoginPageTests extends TestCase {

    @Test(description="login reporting")
    public void teszt()
    {
        System.out.println("Login");
    }
}

And I have used same testng xml config file as you given, following is my output.

Starting beforeTest for AcceptanceTests.ErrorReportingTests on thread:  1968879864
beforeTest has finished
Starting beforeTest for AcceptanceTests.LoginPageTests on thread:  88161428
beforeTest has finished
Starting beforeTest for AcceptanceTests.PopupTests on thread:  1040963230
beforeTest has finished
Login
popup
Error

Upvotes: 0

Related Questions