Package lxml :: Package tests :: Module test_css
[hide private]
[frames] | no frames]

Source Code for Module lxml.tests.test_css

 1  import unittest 
 2   
 3  import lxml.html 
 4   
 5  from lxml.tests.common_imports import doctest, HelperTestCase 
 6   
 7   
 8  HTML = ''' 
 9  <div> 
10    <a href="foo">link</a> 
11    <a>anchor</a> 
12  </div> 
13  ''' 
14   
15   
16 -class CSSTestCase(HelperTestCase):
17
18 - def test_cssselect(self):
19 div, = lxml.html.fromstring(HTML).xpath('//div') 20 21 def count(selector, expected_count, **kwargs): 22 result = div.cssselect(selector, **kwargs) 23 self.assertEqual(len(result), expected_count)
24 25 count('div', 1) 26 count('a', 2) 27 count('em', 0) 28 # Element names are case-insensitive in HTML 29 count('DIV', 1) 30 # ... but not in XHTML or XML 31 count('DIV', 0, translator='xhtml') 32 count('DIV', 0, translator='xml') 33 34 # :contains() is case-insensitive in lxml 35 count(':contains("link")', 2) # div, a 36 count(':contains("LInk")', 2) 37 # Whatever the document language 38 count(':contains("LInk")', 2, translator='xhtml') 39 count(':contains("LInk")', 2, translator='xml') 40 # ... but not in upstream cssselect 41 import cssselect 42 count(':contains("link")', 2, translator=cssselect.HTMLTranslator()) 43 count(':contains("LInk")', 0, translator=cssselect.HTMLTranslator())
44 45
46 -def test_suite():
47 suite = unittest.TestSuite() 48 try: 49 import cssselect 50 except ImportError: 51 # no 'cssselect' installed 52 print("Skipping tests in lxml.cssselect - external cssselect package is not installed") 53 return suite 54 55 import lxml.cssselect 56 suite.addTests(doctest.DocTestSuite(lxml.cssselect)) 57 suite.addTests([unittest.makeSuite(CSSTestCase)]) 58 return suite
59