nesting selectors in view specs with rails and capybara

509 views
Skip to first unread message

jared

unread,
Dec 13, 2012, 11:32:28 PM12/13/12
to rs...@googlegroups.com
I'm having trouble finding a way to check for nested selectors in an view spec with capybara, which according to this issue doesn't accept a block for have_selector like webrat.

There were a couple workarounds with Object#tap given, but neither worked for me (with capybara 2.0.1 and rspec 2.12.0), and thread hasn't been updated in a year.

For clarity, here's what didn't work:

...
render
rendered.find("form") do |form|
  form.should have_selector("input")
end
...
  
Nor did wrapping rendered with Capybara.string as suggested at the bottom of the thread.

Seems like a common scenario, can someone explain the correct way to do this?

Thanks!

David Chelimsky

unread,
Dec 14, 2012, 7:02:45 AM12/14/12
to rs...@googlegroups.com
On Fri, Dec 14, 2012 at 2:32 AM, jared <ja...@yourelevation.com> wrote:
> I'm having trouble finding a way to check for nested selectors in an view
> spec with capybara, which according to this issue doesn't accept a block for
> have_selector like webrat.
>
> There were a couple workarounds with Object#tap given, but neither worked
> for me (with capybara 2.0.1 and rspec 2.12.0), and thread hasn't been
> updated in a year.
>
> For clarity, here's what didn't work:
>
> ...
> render
> rendered.find("form") do |form|
> form.should have_selector("input")
> end
> ...

There's no direct support for nesting, but you can do this:

rendered.should have_selector("form input")

HTH,
David

>
> Nor did wrapping rendered with Capybara.string as suggested at the bottom of
> the thread.
>
> Seems like a common scenario, can someone explain the correct way to do
> this?
>
> Thanks!
>
> --
> You received this message because you are subscribed to the Google Groups
> "rspec" group.
> To post to this group, send email to rs...@googlegroups.com.
> To unsubscribe from this group, send email to
> rspec+un...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/rspec/-/7w5QqXy79y8J.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

jared

unread,
Dec 14, 2012, 1:49:30 PM12/14/12
to rs...@googlegroups.com
Thanks for the reply, at least I can stop looking for a way to do that now.

I had been doing what you suggested, but when checking for a series of elements under a selector it would be nice to do this:

  rendered.should have_selector("form[action='#{new_account_path}']") do |form|
     form.should have_field("user_login", :type => text)
     form.should have_field("user_password", :type => text)
     form.should have_field("user_password_confirmation", :type => text)
    # ... etc ...
  end

rather than this:

  rendered.should have_selector("form[action='#{new_account_path}'] input#user_login[type='text']")
  rendered.should have_selector("form[action='#{new_account_path}'] input#user_password[type='password']")
  rendered.should have_selector("form[action='#{new_account_path}'] input_user_password_confirmation[type='password']")

Doesn't feel right, you know?

David Chelimsky

unread,
Dec 14, 2012, 1:51:45 PM12/14/12
to rs...@googlegroups.com
On Fri, Dec 14, 2012 at 4:49 PM, jared <ja...@yourelevation.com> wrote:
> Thanks for the reply, at least I can stop looking for a way to do that now.
>
> I had been doing what you suggested, but when checking for a series of
> elements under a selector it would be nice to do this:
>
> rendered.should have_selector("form[action='#{new_account_path}']") do
> |form|
> form.should have_field("user_login", :type => text)
> form.should have_field("user_password", :type => text)
> form.should have_field("user_password_confirmation", :type => text)
> # ... etc ...
> end
>
> rather than this:
>
> rendered.should have_selector("form[action='#{new_account_path}']
> input#user_login[type='text']")
> rendered.should have_selector("form[action='#{new_account_path}']
> input#user_password[type='password']")
> rendered.should have_selector("form[action='#{new_account_path}']
> input_user_password_confirmation[type='password']")
>
> Doesn't feel right, you know?

Please file a feature request at https://github.com/rspec/rspec-rails/issues
> https://groups.google.com/d/msg/rspec/-/x9RJfQKx1RsJ.

jared

unread,
Dec 14, 2012, 1:57:25 PM12/14/12
to rs...@googlegroups.com
Will do. Is this a feature request for rspec-rails or is it an issue with Capybara's have_selector? not taking a block, which Jonas says he's not going to do?

David Chelimsky

unread,
Dec 14, 2012, 2:00:18 PM12/14/12
to rs...@googlegroups.com
On Fri, Dec 14, 2012 at 4:57 PM, jared <ja...@yourelevation.com> wrote:
> Will do. Is this a feature request for rspec-rails or is it an issue with
> Capybara's have_selector? not taking a block, which Jonas says he's not
> going to do?

Ah, good point. It does belong in Capybara, so if Jonas won't make it
happen (or accept a pull request that does) you're pretty much out of
luck - unless you figure out a way to do it and release your own gem
:)
> https://groups.google.com/d/msg/rspec/-/d9oxtLUrW7oJ.
Reply all
Reply to author
Forward
0 new messages