Saturday, May 18, 2019

How to tackle with black screenshots in Selenium?

Extent Reports - Black Screenshots
Prior to "How", Let us focus on "Why"??

Why black Screenshot????


Point to Note, I am talking Java here!

Most of us try to take screenshot using "onTestFailure(ITestResult result)" or "onTestSkipped(ITestResult result)" methods of class ITestListener, in Selenium. Sometimes, while trying to take the screenshot after test failure, the driver connection with browser is already broken and hence, webdriver fails to take a screenshot.



OH SNAP!! WEBDRIVER, YOU'RE TOO LATE!!!



Now, we know the cause. Let's find out the best RCA.
It's really simple, Dude. Just break up with Selenium and Move On!
Kidding!!
Well, it's not that simple as it sounds. Remember, Selenium still has the largest community than all other Automation tools out there. Apart from that, Migrating thousands of lines of code from one framework to another is hell of a task.
What should we do then??
Why don't we take the screenshot while the connection is still active??

Let's utilize one of my favourite features of selenium to do so, i.e., AbstractWebDriverEventListener class. This class provides us with the facility to execute any task before/after any web driver event.
Refer: Click Here! 

Now, all we need to do is take a screenshot each time the webdriver performs an event, say, click.



saveScreesnshot(driver) is a local method, i.e.,

NOTE: I use the convention to start the testcase name with "TC". You can use the condition that fits your convention.

Last but not the least, log screenshot to Jenkins.



TA DA!!
We're done. Simple enough, yeah!

There is much more to the AbstractWebDriverEventListener class. You can try the following tasks yourself:

1. Save step-wise screenshots.
2. Add wait after every click or navigation.
3. Think Yourself for more applications!

THANK YOU!

Follow by Email





How to tackle with black screenshots in Selenium?

Extent Reports - Black Screenshots Prior to "How", Let us focus on "Why"?? Why black Screenshot???? ...