Selenium 2/ Web Driver + Screenshot for failing tests

1,804 views
Skip to first unread message

sebarmeli

unread,
Jun 15, 2011, 11:15:27 PM6/15/11
to testng-users
Hi there,

I'm using Selenium 2 in my test cases and I've got a TestBase class
with an init method like this;

protected WebDriver webDriver;
protected Page page;
@BeforeClass
public void init()
{
webDriver = ... //get the WebDriver
page= PageFactory.initElements(webDriver, Page.class);

}

and few test classes extending this base class.

All god, I just want to taking screenshots in case that test methods
fails.

I've got the ScreenshotOnFailureListener that implements
IInvokedMethodListener and I registered it in testng.xml file. This is
the content of the class:

@Override
public void afterInvocation(IInvokedMethod method, ITestResult
result)
{
if (!result.isSuccess()) {

File f = ((TakesScreenshot)
webDriver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(f, new File("target/surefire-
reports/UIIntegrationTests/screenshots/" +
method.getTestMethod().getMethodName()));
}
catch (IOException e) {
e.printStackTrace();
}
}
}

@Override
public void beforeInvocation(IInvokedMethod arg0, ITestResult
arg1)
{
// TODO Auto-generated method stub

}

The class doesn't compile because "WebDriver" doesn't exist in this
context, I tried to set the variable as static but it doesn't seem
right (and debugging I get a null value in the listener).

Any help on that? Is there any better approach?

Cheers

Ramaraju Gelli

unread,
Jun 16, 2011, 1:06:02 AM6/16/11
to testng...@googlegroups.com
Hi,

In you Base class add annote @AfterMethod(ITestResult)
and also add alwaysRun=true (Even to call when using groups)

In this AfterMethod call you screen shot method.Since it is in base class your web drive is still accessible.

 Thanks,
Ramaraju Gelli


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
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.




--
Thanks,
Ramaraju Gelli

sebarmeli

unread,
Jun 16, 2011, 2:48:22 AM6/16/11
to testng-users
Cool, it works.

Only note, I just had to use Augmenter class in the setScreenshot
method.

Thanks!

Lolz ha ha ha

unread,
Oct 19, 2012, 12:53:57 AM10/19/12
to testng...@googlegroups.com, seba....@gmail.com
Hi,

I am facing the same problem of taking screenshots after every test case. I am new to testng. Please help in telling what to do and where to add the things in testng.xml. Please help.

Thank You
Nitin Rajpal

Yevhen Bilevych

unread,
Oct 19, 2012, 2:50:38 AM10/19/12
to testng...@googlegroups.com
Hi,

We extend TestListenerAdapter. Below is the snippet you may use in your listener to grab WebDriver instance (in italics):

public class ScreenshotOnFailureListener extends TestListenerAdapter
{

    @Override
    public void onTestFailure(ITestResult result)
    {
        Reporter.setCurrentTestResult(result);

        Object currentClass = result.getInstance();
        WebDriver webDriver = ((BaseTest) currentClass).webDriver;


            File f = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
            try {
                FileUtils.copyFile(f, new File("target/surefire-reports/UIIntegrationTests/screenshots/" +
--
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/-/_OgWi9UD_IYJ.

Lolz ha ha ha

unread,
Oct 19, 2012, 2:16:02 PM10/19/12
to testng...@googlegroups.com
Hi,

Thank You for the reply.

I have two test cases. One will pass and another will fail. I want to take screenshot in both the cases. The screenshot functionality I want in such a way that it should execute after every test case steps gets executed.
I have no idea about listeners that where to write and how to extend. Please explain more.

Thank You
Nitin

Krishnan Mahadevan

unread,
Oct 19, 2012, 2:21:08 PM10/19/12
to testng...@googlegroups.com
If you would like to take screenshots after every step then you should be thinking of using an EventFiringWebDriver. 

This has got nothing to do with TestNG because TestNG is more of a test framework and it doesnt have this function. 
To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/qfG4bKsjMvQJ.

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.


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

Lolz ha ha ha

unread,
Oct 19, 2012, 2:26:20 PM10/19/12
to testng...@googlegroups.com
Where can i find more details? If u can guide me more about it?
To unsubscribe from this group, send email to testng-users+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.

Krishnan Mahadevan

unread,
Oct 19, 2012, 2:32:39 PM10/19/12
to testng...@googlegroups.com
Nitin,
I dont know if there is any explanatory document that shows you how to use it. 

Take a look at my gist post. It should give you an idea https://gist.github.com/1728633

This thread should also give you some idea : https://groups.google.com/forum/m/#!topic/webdriver/m1sGmmysrMo

It includes Darrel's blog post link as well. 

To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/cUnSIXWXoBoJ.

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.


--
Reply all
Reply to author
Forward
0 new messages