Make Selenium tests illuminating by saving the browser log and a screenshot
Jan 23, 2018
Tests need Illumination to allow us to diagnose what has gone wrong. We should never have to re-run a test to figure out what the actual problem is.
One of the main culprits for tests that are hard to diagnose is Selenium tests. Because Selenium runs via a web browser rather than an isolated bit of code, more things can go wrong.
We can make our Selenium tests illuminating by adding extra information on test failure. By default Selenium will give us an exception saying it expected an element to be present which isn’t. But that’s hard to reason about. Ok, we expected this, but what was on the page? Was it a popup blocked it? Did our frontend code throw an exception?
We have no idea.
The following code is available on Github but you can copy it into your existing code bases too. Once you are using it your exceptions will look like this:
=================== SCREENSHOT ========================
Saved to: /Users/you/selenium-starter/build/screenshot-goToWebPage.jpg
=======================================================
================== BROWSER LOGS =======================
Sun Jan 21 18:50:39 GMT 2018 SEVERE http://automationpractice.com/img/p/1/1-home_default2x.jpg - Failed to load resource: the server responded with a status of 404 (Not Found)
Sun Jan 21 18:50:39 GMT 2018 SEVERE http://automationpractice.com/img/p/7/7-home_default2x.jpg - Failed to load resource: the server responded with a status of 404 (Not Found)
=======================================================
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"partial link text" "selector":"something that isn't there"}
So let’s see what we have to do to get to this point.
First up we have a standard test. This test is checking for something that doesn’t exist, and will always fail.
You may have spotted that this class inherits from SeleniumBase. That’s where we’ve added a sprinkle of magic that makes our tests more illuminating.
The first part is setting up a callback which allows us to do something when a test fails:
From the failed() method we can do anything we want. So first let’s take a screenshot:
This saves a screenshot of what was available to Selenium in the build directory. Allowing you to see precisely what was present.
Next up let’s log everything the browser console has:
This is useful if you are using a frontend framework like React, Angular or copious JQuery. The errors that these libraries create will be available you in the test output.
Now we’ve seen the parts alone, here is the SeleniumBase file in full:
Adding these helpers make your Selenium tests more illuminating. Happy testing.