How to count list items in an unordered list? (RSpec / Capybara)
On my site, I have this list:
<ul class="test">
<li class="social_1"></li>
<li class="social_2"></li>
<li class="social_3"></li>
<li class="social_3"></li>
</ul>
My question is: how can I read li in the ul class I tried this:
my_ul = page.find("ul[class='test']")
my_ul.each do |li|
pp li['class']
end
but that will not work.
Is there a way to do something like I encoded above?
+4
3 answers
Assuming the parent element is ul with id = parent .. you can do it like this
list = Array.new
list = find('#parent ul').all('li')
now you can get the list size just
list.size
and you can benefit from the fact that all li in the array will also collect text in every li element, for example
list = find('#parent ul').all('li').collect(&:text)
+11