nesting selectors in view specs with rails and capybara

Showing 1-6 of 6 messages
nesting selectors in view specs with rails and capybara jared 12/13/12 8:32 PM
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!
Re: [rspec] nesting selectors in view specs with rails and capybara dchel...@gmail.com 12/14/12 4:02 AM
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.
>
>
Re: [rspec] nesting selectors in view specs with rails and capybara jared 12/14/12 10:49 AM
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?
Re: [rspec] nesting selectors in view specs with rails and capybara dchel...@gmail.com 12/14/12 10:51 AM
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.
Re: [rspec] nesting selectors in view specs with rails and capybara jared 12/14/12 10:57 AM
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?
Re: [rspec] nesting selectors in view specs with rails and capybara dchel...@gmail.com 12/14/12 11:00 AM
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.