Implementing TestNG listener to capture screenshot in TestNG parallel running mode

2,238 views
Skip to first unread message

sel.scone

unread,
Nov 12, 2012, 9:50:18 PM11/12/12
to seleniu...@googlegroups.com
I am currently driving my Selenium-Webdriver tests using TestNG and I am planning to run them in parallel in the future using TestNG's parallel option.

Currently, my driver (firefox, ie, etc.) is created in @BeforeMethod. I would like to take a screenshot of my test if it fails. I know one way to do it is to have ITestResult as a parameter of your @AfterMethod, then in your @AfterMethod, check if the result is failure and take a screenshot using your driver if it is. However, I do not want to keep repeating that conditional check (whether test result is failure) in every one of my @AfterMethod (Imagine I have 20 test classes and each one of them has @AfterMethod). An idea I have is to implement ITestListener and take a screenshot in method onTestFailure().

However, I am not familiar with multi-threading at all, and currently my driver is not globally accessible/is instance-based (i.e. I don't have for example, a static method like DriverManager.getDriver() to access/get my driver). My question is, if I change my code and make my driver global, and use it to take a screenshot in the onTestFailure() method of my listener class, is the driver/my test/screenshot going to be all confused when I run my tests in parallel using TestNG?

Thanks a lot in advance! 


Krishnan Mahadevan

unread,
Nov 12, 2012, 9:53:25 PM11/12/12
to seleniu...@googlegroups.com
http://darrellgrainger.blogspot.in/2012/07/generating-screen-capture-when-using.html
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/309tg-OJlioJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 


--
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/

S N

unread,
Nov 12, 2012, 10:01:00 PM11/12/12
to seleniu...@googlegroups.com
Thank you Kristnan! I already have that in my code - I can take screnshot with my driver.

However, I want my code to take a screenshot automatically when a test fails.

I am not sure if implementing TestNG's ITestListener is a solution at all if I am going to run tests in parallel using TestNG..
And basically, is it even possible to use a global driver and run tests in parallel, and the driver in different in each test? It doesn't sound possible at all to me.. Sorry if I ask this question because my brain is not functioning..

S N

unread,
Nov 12, 2012, 10:03:11 PM11/12/12
to seleniu...@googlegroups.com
and the driver *is* different in each test

Krishnan Mahadevan

unread,
Nov 12, 2012, 10:04:04 PM11/12/12
to seleniu...@googlegroups.com
When you are doing parallel runs you should work with a ThreadLocal. That would ensure that every thread gets its own copy of the WebDriver. 

Capturing screenshots on failure IMO should be done using EventFiringWebDriver because the code is a lot more cleaner and you are NOT polluting your listener with all the Webdriver references etc. 

Hope that helps. 

S N

unread,
Nov 12, 2012, 10:05:10 PM11/12/12
to seleniu...@googlegroups.com
I wish I could remove this useless thread...sorry I dont think my brain is working right now -__-.....I dont think implementing ITestListener will work in my case.

S N

unread,
Nov 12, 2012, 10:06:13 PM11/12/12
to seleniu...@googlegroups.com
Hey thanks! Let me look into EventFiringWebDriver. Really appreciate your help!

Michael Wowro

unread,
Nov 13, 2012, 5:29:31 AM11/13/12
to seleniu...@googlegroups.com
@Krishnan,

thx, especially for the hint with LocalThread

Yours Michael

Michael Wowro

unread,
Nov 13, 2012, 5:32:21 AM11/13/12
to seleniu...@googlegroups.com
... ThreadLocal ...

Mark Collin

unread,
Nov 13, 2012, 6:31:31 PM11/13/12
to seleniu...@googlegroups.com

I updated my Selenium Maven template today to use TestNG and take screenshots on failure using a listener.

 

The code for multi threaded runs at method level is in there but not finished yet (need to read in the number of threads from a maven property, coming soon)

 

https://github.com/Ardesco/Selenium-Maven-Template

S N

unread,
Nov 14, 2012, 2:26:24 AM11/14/12
to seleniu...@googlegroups.com
Wow Mark, thanks! I will stay tuned :)
Reply all
Reply to author
Forward
0 new messages