def test_string(reporter): results = [] results.append(escape_xpath_value("tidii")) results.append(escape_xpath_value('"tidii"')) results.append(escape_xpath_value("'tidii'")) results.append(escape_xpath_value("\"'tidii'")) results.append(escape_xpath_value("\"'tidii'\"")) results.append(escape_xpath_value('"\'t"id"ii\'"')) results.append(escape_xpath_value("\"'tidii'\"")) results.append(escape_xpath_value("\"'ti'd'ii'\"")) verify_all("Escape xpath value", results, reporter=reporter)
def _get_attrs_with_url(self, key_attrs, criteria): attrs = [] url = None xpath_url = None for attr in ["@src", "@href"]: if attr in key_attrs: if url is None or xpath_url is None: url = self._get_base_url() + "/" + criteria xpath_url = escape_xpath_value(url) attrs.append("%s=%s" % (attr, xpath_url)) return attrs
def _get_attrs_with_url(self, key_attrs, criteria): attrs = [] url = None xpath_url = None for attr in ['@src', '@href']: if attr in key_attrs: if url is None or xpath_url is None: url = self._get_base_url() + "/" + criteria xpath_url = escape_xpath_value(url) attrs.append("%s=%s" % (attr, xpath_url)) return attrs
def Highlight_Elements_With_Text_On_Time(text, time=2): from time import sleep seleniumlib = BuiltIn().get_library_instance('SeleniumLibrary') locator = u"xpath=//*[contains(text(), {})]".format( utils.escape_xpath_value(text)) elements = seleniumlib._element_finder.find(locator, None, False, False) for element in elements: seleniumlib._current_browser().execute_script( "arguments[0].style['outline'] = '3px dotted red';", element) sleep(time) for element in elements: seleniumlib._current_browser().execute_script( "arguments[0].style['outline'] = '';", element)
def _find_by_default(self, criteria, tag, constraints, parent): if tag in self._key_attrs: key_attrs = self._key_attrs[tag] else: key_attrs = self._key_attrs[None] xpath_criteria = escape_xpath_value(criteria) xpath_tag = tag if tag is not None else "*" xpath_constraints = self._get_xpath_constraints(constraints) xpath_searchers = [f"{attr}={xpath_criteria}" for attr in key_attrs] xpath_searchers.extend(self._get_attrs_with_url(key_attrs, criteria)) xpath = ( f"//{xpath_tag}[{' and '.join(xpath_constraints)}" f"{' and ' if xpath_constraints else ''}({' or '.join(xpath_searchers)})]" ) return self._normalize(parent.find_elements(By.XPATH, xpath))
def _find_by_default(self, criteria, tag, constraints, parent): if tag in self._key_attrs: key_attrs = self._key_attrs[tag] else: key_attrs = self._key_attrs[None] xpath_criteria = escape_xpath_value(criteria) xpath_tag = tag if tag is not None else '*' xpath_constraints = self._get_xpath_constraints(constraints) xpath_searchers = [ "%s=%s" % (attr, xpath_criteria) for attr in key_attrs ] xpath_searchers.extend(self._get_attrs_with_url(key_attrs, criteria)) xpath = "//%s[%s%s(%s)]" % (xpath_tag, ' and '.join(xpath_constraints), ' and ' if xpath_constraints else '', ' or '.join(xpath_searchers)) return self._normalize(parent.find_elements_by_xpath(xpath))
def _find_by_default(self, criteria, tag, constraints, parent): if tag in self._key_attrs: key_attrs = self._key_attrs[tag] else: key_attrs = self._key_attrs[None] xpath_criteria = escape_xpath_value(criteria) xpath_tag = tag if tag is not None else '*' xpath_constraints = self._get_xpath_constraints(constraints) xpath_searchers = ["%s=%s" % (attr, xpath_criteria) for attr in key_attrs] xpath_searchers.extend(self._get_attrs_with_url(key_attrs, criteria)) xpath = "//%s[%s%s(%s)]" % ( xpath_tag, ' and '.join(xpath_constraints), ' and ' if xpath_constraints else '', ' or '.join(xpath_searchers) ) return self._normalize(parent.find_elements_by_xpath(xpath))
def is_text_present(self, text): locator = "xpath://*[contains(., %s)]" % escape_xpath_value(text) return self.find_element(locator, required=False) is not None
def test_escape_xpath_value_with_quote(self): self.assertEqual(escape_xpath_value('test "1"'), "'test \"1\"'")
def test_escape_xpath_value_with_apos(self): self.assertEqual(escape_xpath_value("test '1'"), "\"test '1'\"")
def test_escape_xpath_value_with_quote_and_apos(self): self.assertEqual( escape_xpath_value("test \"1\" and '2'"), "concat('test \"1\" and ', \"'\", '2', \"'\", '')", )
def test_escape_xpath_value_with_quote_and_apos(self): self.assertEqual(escape_xpath_value("test \"1\" and '2'"), "concat('test \"1\" and ', \"'\", '2', \"'\", '')")
def test_escape_xpath_value_with_quote(self): self.assertEqual(escape_xpath_value("test \"1\""), "'test \"1\"'")