Embed screenshot in Watir-rspec report (not just for failed tests)

469 views
Skip to first unread message

Arjen Ruiterkamp

unread,
Apr 15, 2014, 6:07:04 AM4/15/14
to watir-...@googlegroups.com
Hello,

For some acceptance tests I want to include screenshots, which are made during my specs, to the report (thus not just for failing specs).
The watir-rspec readme states:

You can use Watir::RSpec.file_path to have links automatically in the html report to the files created during tests.
uploaded_file_path = Watir::RSpec.file_path("uploaded.txt")
File.open(uploaded_file_path, "w") {|file| file.write "Generated File Input"}
file_field(:name => "upload-file").set uploaded_file_path
Where do I put this code? When I put it in my spec_helper.rb, I get this error: undefined method `file_field' for main:Object (NoMethodError)
Anyone has some ideas what I'm doing wrong?

Jarmo Pertman

unread,
Apr 17, 2014, 5:53:05 PM4/17/14
to watir-...@googlegroups.com
Hi!

Are you saying that you want to take screenshots after every spec?

Jarmo Pertman
-----
IT does really matter - http://itreallymatters.net

Arjen Ruiterkamp

unread,
Apr 18, 2014, 3:30:21 AM4/18/14
to watir-...@googlegroups.com
Hi Jarmo, thanks for replying

I'll clarify a bit what I would like in my rspec report.

I don't want to make a screenshot after every spec, just during some of the specs which test Flot graphs (a Javascript library for displaying graphs).
These Flot graphs are pretty hard to interact with using Watir-Webdriver thus I want to add a screenshot to the report (or a link to the screenshot) so our testers can quickly check if nothing weird is going on.

So my (simplified) spec looks like this: 
describe "do something once logged in" do

  it "should screenshot the flot graph" do
    screenshot.save 'flot.png'
  end
end

Then the generated report should look like this:













Hopefully this clears up my question.

Is this possible with just watir-rspec or is some form of monkey patching required?

Regards,
Arjen

Jarmo Pertman

unread,
Apr 18, 2014, 6:58:13 AM4/18/14
to watir-...@googlegroups.com
Hi!

Unfortunately there's no built-in support for such a requirement in `watir-rspec` nor `rspec` itself.

However, you can achieve that quite easily by creating your own `HtmlFormatter`. Easiest would be to extend `Watir::RSpec::HtmlFormatter` (https://github.com/watir/watir-rspec/blob/master/lib/watir/rspec/html_formatter.rb) and then override `example_passed` from `RSpec::Core::Formatters::HtmlFormatter` (https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/formatters/html_formatter.rb#L48-L52) and insert the links created during your specs from `@files_saved_during_example`. It seems to me that you need to create a different method for printing out the html than it is done currently by RSpec at `HtmlPrinter` (https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/formatters/html_printer.rb#L29-L32).

Then, in your specs you just need to save the screenshot like this:
browser.screenshot.save Watir::RSpec.file_path("uploaded.txt") # => this creates a unique file name and adds it to the `@files_saved_during_example` array as well.

Also, refer to the code currently adding links to files created during failing specs to get some idea how it is done:

And don't forget to use your own HtmlFormatter in spec_helper instead of Watir::RSpec::HtmlFormatter.

The whole approach written above is not tested in practice, but seems to be doable in theory without monkey-patching, but using regular OO programming practices :)

Jarmo Pertman
-----
IT does really matter - http://itreallymatters.net


Jarmo Pertman

unread,
Apr 18, 2014, 9:40:03 AM4/18/14
to watir-...@googlegroups.com
Now, after giving some thoughts about it - the way you try to solve it is not best solution for your problem. I mean, automated tests should be automated - if test does not fail then there should be any reason to look at the reports. By making test pass and then looking at the screenshot attached to the report to make sure that it really passed seems wrong.

Correct solution would be to test it on some lower level. For example, test the data given to the Flot library. Best solution would be to use JavaScript unit testing in that case, but you can also do it by using Watir::Browser#execute_script by returning some JavaScript object back to Ruby and then asserting there.

Jarmo Pertman
-----
IT does really matter - http://itreallymatters.net


Arjen Ruiterkamp

unread,
May 6, 2014, 5:43:16 AM5/6/14
to watir-...@googlegroups.com
Hi Jarmo,

You give some good points to think about. I agree that there should be no reason to look at the reports when they don't fail.
On the level on integration testing, we're mostly interested in the layout of the Flot graph (scaling/stacking of bars etc.) and I'm not sure if unit testing the data given to the Flot graph will cover this 'requirement'.
Maybe I can extract the location of a bar in the Flot graph using Javascript and then interact with that using Watir-webdriver.
This will require some more research on my part and is probably more related to Flot than Watir ;)

Regards,
Arjen

On Tuesday, April 15, 2014 12:07:04 PM UTC+2, Arjen Ruiterkamp wrote:
Reply all
Reply to author
Forward
0 new messages