2
votes

I am trying to set up Webdriver parallel execution with Webdriver Hub and TestNG parallel mechanism. I am facing an issue with thread

I have this class which extends TestBaseSetUp, which has a BeforeMethod and AfterMethod and set to run always. For webdriver parallel execution, I wanted to use ThreadLocal, but @Test and @Before/@After method are in different thread.So If I set webdriver as ThreadLocal in my TestBaseSetUp, and try get in my test method it returns null.

public class TestCheck extends TestBaseSetUp {
    @Test
    public void test(){
          System.out.println("Thread in test " + Thread.currentThread().getId());

} }

Do we have a way so that @Test is also in same thread as @Before/@After method

2

2 Answers

0
votes

did you tried

    @Test(singleThreaded=true)

At class level. so that all test methods in that class will run on same thread. here is example

Thank You, Murali

0
votes

@Manish_pat

Take a look at this blog post of mine : https://rationaleemotions.wordpress.com/2013/07/31/parallel-webdriver-executions-using-testng/ The idea is to move away from relying on config methods for web driver instantiation into a TestNG listener driven model wherein the webdriver object is created from within the beforeInvocation() and destroyed in the afterInvocation(). TestNG guarantees that beforeInvocation(), @Test and afterInvocation() would always be executed on the same thread. So now you can go ahead and work with ThreadLocal in here.