Search code examples
page-object-gem

using page-object-gem obtain count of nested elements


What is the most efficient way to obtain a count of nested elements using the page-object-gem

Given some sample HTML

<ul :id="templateFields">
    <li class="review">Review</li>
    <li class="review">Assign</li>
    <li class="generate">Retrieve</li>
</ul>

Where we have our page-object defined as

class MyPage
  include  PageObject

  unordered_list(:steps, :id => 'templateFields')
end

Looking for the count of the nested ordered_list elements?


Solution

  • An unordered list element has a list_items method that returns its li elements. You could count how many elements are returned:

    page.steps_element.list_items.count
    #=> 3
    

    If you do this frequently, you may be better of defining an accessor for the list items themselves.

    class MyPage
      include PageObject
    
      unordered_list(:steps, :id => 'templateFields')
      list_items(:step_item, :css => '#templateFields li')
    end
    

    Then you would simpliy call:

    page.step_item_elements.count
    #=> 3