How can I see what capybara found in a failing cucumber step? - ruby-on-rails

I started migrating from cucumber + webrat to cucumber + capybara. Now the behavior of "I should see " seems to be somewhat different. Most of these fail now, although I didn't change anything on the page. I replaced the snippet that should be found with some stuff that is on every page and for some text it works and for other text it doesn't. I can't find any pattern in what is found in the page's content and what is not.
Webrat used to print what the page content is that it found, in case it did not contain the required phrase. Is there anyway to have capybara show what text it got from the page in which it tried to find the text?

Then show me the page calls webrat/capybara's underlying save_and_open_page method. Found that useful when working with steak.

Try adding this step:
Then show me the page

If you want to have the browser open the page when the page fails you use the 'launchy' gem.
Add it to your gem file, and then in /features/support create a file called debugging.rb with contents:
After do |scenario|
save_and_open_page if scenario.failed?
end

If you're using Javascript or Ajax in your pages and want to see what's going on, I've found that the Poltergeist driver is very good at letting you get into the DOM and find out what's going wrong.
If you setup your Capybara driver with the remote debugging option:
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, inspector: true)
end
You can then put the following line in your steps:
page.driver.debug
Which fires up a new Chromium browser with the current DOM state set, letting you get at the console. (On my version of Linux, I had to symlink chromium to chromium-browser but it otherwise worked fine).
Source Info: http://jonathanleighton.com/articles/2012/poltergeist-0-6-0/

Then show me the response didn't work for me with cucumber 1.1. I found useful to write a step using capybara's command:
print page.html
This outputs the current state of the DOM

You could also use "Then show me the response" which outputs the HTML to the console if you don't want to use a browser.

You could always have it take a screen shot when something failed. I debug a LOT of failing features that way.

Related

RSpec / Capybara debugging

I'm trying to test the ability to vote on an event. I've created the event via factory_girl. When the user participated to the event, he should have the ability to vote.
The vote button is visible to capybara as well as visible to me when I manually test for its presence in the browser. When I use click_on "vote", there is no error. Now comes the weird thing. When I manually test the vote button via the browser, I now see the vote form and have a /vote/... path in my browser. When I use click_on "vote" in Capybara and check for the path, the path equals the start page.
I've tried everything I can think of to figure out why Capybara is hitting the wrong path.
My question is, is there a possibility to see some 'advanced' logging or something else? Like why a redirect happened or something else?
You can use byebug to step through your spec. Also, I find chromedriver to be very useful. It runs your tests in chrome environment so you can eyeball your test and confirm that it is doing what it is supposed to.
Byebug:
https://github.com/deivid-rodriguez/byebug
Chrome driver:
http://collectiveidea.com/blog/archives/2011/09/27/use-chrome-with-cucumber-capybara/

save_and_open_page not working with capybara 2.0?

I just had my capybara updated to 2.0, and all of my save_and_open_page calls return an html page without styling. It has the style sheets links properly at the top of the page. When I revert back to capybara 1.3, the styles work again. Anyone know how to fix this, and/or what the problem is?
thanks,
Dave
Update 1.
hack:
I have found a reasonable way to get around the problem following the links recommended by simonmorley:
Capybara Webkit problem
which points to: capybara-screenshot
It involves using the capybara-screenshot gem to get a screenshot of what the page looks like. When you combine it with the save_and_open_page, which generates the html, you can see what it looks like, and see the HTML.
save_and_open_page
Capybara::Screenshot.screenshot_and_open_image
If I could get Capybara::Screenshot.screenshot_and_save_page to work, then I think that I might have a solution (if the name implies the action I think it does). However, when I try it, I get cannot load such file -- capybara/util/save_and_open_page
Update 2.
!! wait, the screenshot_and_open_image is not working now (no png image created or displayed). !!
Update 3.
Test App:
I created a test app, and posted it at test_capybara_screenshot on Github.
When I run this test app in development, the page come up with the blaring red background style displayed. When I run the tests, the only thing that works for me is the save_and_open_page, except there is no styling.
Note, when I run any tests, I get the following warning:
WARNING: Nokogiri was built against LibXML version 2.7.8, but has dynamically loaded 2.7.3
Details of the rspec test:
The save_and_open_page comes up with a boring white background (no
styling).
the screenshot_and_open_image returns with the error:
Rack::Test capybara driver has no ability to output screen shots. Skipping.
Failure in opening /~/Documents/experiments/test_capybara_screenshot/tmp/capybara/screenshot-2012-11-26-07-48-29.png with options {}: No application found to handle '/~/Documents/experiments/test_capybara_screenshot/tmp/capybara/screenshot-2012-11-26-07-48-29.png'
The screenshot_and_save_page returns:
An error occurred in an after hook
LoadError: cannot load such file -- capybara/util/save_and_open_page
occurred at ~/.rvm/gems/ruby-1.9.3-p125#global/gems/activesupport-3.2.6/lib/active_support/dependencies.rb:251:in `require'
Try adding the following to your gem file
group :test do
gem "capybara"
gem "launchy"
end
save_and_open_page won't work without this I don't believe.
-- Updated --
I've tested this for you and when I used the save and open method, I get the same error. In my set up, this is because my page is referencing:
<link href="/assets/application.css" media="screen" rel="stylesheet" type="text/css" />
This happens in Firefox, Chrome and Safari. According to the inspector, none of the files are found.
I haven't looked into this in too much detail and don't have an answer why it's not working. However there are a number of people struggling with this also:
https://github.com/jnicklas/capybara/issues/485
And, a bit more information about capybara and asset precompilation:
https://github.com/jnicklas/capybara/pull/609
And what might be a fix here:
https://groups.google.com/forum/#!msg/ruby-capybara/SBCor8UUj7w/hv97EgUQ1P4J
However, if you just want to see your page you could try this: delete scrap save_and_open_page and change your tests to use javascript. This way, my pages open fine.
require 'spec_helper'
describe "Foo" do
it "should fail if Bar is lactose intolerant", :js => true do
# .....
end
end
Try that and let me know if that helps.
response (From Taylored Web Sites):
I see the page for a few seconds, then it goes away. Is there a way for the browser window to not close?
I was able to fix the problem with save_and_open_page after some blog browsing and digging. I have dropped the capybara_screenshot gem from my project. You can see my working code on my github test_capybara_screenshot repository. The solution that I figured out uses some pointers that I found on the capybara github site.
Assumptions:
You are using rspec for testing.
Your app is already configured for using the asset pipeline.
The first thing that I do is set it up so that assets are precompiled into a test directory. I do this by adding the following code into the spec/spec_helper.rb file within the RSpec.configure loop:
config.before (scope = :suite) do
%x[bundle exec rake assets:precompile]
end
I specify where the assets are pre-compiled to in the config/environments/test.rb file:
config.assets.prefix = "assets_test" # place test assets in public/assets_test directory
config.action_controller.asset_host = "file://#{::Rails.root}/public"
This makes it so that the test assets are independent of the development assets, and are only generated during a test suite run.
If you do this, you will probably want to have git ignore the /public/assets* directories.

