I ran into this today when writing an integration test for a JS intensive flow:
5 Capybara Debugging Tips
There’s a few good tips here: Five Capybara Hacks to Make Your Testing Experience Less Painful which I’ll summarize:
- Run a script:
page.execute_script("<some js code>")
- Tails the test log
tail -f log/test.log
- Use pry to get a view into the browser:
puts current_url; require 'pry'; binding.pry
Definitely take a look at this article: Interactive Debugging With Pry. The key thing to know is thatctrl-d
makes your test continue and that the you can visit the URL that prints in Chrome. If your test depends on a - Make Sure DatabaseCleaner Plays Nice with PhantomJS (check the article mentioned above and my article: Capybara, PhantomJs, Poltergeist, and Rspec Tips
- Split VCR/Webmock Specs Into a Separate Rake Task From Your JavaScript Tests. I haven’t tried this one yet.
Logging into Capybara/PhantomJs Browser with a User and Password
Here’s a nifty extension to step 3, as my tests typically depend on a logged in user.
First, I define this method for login_as_user, in spec/support/capybara.rb
include Devise::TestHelpers
# gives us the login_as(@user) method when request object is not present
Warden.test_mode!
def login_as_user(user = nil)
current_user = user || Factory.create(:user)
login_as(current_user, :scope => :user)
@current_user = current_user
end
Then I create this “Live Template” in RubyMine with abbreviation pryr
:
puts "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
puts "user: #{@current_user.email}, password: #{@current_user.password}"
puts current_url
puts "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
pause
See below for the definition of pause
or see this.
And then when I put this in a test, I get the URL, as well as the user/password to troubleshoot the issue.
Much better than save_and_open_page
This can be much better than using save_and_open_page
or rendering the page. Not only can you inspect variables, but you can test your your capybara commands on the browser page. Try doing a click_link "some link"
and your Chrome browser move as if you clicked on the screen!
And then you’ll be all set to efficient write more integation tests!
UPDATE: 4/9/2015: You want to now use byebug
rather than binding.pry
.
UPDATE: 2/10/2016: See thread below on using pause
or see this. You should use driver :selenium
if you want to pause, and you don’t need to login.