Issue in capturing Selenium failure screenshots in Listener class

860 views Asked by At

I am using a listener class with ReportNG to capture screenshot of failed test cases. The problem is When I am using following in listener class:

WebDriver driver = new FirefoxDriver();
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

Problem :Webdriver opens another blank browser window as the listener class is not doing anything.And captures blank screenshot.

So my objective is to take screenshot of failed test case written in base class from Listener class.

Please help me.

2

There are 2 answers

0
RAKY On BEST ANSWER

I got it resolved by defining a static method for capturing screenshot in Base class and calling it by class name in my Listener class.

Thanks People.

0
d0x On

If you instantiate a new webDriver with new FirefoxDriver();. The web driver does not know which address to open. You have to call the webDriver.get() method before taking the screenshot.