Rails and Selenium: how to stop/pause execution of a test in the browser?

I'm having a hard (but very interesting time) diving into Behavior Driven Development using Cucumber, RSpec, Selenium, and Rails.
I have my setup ready for testing with Selenium, and it's funny to watch Firefox pop up and run automatically through my scenarios. But one thing I'd like to do is pause or stop execution at a certain point, so I can inspect what Selenium sees at a certain point.
I know of the save_and_open_page command, but this only shows me plain HTML without formatting. So maybe there is a stop_execution method or something that stops Selenium without closing the browser?
Install pry, then put binding.pry in your test where you want it to pause. When you're done, press Ctrl+D or type exit in the REPL that gets opened to continue execution.
or just:
visit '/'
sleep(inspection_time=5)
visit '/dreamland'
All the answers need installing new gems or even setting a sleep which is not the best approach. You can put this line anywhere in you step:
ask "Continue?"
It will stop execution until you enter y (Yes)
So, for example it would look like this:
expect(page).to have_button('Submit')
ask "Continue?"
click_button('Submit')
Use Debugger where you want to stop/pause the execution.
or
In Selenium IDE you can right click on the commands line and you can select Set/Clear Start point to stop/pause the execution.
Okay, I got it working by installing ruby-debug19 (for Ruby 1.9.3), and then just setting a breakpoint somewhere in a Cucumber step.
http://rails.vandenabeele.com/blog/2011/12/21/installing-ruby-debug19-with-ruby-1-dot-9-3-on-rvm/
Another option is to use the Capybara-firebug gem which adds a "Then stop and let me debug" step which basically seems to do the same (I don't know whether it relies on the ruby-debug gems).
try to use selenium.sleep(ms)
this will make the test execution wait for the specified amount of time
You can use do this without installing any gems in rails 5 or above by using byebug. Just type byebug on the line that you want the test to pause at.
Example:
visit post_url(id: posts(:one).id)
byebug
click_on "...", match: :first
Doing this will pause the test after the new page loads but before the next button is clicked. If using an older version of rails you may have to install the byebug gem but it is useful for troubleshooting and I recommend using it regardless.

How can I simulate the browser back button in Capybara?

We have a issue on our e-commerce site where users occasionally hit "checkout" twice and have their card charged twice.
It's a common enough bug and easy to fix, but I'd like to test the solution in our capybara setup. Once I've called click_button('checkout'), is it possible for me to pretend I'm a user hitting the browsers back button and then call click_button('checkout') a second time?
You may want to try:
When(/^I go back$/) do
page.evaluate_script('window.history.back()')
end
This will require running the senario in a javascript capable driver (selenium/celerity/akephalos)
You can use page.driver.go_back, if you are using webkit as your capybara javascript driver via the capybara-webkit gem. Also requires :js => true for the scenario.
At least with capybara 2.10 and selenium-webdriver 2.53 this works:
When(/^I go back$/) do
page.go_back
end
It's basically a shortcut for jbarr's answer. For details more see the capybara documentation on go_back .
BTW: The counter part is page.go_forward.
I've used this method in Webrat. I'm sure something similar for Capybara would work.
When(/^I go back$/) do
visit request.env['HTTP_REFERER']
end
Side note: the "redirect_to :back" method didn't work for me for whatever reason.
Thanks! This question and answer helped me a lot!
Just to add to #Jake Mallory's answer, selenium is now part of capybara and you can fairly easily run javascript in the test by adding :js => true (and possibly a couple more tweaks) as described in these two tutorials:
http://www.opinionatedprogrammer.com/2011/02/capybara-and-selenium-with-rspec-and-rails-3/
http://railscasts.com/episodes/257-request-specs-and-capybara?view=asciicast

How do you actually see the html output during a functional test in rails

One of my assert_selects is driving me up the wall. Its failing and of course its telling me what it expected but Id like to see what was actually there on that page when it rendered to track down this bug.
Ive already tried using my browser and the app is behaving as it should but thats using development data.
Is there a command i can use to print the html to console or my test log?
Just do
puts #response.body
right before the problematic assertion and you should see the thing.
Webrat does this very thing with the command save_and_open_page.
on a Rails 5.x environment, using parsed_body worked for me, while just plain body did not:
puts #response.parsed_body

Resources