watirpage-object-gem

best option for returning visible list_items in a large collection of hidden elements


I am looking for best performance input on how to obtain list_items from a unordered list. Where I only want the visible list_items returned.

The following is a sample of the html that I'm working with

<div id="queue-body">
 <ul id="queue-list">
  <li class="message" style="display: none;">oculto</li>
  <li class="message" style="display: none;">oculto</li>
  <li class="message" style="display: none;">oculto</li>
  <li class="message">vidljiv</li>
  <li class="message">vidljiv</li>
  <li class="message">vidljiv</li>
  <li class="message">vidljiv</li>
 </ul>
</div>

There are roughly 300 list_items and it is currently taking about 19 seconds to return 40 that are visible

I'm using the following in my page-object

unordered_list(:queue_list, :id => 'queue-list')
 def visible_queue_submissions
   queue_list_element.list_items.find_all(&:visible?)
 end 

This is the stack for my env

page-object 2.2.4
watir 6.10
selenium-webdriver 3.7.0

Solution

  • find_all(&:visible?) will be slow because Watir will have to make a wire call for each list item. To make it faster, you need pick a selector that doesn't require iteration. This typically means finding properties that Watir can combine into it's XPath builder.

    In this case, the visible elements do not have a style attribute. Therefore, you can use the new presence/absence locator:

    def visible_queue_submissions
      queue_list_element.lis(style: false)
    end
    

    Even with this small list, I saw the execution time from 1.26s down to 0.14s.