Run onFinish listener in the same thread as the test

299 views
Skip to first unread message

Ajith V L

unread,
Jan 22, 2015, 9:38:35 AM1/22/15
to testng...@googlegroups.com
Hi All,

I am using TestNG in my selenium framework and i am running tests in parallel. I am using ThreadLocal to instantiate driver for each thread. It works great but the problem is when i want to close the driver. 

The onFinish() method in ITestListener runs in a different thread compared to the test. So its tricky to close the driver instance after test execution. Currently I am working around this by defining a @AfterClass method in each test (even if i don't need it) and closing the driver in afterInvocation() method in IInvokedMethodListener. 

    @Override
    public void afterInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) {
        if (iInvokedMethod.getTestMethod().isAfterClassConfiguration()) {
            Library.quitDriver();
        }
    }

Could someone please suggest me a better way to close driver?


Heres how the thread association looks like,

87   [TestNG] INFO  com.aol.automation.framework.listener.TestListener  - (8)## onStart EVENT LISTENER --------------------------------

  8520 [pool-1-thread-1] INFO  com.aol.automation.framework.listener.TestListener  - (11)## onTestStart EVENT LISTENER ----------------------------
11553 [pool-1-thread-1] INFO  com.aol.automation.framework.listener.TestListener  - (11)## onTestFailure EVENT LISTENER --------------------------
11850 [pool-1-thread-1] INFO  com.aol.automation.framework.listener.TestListener  - (11)## onTestStart EVENT LISTENER ----------------------------
12364 [pool-1-thread-1] INFO  com.aol.automation.framework.listener.TestListener  - (11)## onTestSuccess EVENT LISTENER --------------------------
12365 [pool-1-thread-1] INFO  com.aol.automation.framework.listener.TestListener  - (11)## onTestStart EVENT LISTENER ----------------------------
13913 [pool-1-thread-1] INFO  com.aol.automation.framework.listener.TestListener  - (11)## onTestSuccess EVENT LISTENER --------------------------

14199 [TestNG] INFO  com.aol.automation.framework.listener.TestListener  - (8)## onFinish EVENT LISTENER -------------------------------


Thanks,
-Ajith

Shawn McCarthy

unread,
Jan 22, 2015, 12:48:54 PM1/22/15
to testng...@googlegroups.com
I use InheritableThreadLocal  ( http://docs.oracle.com/javase/7/docs/api/java/lang/InheritableThreadLocal.html ). Maybe that might help you ?

Krishnan Mahadevan

unread,
Jan 29, 2015, 2:08:20 AM1/29/15
to testng...@googlegroups.com
Shawn,

To the best of my knowledge, InheritableThreadLocal would be used when the parent thread wants the threadlocal variable to which it has access to, to be available to all the child threads as well.

So are you sure that it would work in this case because the listeners arent spawning any additional threads but it is TestNG that is doing this.


Ajith,

TestNG guarantees that the below would always be executed on the same thread :

  • beforeInvocation
  • @Test annotated test method
  • afterInvocation
So keeping that in mind, please check if this blog post of mine helps you with your problem : https://rationaleemotions.wordpress.com/2013/07/31/parallel-webdriver-executions-using-testng/








Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

Shawn McCarthy

unread,
Jan 29, 2015, 9:35:26 AM1/29/15
to testng...@googlegroups.com
Krishnan, no I am not sure. You seem to be more knowledgeable about TestNG than me, so your guess is better than mine.
Reply all
Reply to author
Forward
0 new messages