Re: [testng-users] Capture WebDriver Screenshots When Running Parallel Tests With TestNG

900 views
Skip to first unread message

Krishnan Mahadevan

unread,
Sep 21, 2012, 4:06:54 AM9/21/12
to testng...@googlegroups.com
Yes there is.

Here's what you would need to do :

To begin with, you would need to create a new Listener, which takes the responsibility of instantiating your webdrivers.

The listener would need to implement IInvokedMethodListener and within its beforeInvocation() and afterInvocation, you would basically need to write the logic of instantiating the webdriver [ A good approach would be to invoke a static method within another class, which would instantiate the webdriver and then insert it into a ThreadLocal variable]

Now when the control comes back to your Test method, you would already have a reference to the threadlocal variable [you can have a simple getter that can do this]

Now you would need to write up another static method implementation which basically recycles the threadlocal webdriver instance after usage and then have it called from 

onTestFailure
onTestSkipped
onTestFailedButWithinSuccessPercentage
onTestSuccess

since a test method can end via any one of the events and within onTestFailure plugin a call to your captureScreenshot()

That should do !

Hope that helps !


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/


On Thu, Sep 20, 2012 at 9:40 PM, Herik Webb <heri...@gmail.com> wrote:

I am currently capturing screenshots on failure and success in TestNG by way of overriding the TestListenerAdapter methods onTestFailure, and onTestSuccess respectively. In order to do this you need to specify which driver you want to take a screenshot of.

My question: Is there a good way to capture screenshots when running tests in parallel on the method level?

In order to run tests in parallel, each individual test needs a unique driver instance. So, at any given instance you have x number of driver instances running. When it comes time to capture a screenshot, how do you determine which driver to use?


This question is also posted on stackoverflow here, http://stackoverflow.com/questions/12481972/capture-webdriver-screenshots-when-running-parallel-tests-with-testng


Code snippets:

public class OnFailureListener extends TestListenerAdapter {    

@Override   
public void onTestFailure(ITestResult tr) {     
   Screenshots.captureScreenshot(tr);

   super.onTestFailure(tr);             
}
public static void captureScreenshot(ITestResult tr) {
   WebDriver driver = TestClass.driver;

   if (driver instanceof TakesScreenshot) {                                                                                                         
      String filename = "path/to/screenshot/file";

   try {
      File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
      FileUtils.copyFile(scrFile, new File(filename));
   } catch (IOException e) { e.printStackTrace(); }
}

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/okk2NYruNqsJ.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.

Herik Webb

unread,
Sep 25, 2012, 10:11:16 AM9/25/12
to testng...@googlegroups.com
Thanks Krishnan! 

I'll report back once I've had a chance to implement this.

-Herik

edwolb

unread,
Sep 26, 2012, 4:24:57 PM9/26/12
to testng...@googlegroups.com
An alternative approach is that the TestListener passes an ITestResult which can be used to get a reference to your test instance.  Our tests are always run in parallel=classes, which means we can use class variables to store the WebDriver.  Our tests all extend a base template class, where we've implemented a takeScreenshot() function which returns a File representing the screenshot taken from the local WebDriver instance.  Krishnan's solution looks better, but this might be the cheaper way to do it :)

--
Chris

Manoj Kapuganti

unread,
Mar 7, 2013, 12:41:34 PM3/7/13
to testng...@googlegroups.com
Hello Tyagi,

I am also executing suites in parallel

Below is the code that I am using and its working fine

@AfterMethod
public void setScreenshot(ITestResult result)throws IOException  {
if (!result.isSuccess()) {
Calendar calendar = new GregorianCalendar();
String methodName = result.getName();
String filename=ProjectPath+"\\Screenshots\\"+methodName+ "-" +calendar.get(Calendar.HOUR)+ "-" +calendar.get(Calendar.MINUTE)+ "-" + calendar.get(Calendar.SECOND)+".jpg";
File screenshotFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshotFile,new File(filename));
}
}

Best Regards,
Manoj Kapuganti

On Wednesday, 27 February 2013 18:58:54 UTC+5:30, Deepankur Tyagi wrote:
Hi Herik,
Have you got it working?
Are you gracious enough to share the code please?
Thanks
Reply all
Reply to author
Forward
0 new messages