如何使用页面对象迭代一系列复选框?

How to iterate over series of checkboxes using page-object?

我有一个使用页面对象 gem 由其 ID 定义的复选框列表。示例如下:

checkbox        :main_product,                      id:             '19437578_58659806'
checkbox        :thumb_one,                         id:             '19437579_58659811'
checkbox        :thumb_two,                         id:             '19437580_58659812'
checkbox        :thumb_three,                       id:             '19437581_58659813'
checkbox        :thumb_four,                        id:             '19437582_58659814'
checkbox        :thumb_five,                        id:             '19437583_58659815'
checkbox        :thumb_six,                         id:             '19437584_58659816'
checkbox        :thumb_seven,                       id:             '19437585_58659817'
checkbox        :thumb_eight,                       id:             '19437586_58659818'
checkbox        :thumb_nine,                        id:             '19437587_58659819'
checkbox        :thumb_ten,                         id:             '19437588_58659820'
checkbox        :tt_one,                            id:             '19437594_58659842'
checkbox        :tt_two,                            id:             '19437595_58659843'
checkbox        :tt_three,                          id:             '19437596_58659844'
checkbox        :tt_four,                           id:             '19437597_58659845'
checkbox        :tt_five,                           id:             '19437598_58659846'
checkbox        :tt_six,                            id:             '19437599_58659847'
checkbox        :tt_seven,                          id:             '19437600_58659848'
checkbox        :tt_eight,                          id:             '19437601_58659849'
checkbox        :tt_nine,                           id:             '19437602_58659850'
checkbox        :tt_ten,                            id:             '19437603_58659851'

我的看法;必须有一种更有效的方法来收集特定 div 内的所有复选框元素。

我正在寻找一种方法来做

checkbox_objects.each do |checkbox|
  checkbox.check
end

但我不确定如何定义 checkbox_objects。有没有人知道将所有复选框收集在特定元素内并将它们放入数组中的方法?提前致谢。

checkboxes 访问器方法(相对于 checkbox)用于创建 Array 个复选框。

假设页面 HTML 的结构如下:

<div id="other">
  <input type="checkbox">
  <input type="checkbox">
</div>
<div id="container">
  <input type="checkbox">
  <input type="checkbox">
</div>

您可以指定一个块来定位所有复选框,例如 div 中的 ID "container":

checkboxes(:thumb) { div_element(id: 'container').checkbox_elements }

或者,您可能会发现 CSS-选择器或 XPath 更简洁:

checkboxes(:thumb, css: 'div#container input[type="checkbox"]')

无论哪种方式,页面对象都有一个 <name>_elements 方法,returns 匹配的复选框:

page.thumb_elements.each do |checkbox|
  checkbox.check
end

或作为单行:

page.thumb_elements.each(&:check)