def test_removes_added_selector(self): assert "custom_selector" not in selectors with add_selector("custom_selector"): pass assert "custom_selector" in selectors remove_selector("custom_selector") assert "custom_selector" not in selectors
def test_allows_custom_matcher_using_css(self, string): with add_selector("section") as s: s.css = lambda css_class: "section .{}".format(css_class) assert string.has_selector("section", "subsection") assert not string.has_selector("section", "section_8") remove_selector("section")
def test_css_decorator_with_parens_sets_css_query_function(self): with add_selector("custom_selector") as s: @s.css() def css(id): return "h1#{}".format(id) selector = selectors["custom_selector"] assert selector("foo") == "h1#foo"
def test_xpath_decorator_with_parens_sets_xpath_query_function(self): with add_selector("custom_selector") as s: @s.xpath() def xpath(id): return ".//h1[./@id = '{}']".format(id) selector = selectors["custom_selector"] assert selector("foo") == ".//h1[./@id = 'foo']"
def test_uses_a_custom_selector(self, session): with add_selector("level") as s: @s.xpath def xpath(num): return ".//*[@id='ancestor{num}']".format(num=num) el = session.find("css", "#child") assert el.ancestor("level", 1).text == "Ancestor Child" assert el.ancestor("level", 3).text == "Ancestor Ancestor Ancestor Child"
def test_uses_a_custom_selector(self, session): with add_selector("data_attribute") as s: @s.xpath def xpath(attr): return ".//*[@data-{}]".format(attr) el = session.find("css", "#mid_sibling") assert el.sibling("data_attribute", "pre").has_text("Pre Sibling") assert el.sibling("data_attribute", "post").has_text("Post Sibling")
def test_allows_using_custom_matchers(self, string): with add_selector("lifeform") as s: s.xpath = lambda name: "//option[contains(.,'{}')]".format(name) assert string.has_selector("id", "page") assert not string.has_selector("id", "does-not-exist") assert string.has_selector("lifeform", "Monkey") assert not string.has_selector("lifeform", "Gorilla") remove_selector("lifeform")
def test_uses_a_custom_selector(self, session): with add_selector("beatle") as s: s.xpath = lambda name: ".//*[@id='{}']".format(name) assert session.find("beatle", "john").text == "John" assert session.find("beatle", "paul").text == "Paul"
def test_css_assignment_sets_css_query_function(self): with add_selector("custom_selector") as s: s.css = lambda id: "h1#{}".format(id) selector = selectors["custom_selector"] assert selector("foo") == "h1#foo"
def test_xpath_assignment_sets_xpath_query_function(self): with add_selector("custom_selector") as s: s.xpath = lambda id: ".//h1[./@id = '{}']".format(id) selector = selectors["custom_selector"] assert selector("foo") == ".//h1[./@id = 'foo']"
def test_label_assignment_sets_label(self): with add_selector("custom_selector") as s: s.label = "My Custom Selector" selector = selectors["custom_selector"] assert selector.label == "My Custom Selector"
def test_sets_name(self): with add_selector("custom_selector"): pass selector = selectors["custom_selector"] assert selector.name == "custom_selector"