def test_incorrect_xpath(self): # The lxml xpath function return boolean for following xpath # This breaks selector internal logic that assumes that only # list could be returnsed # So it was fixed and this test was crated sel = XpathSelector(self.tree).select('//ul/li/text()="oops"') self.assertEquals(False, sel.exists()) # Selector list is always empty in this special case # Even if the xpath return True on lxml level self.assertEquals(True, self.tree.xpath('//ul[1]/li[1]/text()="one"')) sel = XpathSelector(self.tree).select('//ul[1]/li[1]/text()="one"') self.assertEquals(False, sel.exists())
def test_select_select(self): root = XpathSelector(self.tree) self.assertEquals( set(['one', 'yet one']), set([x.text() for x in root.select('//ul').select('./li[1]')]), )
def test_textselector(self): self.assertEquals( 'one', XpathSelector(self.tree).select('//li/text()').text())
def test_text_selector(self): sel = XpathSelector(self.tree).select('//li/text()').one() self.assertTrue(isinstance(sel, TextSelector))
def test_select_node(self): self.assertEquals('test', XpathSelector(self.tree).select('//h1')[0].node.text)
def test_html(self): sel = XpathSelector(self.tree.xpath('//h1')[0]) self.assertEquals('<h1>test</h1>', sel.html().strip())
def test_text_list(self): root = XpathSelector(self.tree) self.assertEquals(set(['one', 'yet one']), set(root.select('//ul/li[1]').text_list()), )
def test_in_general(self): sel = XpathSelector(self.tree)
def select(self, *args, **kwargs): return XpathSelector(self.tree).select(*args, **kwargs)
def test_one(self): sel = XpathSelector(self.tree).select('//ul/li') self.assertEquals('one', sel.one().node.text) self.assertEquals('one', sel.text())
def test_exists(self): sel = XpathSelector(self.tree).select('//li[4]') self.assertEquals(True, sel.exists()) sel = XpathSelector(self.tree).select('//li[5]') self.assertEquals(False, sel.exists())
def _selector(self): return XpathSelector(self._node)
def test_number(self): sel = XpathSelector(self.tree).select('//li[4]') self.assertEquals(4, sel.number())
def test_attr_list(self): root = XpathSelector(self.tree) self.assertEquals(set(['li-1', 'li-2']), set(root.select('//ul[@id="second-list"]/li')\ .attr_list('class')) )
def test_text_list(self): root = XpathSelector(self.tree) self.assertEquals( set(['one', 'yet one']), set(root.select('//ul/li[1]').text_list()), )
def test_select_select(self): root = XpathSelector(self.tree) self.assertEquals(set(['one', 'yet one']), set([x.text() for x in root.select('//ul').select('./li[1]')]), )
def test_number(self): sel = XpathSelector(self.tree).select('//ul/li[4]') self.assertEquals(4, sel.number())
def test_exists(self): sel = XpathSelector(self.tree).select('//ul/li[4]') self.assertEquals(True, sel.exists()) sel = XpathSelector(self.tree).select('//ul/li[5]') self.assertEquals(False, sel.exists())