SHARE
TWEET

Untitled

a guest Jan 17th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def html = '''
  2.   <html>
  3.     <ul>
  4.       <li>item 1</li>
  5.       <li>item 2</li>
  6.       <li>item 3</li>
  7.       <li>item 4</li>
  8.     </ul>
  9.   </html>'''
  10.  
  11. def htmlNode = new XmlParser().parseText html
  12.  
  13. assert htmlNode.ul.li.size() == 4
  14.    
  15. import groovy.util.*
  16. import org.jsoup.*
  17. import org.jsoup.nodes.*
  18. import org.jsoup.select.*
  19.  
  20. class HtmlTester extends GroovyTestCase {
  21.     // returns true if fragment has:
  22.     // <div class='list'> <ul> <li> ... </li> </ul> </div>
  23.     def verifyHtml(String htmlFragment) {
  24.         Document doc = Jsoup.parse(htmlFragment)
  25.         Elements divs = doc.select("div.list ul li")
  26.         boolean result = (divs.size() > 0)
  27.  
  28.         return result
  29.     }
  30.  
  31.     void testDivNoClass() {
  32.         def htmlDivNoClass = "<div><ul><li>list 1</li></ul></div>"        
  33.         assertFalse verifyHtml(htmlDivNoClass)
  34.     }
  35.  
  36.     void testDivNoUl() {
  37.         def htmlDivNoUl = "<div class='list'></div>"        
  38.         assertFalse verifyHtml(htmlDivNoUl)
  39.     }
  40.  
  41.     void testDivUlNoLi() {
  42.         def htmlDivUlNoLi = "<div class='list'><ul></ul></div>"        
  43.         assertFalse verifyHtml(htmlDivUlNoLi)
  44.     }
  45.  
  46.     void testWithGoodHtml() {
  47.         def html = """
  48.         <div class='list'>
  49.             <ul>
  50.                 <li>list 1</li>
  51.                 <li>list 2</li>
  52.             </ul>
  53.         </div>
  54.         """    
  55.         assertTrue verifyHtml(html)
  56.     }    
  57. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top