def params(self, button): params = MultiDict() types = ["input", "select", "textarea"] xpath = x.descendant(*types)[~x.attr("form")] if self.native.get("id"): xpath += x.anywhere( *types)[x.attr("form") == self.native.get("id")] xpath = xpath[~x.attr("disabled")] for field in self._find_xpath(to_xpath(xpath)): if field.tag_name == "input": if field["type"] in ["checkbox", "radio"]: if field.checked: params.add(field["name"], field.value) elif field["type"] == "file": if self._multipart: if field.value: params.add( field["name"], FileStorage(stream=open(field.value, "rb"), filename=os.path.basename( field.value))) else: params.add( field["name"], FileStorage( stream=BytesIO(), content_type="application/octet-stream")) else: if field.value: params.add(field["name"], os.path.basename(field.value)) elif field["type"] in ["submit", "reset", "image"]: pass else: params.add(field["name"], field.value) elif field.tag_name == "textarea": if field.value: params.add(field["name"], re.sub("\n", "\r\n", field.value)) elif field.tag_name == "select": if field["multiple"] == "multiple": options = field.native.xpath( ".//option[@selected='selected']") for option in options: params.add(field["name"], option.get("value", option.text)) else: options = field.native.xpath( ".//option[@selected='selected']") if not len(options): options = field.native.xpath(".//option") if len(options): params.add(field["name"], options[0].get("value", options[0].text)) params.add(button["name"], button["value"] or "") return params
def _set_radio(self, value): other_radios = self.native.xpath(to_xpath( x.anywhere("input")[x.attr("name") == self["name"]])) for node in other_radios: node.attrib.pop("checked", None) self.native.set("checked", "checked")
def _locate_field(field_expr, locator): expr = field_expr[ attr("id").equals(locator) | attr("name").equals(locator) | attr("placeholder").equals(locator) | attr("id"). equals(anywhere("label")[string.n.is_(locator)].attr("for"))] expr += descendant("label")[string.n.is_(locator)].descendant(field_expr) return expr
def _locate_field(field_expr, locator): expr = field_expr[ x.attr("id").equals(locator) | x.attr("name").equals(locator) | x.attr("placeholder").equals(locator) | x.attr("id").equals(x.anywhere("label")[x.string.n.is_(locator)].attr("for"))] expr += x.descendant("label")[x.string.n.is_(locator)].descendant(field_expr) return expr
def _locate_field(field_expr, locator): attr_matchers = ( x.attr("id").equals(locator) | x.attr("name").equals(locator) | x.attr("placeholder").equals(locator) | x.attr("id").equals( x.anywhere("label")[x.string.n.is_(locator)].attr("for"))) if capybara.enable_aria_label: attr_matchers |= x.attr("aria-label").is_(locator) expr = field_expr[attr_matchers] expr += x.descendant("label")[x.string.n.is_(locator)].descendant( field_expr) return expr
def test_finds_nodes_that_contain_the_given_expression(self): expr = x.anywhere("div")[x.attr("title") == "fooDiv"].attr("id") xpath = to_xpath(x.descendant("div")[x.attr("title").starts_with(expr)]) results = self.find_all(xpath) self.assertEqual(results[0].get("id"), "foo")
def test_finds_nodes_regardless_of_the_context(self): foo_div = anywhere("div")[attr("id").equals("foo")] xpath = to_xpath( descendant("p")[attr("id").equals(foo_div.attr("title"))]) results = self.find_all(xpath) self.assertEqual(results[0].text, "Blah")
def test_finds_nodes_regardless_of_the_context(self): foo_div = x.anywhere("div")[x.attr("id").equals("foo")] xpath = to_xpath(x.descendant("p")[x.attr("id").equals(foo_div.attr("title"))]) results = self.find_all(xpath) self.assertEqual(inner_text(results[0]), "Blah")
def test_finds_nodes_that_contain_the_given_expression(self): expr = x.anywhere("div")[x.attr("title").equals("fooDiv")].attr("id") xpath = to_xpath(x.descendant("div")[x.attr("title").contains(expr)]) results = self.find_all(xpath) self.assertEqual(results[0].get("id"), "foo")