Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

better cucumber (Capybara-based) selection when multiple similar fields appear in a form

(Some background if it helps: our Rails app has a bunch of potentially repetitive dynamically-generated forms using double-nested fields, with ids in the pattern foo_attributes_0_bar_attributes_0_bat_12, where basically the first two numbers increment based on how many repetitions of the nested form there are, and the last number is the id of the particular "bat" object. This makes selection in Cucumber for testing difficult!)

I am trying to write some steps like this:

When I check the 1st checkbox with the value "Bat 12"

or

When I check the 3rd checkbox with id matching "bar_attributes_bat"

I have the following working, but my method of finding the checkboxes (or fields) seems pretty crappy and inefficient to me:

When /^I check the (\d+)(st|nd|rd|th) checkbox with the value "([^"]*)"$/ do |number, junk, value|
  check(page.all("input").select {|el| el.node['value'] == value}[(number.to_i-1)].node['id'])
end

When /^I check the (\d+)(st|nd|rd|th) checkbox with id matching "([^"]*)"$/ do |number, junk, id_string|
  check(page.all("input").select {|el| el.node['id'] && el.node['id'].match(/#{id_string}/)}[(number.to_i-1)].node['id'])
end

Is there a better way to select these input elements?

like image 989
shalott Avatar asked Jan 20 '23 15:01

shalott


1 Answers

When /^I check the (\d+)(st|nd|rd|th) checkbox with the value "([^"]*)"$/ do |index, junk,   value|
  page.all("input[value='#{value}']")[index.to_i-1].check
end
like image 98
Andy Waite Avatar answered Jan 30 '23 20:01

Andy Waite