• <...">

    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
    source share
    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
    source

    RSpec 3 Capybara:

    it "should have 4 li elements" do
       expect(find('ul.text')).to have_selector('li', count: 4)
    end
    

    : https://github.com/jnicklas/capybara#querying

    +5

    Use page.all("ul.test li").size

    +1
    source

    Source: https://habr.com/ru/post/1543972/


    All Articles