Capybara & Cucumber - feature with expected controller exceptions fails - ruby-on-rails

I recently switched to capybara 2.5.0 & webkit 1.7.1.
I have a cucumber feature in which I want to check my javascript handling of a failed ajax request.
With previous capybara version:
I stubbed an API request to respond with 400 thus the controller action raised this exception (RestClient::BadRequest) without rescuing it.
My javascript was showing a custom message in case the ajax request failed.
The feature didn't fail when the exception was raised by the controller, the flow was continuing normally. The ajax request failed and my js was handling it as expected.
With the new capybara version: The feature fails when the exception takes place at the controller level.
I don't want the feature to stop at that level but to continue with the error response to the browser so I can handle the error with my js.

I would guess this behavior change isn't because of the capybara update, but rather because you moved the web_console gem out of the test group. That meant exceptions were never actually being raised in the server because web_console caught them all. Now that exceptions aren't being caught Capybara is displaying them. Capybara has the Capybara.raise_server_errors setting to enable/disable that behavior.
Capybara.raise_server_errors = false

Related

Rails: Can I use the ruby gem better_errors with any frontend framework?

I have a backboneJS app working with the gem rabl on Ruby on Rails
Better error only shows ruby errors (or rather server side errors), if it's a javascript error it will occur on the browser not the server, if the assets have errors it should appear in the precompilation, so you probably are covered too.
Anyways, using better errors has no downsides, cause the cases that won't work with better errors won't work with the basic error pages without better errors.
PS: If you want to debug an ajax request you can try opening the browser console and click the request that has an error and choose something that means close to 'open this request in a new tab'

Capybara specs fails after opal js update?

Previously I used to load opal.js, opal-parser.js, opal-jquery.js(version 0.3x). Now I added gem opal and opal-rails(version 0.6.2) instead of js files.
Previously my whole test suit(capybara + rspec) used to passed but after update capybara specs start failing. Sometime it behave wired(i.e passed in firefox but fail in chrome).
One of the example error is
unknown error: Runtime.evaluate threw exception: RangeError: Maximum call stack size exceeded
It is said that there is compatibility issue with opal and rspec.
I will be grateful if someone help me fixing this issue.
I thinks its a known bug in the chrome webdriver:
https://code.google.com/p/chromedriver/issues/detail?id=683

Why are my Javascript-dependent feature specs passing when not in Javascript mode?

I have non-Javascript specs and Javascript feature specs that use capybara/capybara-webkit.
I have some tests that I expected to fail when in non-Javascript mode, but they are passing. I've tried the functionality in my browser with Javascript turned off, and the functionality does indeed not work as expected. But in Capybara, it works.
I've also tried adding js: false to the individual test, to make sure there wasn't something hidden in my config turning it on. The spec still passes.
The selenium gem is not included in my Gemfile.
I see two possibilities: your tests are correct but there might be some reason why they pass unexpectedly, or your tests are incorrect and are passing for the wrong reason when you run them in Javascript.
A few suggestions for how to debug that apply to both possibilities:
Use a debugger. (If you have, let us know what you learned.)
Look at the Rails log from a single run of a spec to see what controller actions are called and in what order.
Use save_and_open_page to snapshot your page and see whether your spec should pass on what you're looking at.
Double-check your tests when run with Javascript:
Run them with the poltergeist driver instead of the capybara-webkit driver. It gives better error messages than capybara-webkit and can be configured to report Javascript error messages.
Use save_screenshot to take an actual screenshot of the page and verify that the spec is not passing for a spurious reason, perhaps because some content is off the screen.
If you want to be sure whether rspec is running Javascript, use ps to see whether there is a capybara-webkit or phantomjs process running.

Cucumber only showing Capybara errors when there's an error in a Controller

I keep having an annoying issue with Cucumber. If there are any errors in the Controller, it only shows the errors raised by capybara. I'm sure that's not the way it used to work?
For example, if an exception was raised, the only output I see is:
expected to find css "h1" with text "blah"
In order to actually see the error, I have to puts page.body in order to see the Controller error
Is this expected behaviour?
It is the expected behavior. Capybara is acting in the role of the user and responding to what it sees back from the server. Yes, it is annoying and makes failures hard to debug.
After you find the problem using tricks like puts page.body you should improve your lower-level test coverage to detect the cause of the error, giving a more informative failure, and then fix the problem. The better your lower-level test coverage is, the less often you'll have an error in Ruby code when running a full-stack test.
By the way, I would characterize this as an issue with full-stack testing (e.g. Capybara) and not a Cucumber-related issue. Cucumber can run tests that are not full-stack, and you can run full-stack tests outside of Cucumber (e.g. by using Capybara in RSpec)

jQuery document.ready not called during rails integration test

I have an integration test that fails for a page that depends heavily on javacript. The same page runs just fine from the browser.
Simplifying the test to a bare minimum I found out that just testing for the presence of a selector that is added by javascript after the page load, would fail.
After precompiling the test assets and using save_and_open_page I found that the handler for the jQuery ready event is not running during the integration test.
I didn't find any references to this problem, so I guess I'm doing something wrong.
Can anyone help me figuring this out?
I'm using using rails 3.2.11, RSpec 2.13.0, Capybara 2.0.3 and capybara-webkit 0.14.2
By default Rails uses webdriver which doesn't handle JS. If you want JS support you'd need to use one of the JS-aware drivers, like Selenium (which runs full featured browser), capybara-webkit or poltergeist (which are headless webkit browsers). There are many others but these three are most popular.
I solved this problem for me by examining my AJAX request. The problem was that I was requesting a 'http://..." instead of '/' of my page. This meant that it raised a CORS ('ajax request from outside the domain') even though the request looked like it was coming from inside the domain.
My original symptom was 'javascript-inserted HTML elements are not on the page when testing using Capybara with Poltergeist or Selenium (but are on the page when running the application).'

Resources