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

sha*_*ott 4 checkbox cucumber capybara

(有些背景如果有帮助:我们的Rails应用程序有一堆使用双嵌套字段的可能重复的动态生成表单,模式中的id为foo_attributes_0_bar_attributes_0_bat_12,其中前两个数字基本上是根据嵌套表单的重复次数而增加的是,并且最后一个数字是特定"bat"对象的id.这使得在Cucumber中进行选择以进行测试很困难!)

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

When I check the 1st checkbox with the value "Bat 12"
Run Code Online (Sandbox Code Playgroud)

要么

When I check the 3rd checkbox with id matching "bar_attributes_bat"
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

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

And*_*ite 9

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
Run Code Online (Sandbox Code Playgroud)