skip to Main Content

I have an html page with several of a certain type of element contained on a single page. How would you go about counting how many of these certain elements are present on the page with groovy? HTML looks something like this (with some extra divs between elements for styling purposes), would need to count how many ‘custom-list-items’ are present.

<div>
     <div-list-holder>
          <list>
               <custom-list-item>
                    list item 1
               </custom-list-item>
               <custom-list-item>
                    list item 2
               </custom-list-item>
               <custom-list-item>
                    list item 3
               </custom-list-item>
          </list>
     </div-list-holder>
</div>

Obviously the easy way to do this would be to count the length of the list/collection that holds all of the elements, but its a bit tricky for me with the nested elements. Looking for some type of WebUI.countOfElementsPresent(String elementName). I really don’t even need to know exactly how many there are, I just need to verify there are more than 1 of this specific element on the present page.

2

Answers


  1. Use Jsoup for counting number of tags/elements from html content as below-

         @Grab('org.jsoup:jsoup:1.14.3')
        import org.jsoup.Jsoup
        
        def html = '''
        <div>
             <div-list-holder>
                  <list>
                       <custom-list-item>
                            list item 1
                       </custom-list-item>
                       <custom-list-item>
                            list item 2
                       </custom-list-item>
                       <custom-list-item>
                            list item 3
                       </custom-list-item>
                  </list>
             </div-list-holder>
        </div>
        '''
        
        def elementName = "custom-list-item"
        
        def document = Jsoup.parse(html)
        def count = document.select(elementName).size()
        
        println "Number of $elementName elements: $count"
    
    def hasMultipleOccurrences = count > 1
    
    println "Is more than once: $hasMultipleOccurrences"
    
    Login or Signup to reply.
  2. Example using groovy alone.

    import groovy.xml.XmlSlurper
    
    def html =
            '''<div>
                 <div-list-holder>
                      <list>
                           <custom-list-item>
                                list item 1
                           </custom-list-item>
                           <custom-list-item>
                                list item 2
                           </custom-list-item>
                           <custom-list-item>
                                list item 3
                           </custom-list-item>
                      </list>
                 </div-list-holder>
            </div>'''
    
    def elementCount( markup, elementName ) {
        new XmlSlurper().parseText(markup).'**'.findAll { node -> node.name() == elementName }.size()
    }
    
    assert 3 == elementCount( html, 'custom-list-item')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search