Add "wait_for_query_selector()" method
This commit is contained in:
parent
e014f72643
commit
3b2b96c36b
|
@ -663,6 +663,8 @@ self.generate_traffic_chain(pages, loops=1)
|
|||
self.get_element(selector, by="css selector", timeout=None)
|
||||
# Duplicates: self.wait_for_element_present(selector, by="css selector", timeout=None)
|
||||
|
||||
self.wait_for_query_selector(selector, by="css selector", timeout=None)
|
||||
|
||||
self.assert_element_present(selector, by="css selector", timeout=None)
|
||||
|
||||
self.assert_elements_present(*args, **kwargs)
|
||||
|
|
|
@ -10412,12 +10412,28 @@ class BaseCase(unittest.TestCase):
|
|||
The element does not need be visible (it may be hidden)."""
|
||||
self.__check_scope()
|
||||
if not timeout:
|
||||
timeout = settings.SMALL_TIMEOUT
|
||||
if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:
|
||||
timeout = settings.LARGE_TIMEOUT
|
||||
if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:
|
||||
timeout = self.__get_new_timeout(timeout)
|
||||
selector, by = self.__recalculate_selector(selector, by)
|
||||
return self.wait_for_element_present(selector, by=by, timeout=timeout)
|
||||
|
||||
def wait_for_query_selector(
|
||||
self, selector, by="css selector", timeout=None
|
||||
):
|
||||
"""Waits for an element to appear in the HTML of a page.
|
||||
The element does not need be visible (it may be hidden).
|
||||
This method uses document.querySelector() over Selenium."""
|
||||
self.__check_scope()
|
||||
if not timeout:
|
||||
timeout = settings.LARGE_TIMEOUT
|
||||
if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:
|
||||
timeout = self.__get_new_timeout(timeout)
|
||||
css_selector = self.convert_to_css_selector(selector, by=by)
|
||||
return js_utils.wait_for_css_query_selector(
|
||||
self.driver, css_selector, timeout
|
||||
)
|
||||
|
||||
def assert_element_present(
|
||||
self, selector, by="css selector", timeout=None
|
||||
):
|
||||
|
|
|
@ -251,19 +251,60 @@ def safe_execute_script(driver, script):
|
|||
driver.execute_script(script)
|
||||
|
||||
|
||||
def remove_extra_slashes(selector):
|
||||
if selector.count('\\"') > 0:
|
||||
if selector.count('\\"') == selector.count('"'):
|
||||
selector = selector.replace('\\"', '"')
|
||||
elif selector.count('\\"') == selector[1:-1].count('"') and (
|
||||
"'" not in selector[1:-1]
|
||||
):
|
||||
selector = "'" + selector[1:-1].replace('\\"', '"') + "'"
|
||||
else:
|
||||
pass
|
||||
if selector.count("\\'") > 0:
|
||||
if selector.count("\\'") == selector.count("'"):
|
||||
selector = selector.replace("\\'", "'")
|
||||
elif selector.count("\\'") == selector[1:-1].count("'") and (
|
||||
'"' not in selector[1:-1]
|
||||
):
|
||||
selector = '"' + selector[1:-1].replace("\\'", "'") + '"'
|
||||
else:
|
||||
pass
|
||||
return selector
|
||||
|
||||
|
||||
def optimize_selector(selector):
|
||||
if (len(selector) > 2 and selector[0] == "'") and (
|
||||
selector[-1] == "'" and '"' not in selector[1:-1]
|
||||
):
|
||||
selector = '"' + selector[1:-1] + '"'
|
||||
if (
|
||||
selector.count('"') == 0
|
||||
and selector.count("'") >= 2
|
||||
and selector.count("'") % 2 == 0
|
||||
and "='" in selector
|
||||
and "']" in selector
|
||||
):
|
||||
swap_char = "*_SWAP_CHAR_*"
|
||||
selector = selector.replace("'", swap_char)
|
||||
selector = selector.replace('"', "'")
|
||||
selector = selector.replace(swap_char, '"')
|
||||
return selector
|
||||
|
||||
|
||||
def wait_for_css_query_selector(
|
||||
driver, selector, timeout=settings.SMALL_TIMEOUT
|
||||
driver, selector, timeout=settings.LARGE_TIMEOUT
|
||||
):
|
||||
element = None
|
||||
selector = escape_quotes_if_needed(selector)
|
||||
selector = remove_extra_slashes(selector)
|
||||
selector = optimize_selector(selector)
|
||||
script = """return document.querySelector('%s');""" % selector
|
||||
start_ms = time.time() * 1000.0
|
||||
stop_ms = start_ms + (timeout * 1000.0)
|
||||
for x in range(int(timeout * 10)):
|
||||
try:
|
||||
selector = re.escape(selector)
|
||||
selector = escape_quotes_if_needed(selector)
|
||||
element = driver.execute_script(
|
||||
"""return document.querySelector('%s');""" % selector
|
||||
)
|
||||
element = driver.execute_script(script)
|
||||
if element:
|
||||
return element
|
||||
except Exception:
|
||||
|
|
Loading…
Reference in New Issue