Migrating from Webrat to Capybara
This is a collection of tips on how to migrate from Webrat to Capybara using RSpec.
Made a library? Written a blog post? Found a useful tutorial? Share it with the Ruby community here or just enjoy what everyone else has found!
This is a collection of tips on how to migrate from Webrat to Capybara using RSpec.
Comments
I tried to post this comment on the blog, but that didn’t work unfortunately. So posting this here in the hope that someone sees it:
Thank you for compiling your notes, unfortuantely quite a bit of this information is inaccurate. I feel like I need to set the record straight, so that others who come here aren’t misled by some of these examples.
From the top:
this doesn’t work:
Capybara has no response object. Instead you would have to do something like:
page
is a shorthand for accessing the current Capybara session.same here, actually even though it is undocumented, Capybara supports the option :with, to select elements by label.
but this selector can be more elegantly written as:
in Capybara.
Be careful with this one:
If there are multiple divs with class container on the page, this will not work as you expect. within in Capybara differs from Webrat in that it only matches on the first found element, not on all found elements. This might be a better example:
Clearly there will only be one div with id=”sidebar” on the page. This is what within is meant for. Personally I feel there is no point in testing for the fact that a link is nested in a particular generic div. If it’s not unique, it’s not worth checking for. If you have to, though, you can achieve the same thing with standard enumberable methods:
not super elegant though.
Finally, if you do this:
You cannot select the driver with
:mobile => true
, you will need to do:driver => :mobile
. Capybara only registers the special:js => true
option@Jonas Thanks for the comment, and sorry I just noticed it :s - Regarding the response object, I was referring to the response object that comes with RSpec, but if we’re not using RSpec it would not work. - Didn’t know about :with. That’s why I love Capybara, there is a lot of flexibility, though it is hard to find :) - For driver selection, I guess it must be an RSpec thing, cause it is working for me.
Thanks again for clearing things up. I will update the post.
Post a comment