0
votes

I have two field-set on my page. None of them have any id or class. Now i want to fill specific field in second field-set.

Currently i a doing something like this which is not working:

within_fieldset('fieldset') do
  fill_in 'app_answers_attributes_0_answer', with: 'My First Answer'
end
click_on 'Submit'

And it is giving error:

Capybara::ElementNotFound: Unable to find fieldset "fieldset"

Any idea on how to do that?

1

1 Answers

1
votes

within_fieldset takes the id or legend text of the fieldset - http://www.rubydoc.info/gems/capybara/Capybara/Session#within_fieldset-instance_method - so it's not really surprising within_fieldset('fieldset') isn't working for you. How you can do what you want really depends on how you're HTML is structured. For instance if you're fieldsets have legends

<fieldset>
  <legend>Something</legened>
  ...
</fieldset>
<fieldset>
  <legend>Other thing</legened>
  ...
</fieldset>

You can do

within_fieldset('Other thing') do
    ...
end 

If you don't have legends, but have wrapping elements

<div id="first_section">
   ...
    <fieldset>...</fieldset>
<div>
<div id="second_section">
    <fieldset>...</fieldset>
</div>

then you can just scope using CSS to the correct fieldset

within('#second_section fieldset') do
   ...
end

If instead the fieldsets are siblings

<div>
  <fieldset>...</fieldset>
  <fieldset>...</fieldset>
</div>

then you can just scope to the second fieldset with a CSS sibling selector

within("fieldset + fieldset") do
  ...
end