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

Source Code for Module lxml.tests.test_css

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