Search code examples
rubywatirwatir-webdriverpageobjectspage-object-gem

How to count number of matches using page object?


My application has a list and it's size changes very regularly. So I need to get the li count before starting the test cases. So this is how I used to get the count

b.lis(:xpath => "//ul[@id='global_nav']/li[contains(@id, 'nav-')]").count

Now I need to achieve this in page object way. I declared the list in my page file as below.

list_item(:global_nav_count, :xpath => "//ul[@id='global_nav']/li[contains(@id, 'nav-')]")

If I try to get count as below

page.global_nav_count_element.count

It returns method not found error. Can any one suggest any solution to achieve this in page object way ?


Solution

  • The list_item accessor is similar to Watir's li method in that it returns the first matching element. If you want a collection of elements, you need to tell the page object.

    The accessor for a collection of li elements is list_items (note the plural):

    list_items(:global_nav_count, :xpath => "//ul[@id='global_nav']/li[contains(@id, 'nav-')]")
    

    The method created to access the collection is also pluralized:

    page.global_nav_count_elements.count