How to Add Growl Notifications to Your Tests
Intro
Good test reports are a fundamental component of successful test automation, but running down a test failure by looking at a test report can be a real pain sometimes. Oftentimes, you're left to decipher debug statements, or go through things piece by piece -- all for the sake of trying to track down a transient issue.
A Solution
By leveraging something like jQuery Growl you can output non-interactive debugging statements directly to the page you're testing.
With Growl way you can see helpful information and more-likely correlate it to the test actions that are being taken. This can a boon for your test runs when coupled with screenshots and/or video recordings of your test runs
Let's step through an example of how to set this up.
Example
We'll visit the page we want to display notifications on and do some work with JavaScript to load jQuery, jQuery Growl, and the styles for jQuery Growl. After that, we can issue commands to jQuery Growl to display notification messages:
- Check for jQuery on the page, and add it if need be.
- Use jQuery to add jQuery Growl to the page.
- Use jQuery to add the jQuery Growl styles to the page.
- Display a message with jQuery Growl (e.g.,
$.growl({ title: 'GET', message: '/' });).
Adding the scripts to the page happens in the background, so after steps 1 and 3 we wait until they are loaded
(e.g., until window.jQuery and $.growl are defined) instead of sleeping for a fixed amount of time. And to make
sure everything worked, we wait for the notification to be displayed and check its title and message.
If we wanted to see color-coded notifications, then we could use one of the following instead:
$.growl.error({ title: 'ERROR', message: 'your error message goes here' });
$.growl.notice({ title: 'Notice', message: 'your notice message goes here' });
$.growl.warning({ title: 'Warning!', message: 'your warning message goes here' });
Expected Behavior
- Browser opens
- Load the page
- Add jQuery, jQuery Growl, and the jQuery Growl styles to the page
- Display a notification message in the top-right corner of the page
- Check the title and message of the notification
- Browser closes
Summary
In order to use this approach, you will need to load jQuery Growl on every page you want to display output to -- which can be a bit of overhead. But if you want rich messaging like this then that's the price you have to pay.
I'd like to give a big thanks to Jon Austen (GitHub) for giving me the idea to use jQuery Growl with Selenium.
Happy Testing!