Search code examples
rubytestingpage-object-gem

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


I have a list of checkboxes defined by their IDs using the page-object gem. A sample follows:

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'

The way I see it; there must be a more efficient way to gather all of the checkbox elements inside of a particular div.

I'm looking for a way to essentially do

checkbox_objects.each do |checkbox|
  checkbox.check
end

But I'm not sure how to define checkbox_objects. Is anyone knowledgable of a way to gather all of the checkboxes inside a particular element and put them into an array? Thanks in advance.


Solution

  • The checkboxes accessor method (as opposed to checkbox) is used to create an Array of checkboxes.

    Assuming the page HTML is structured like:

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

    You could specify a block that locates all checkboxes in say the div with id "container":

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

    Alternatively, you may find a CSS-selector or XPath more concise:

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

    Either way, the page object gets a <name>_elements method that returns the matching checkboxes:

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

    Or as a one-liner:

    page.thumb_elements.each(&:check)