capybara-webkit: automatically save a screenshot on an RSpec test failure - ruby

How can I automatically save the html and a screenshot when a test fails using capybara-webkit with Rspec? How can I execute a callback when an RSpec test fails.
Bonus points: how can I avoid getting the following error:
Capybara::Driver::Webkit::WebkitInvalidResponseError
when executing this code:
require 'capybara/util/save_and_open_page'
path = "/#{Time.now.strftime('%Y-%m-%d-%H-%M-%S')}"
png = Capybara.save_and_open_page_path + "#{path}.png"
page.driver.render Rails.root.join(png)

I have written a gem Capybara-Screenshot specifically for this, check out https://github.com/mattheworiordan/capybara-screenshot
It will automatically create screen shots on failed RSpec or Cucumber steps.

Capybara provides a function for saving and opening a screenshoot during the testing. You just need to call anywhere in your test:
save_and_open_screenshot
and it will open a picture how the test looks like at that point.
No need for any additional gems.
Capybara::save_and_open_screenshot

Found a gist that might help you https://gist.github.com/1156691

Related

RSpec, Appium: provide desired capabilities from CLI

I am using this official Appium page http://appium.io/docs/en/writing-running-appium/default-capabilities-arg/
and I want to override the capabilities which are provided in spec_helper.rb this way
rspec --default-capabilities spec/test_data/new_caps.json spec/cli_test/cli_test_spec.rb
but it is giving me error invalid option: --default-capabilities.
Appium server already running in the background.
What am I doing wrong here?

Does RSpec require openstruct?

I have some strange behaviour in a small command line ruby app that parses cron strings.
I use an OpenStruct in part of my code and I forgot to require "ostruct". My tests ran green and the code using OpenStruct is well tested.
However when I attempted to run the application 'as a user' from the command line I receive a uninitialized constant CronParser::OpenStruct error. I have added require "ostruct" and all is well again.
RSpec is bringing in OpenStruct and so my tests are not failing. But the application does not work. Clearly not ideal.
I would appreciate any thoughts on how I should be testing for this?
Thanks in advance.
T

I get the same error on terminal for different file

I am running a feature file on Cucumber and every-time I get the same error message. No matter what feature file tags I run.
Missing Examples section for Scenario Outline at
features/support/internetonly.feature:10
(Cucumber::Core::Gherkin::ParseError)
Please help.
Thanks.
Error message speaks for itself: you have scenario outline without examples table. Either include examples table, either replace Scenario Outline: with just Scenario:.

How to build a very basic Guard example?

My goal is to build a simple custom guard with Guard. The gem install and bundler install for my app went fine. My Guardfile contains:
notification :growl
guard 'eyeball' do
watch %r{^app/(.*)}
watch %r{^config/(.*)}
watch %r{^lib/(.*)}
end
Ok, next, I need to tell Guard what to do when a match happens. But I don't know where to do that. (In this case, I want to watch my application for changes and run some arbitrary code. Assume that there isn't a guard available for what I want. I want to learn how to do it myself.)
In true 'blunder and see what errors pop up next' style, when I run guard I get this error message:
ERROR: Could not load 'guard/eyeball' or find class Guard::Eyeball
ERROR: cannot load such file -- guard/eyeball
ERROR: Invalid Guardfile, original error is:
undefined method `new' for nil:NilClass
ERROR: No guards found in Guardfile, please add at least one.
Guard uses Growl to send notifications.
Guard is now watching at '/Users/my-user-name/dev/my-project-name'
So, that gives me a hint that I need to create a guard/eyeball.rb file. Maybe? But how was I supposed to know this from the documentation?
I've read (several times) the very detailed and useful Guard README but haven't found a good simple example that shows someone how to do 'just the basics' of writing your own guard. Unexpectedly, RailsCasts didn't really answer my question either: see RailsCast #264 Guard.
Did I overlook something in the Guard README? Can you help or point to a good example? Thanks!
Sweet! I just found a Wiki page on the Guard wiki titled Create a guard that answers my questions. It was not mentioned in the README, so I had to dig for it.

Selenium RC test passes when it should fail on verifyTrue(selenium.isTextPresent("string"))

I have a suite of Selenium tests that I created in the Selenium IDE and ported over to Java. In several tests I use the Java equivalent of the verifyTextPresent command to confirm some text on the page (verifyTrue(selenium.isTextPresent())).
I found an spelling error in the text on the page when running the test from the IDE, but the error was not caught when running the test via Selenium RC/TestNG. Here's an example of the code that I have and the text that is causing the problem (spelling error in bold):
Text:
Please correct the errors indicated below.
You need to add a least one restriction.
IDE:
verifyTextPresent | Please correct the errors indicated below.
verifyTextPresent | You need to add at least one restriction.
Java:
verifyTrue(selenium.isTextPresent("Please correct the errors indicated below."));
verifyTrue(selenium.isTextPresent("You need to add at least one restriction."));
Since both versions of the test have the correct text, why is the Selenium RC version not catching the error? Has anyone else had this problem?
The reason why this happens is that the test continues to run after the call to verifyTrue(). Verifications in Selenium catch the exceptions that would be thrown by a failed verification, as opposed to an assertion which throws an exception and causes the test to fail. Because verifications catch exceptions instead of throwing them, the test passes.
At the end of the test, the method checkForVerificationErrors() needs to be called to see if any of the verifications failed. If the method is not called, any verification errors will be ignored and the test will still pass (absent any other problems).
A discussion on the Selenium Google Group about the error itself is here. A discussion about the various verifications vs. assertions is here.
I had a similar problem... My workaround is to use assertTrue() instead of verifyTrue(). I hope it works for you to.

Resources