From 001cf39ff436fb44da2ae5e8b60aaef73991310e Mon Sep 17 00:00:00 2001 From: Paul Tremberth Date: Fri, 17 Jan 2014 00:04:20 +0100 Subject: [PATCH] Add testcase to check is default Selector doesnt return smart strings --- scrapy/tests/test_selector.py | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/scrapy/tests/test_selector.py b/scrapy/tests/test_selector.py index d84e4bd47..27f10a2b5 100644 --- a/scrapy/tests/test_selector.py +++ b/scrapy/tests/test_selector.py @@ -297,6 +297,49 @@ class SelectorTestCase(unittest.TestCase): sel.remove_namespaces() self.assertEqual(len(sel.xpath("//link/@type")), 2) + def test_smart_strings(self): + """Lxml smart strings return values""" + + class SmartStringsSelector(Selector): + _lxml_smart_strings = True + + body = """ +
+ +
+
+ +
+ """ + + response = HtmlResponse(url="http://example.com", body=body) + + # .getparent() is available for text nodes and attributes + # only when smart_strings are on + x = self.sscls(response) + li_text = x.xpath('//li/text()') + self.assertIs( + any(map(lambda e: hasattr(e._root, 'getparent'), li_text)), + False) + div_class = x.xpath('//div/@class') + self.assertIs( + any(map(lambda e: hasattr(e._root, 'getparent'), div_class)), + False) + + x = SmartStringsSelector(response) + li_text = x.xpath('//li/text()') + self.assertIs( + all(map(lambda e: hasattr(e._root, 'getparent'), li_text)), + True) + div_class = x.xpath('//div/@class') + self.assertIs( + all(map(lambda e: hasattr(e._root, 'getparent'), div_class)), + True) + class DeprecatedXpathSelectorTest(unittest.TestCase):