复选框 – 当多个相似字段出现在表单中时,更好的黄瓜(基于水豚)选择

(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!)

我想写一些像这样的步骤:

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

要么

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

我有以下工作,但我找到复选框(或字段)的方法对我来说似乎非常糟糕和低效:

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

有没有更好的方法来选择这些输入元素?

最佳答案
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

转载注明原文:复选框 – 当多个相似字段出现在表单中时,更好的黄瓜(基于水豚)选择 - 代码日志