The beginning of Watir’s RSpec html formatter
I’ve promised in one of my earlier posts to create a better RSpec formatter for using with Watir. There isn’t anything hard when it comes to creating your own formatters - you just have to extend some of the already existing RSpec formatter’s classes and then add your own functionality to them.
Since I’m interested in HTML reports as the one created in earlier post, then i’m going to extend RSpec’s HtmlFormatter class. You can look it’s code if you like, but it’s not necessary to understand the things i’m going to do.
So, what could we add to these reports? For example, it would be cool if there would be a screenshot of the browser window when spec failed. Also html of the same page would be nice, i guess.
For the screenshots i’m going to use the gem called win32screenshot. Since it makes screenshots in bitmap (bmp) format, then i’m going to use the RMagick gem which is an interface for the ImageMagick to convert them into png format for less space. Make sure that you download the win32 version of it which also includes correct version of ImageMagick itself. You could also use something else for creating the screenshots if you like.
Next step would be to create the formatter class itself. It would have to be something like this:
That is in fact a fully working RSpec formatter and it works just because we haven’t broken any HtmlFormatter’s functionality. There are some methods in the formatter classes, which might be of interest to us, like: example_group_started, example_started, example_passed, example_failed, example_pending and extra_failure_content. The most interesting one is extra_failure_content, which is used to add some extra content to failing spec’s output and that is what we want. Something like this might work for our current needs:
As you can probably understand, then this method saves a screenshot and a html when spec fails and adds links to the files to the report. Excellent! Let’s have a closer look of the used methods:
First thing to notice here is that a global variable called $browser is used. This is not a good style, but don’t worry about it - i’m going to remove it at some of my next posts and it’s only used this time for simplicity’s sake also in save_screenshot method. There is also used a method called file_path which is needed to create a unique file path for every file created during the spec so you won’t accidentally overwrite one file with another and then wonder how the heck something got into your results which shouldn’t be there at all. Ok, this is how save_screenshot looks like:
I’m not sure if it is a limitation of Windows itself but browser window has to be visible thus it’s needed to bring to front before making the actual screenshot. So if you’re running your specs with browser being invisible then this would be the place to make it visible (at least for a second).
I have to change my original spec to use a global variable called $browser and also maximize the window for a better screenshot experience:
Now, to use your own custom formatter you just have to specify it when launching your specs:
spec --require watir_html_formatter.rb --format WatirHtmlFormatter:output/index.html watir_with_rspec.rb
You can see the final results at here and grab the whole formatter implementation from GitHub.
That would be all for now.




2 years ago