Skip to content

Commit 5147555

Browse files
committed
Update CDP Mode
1 parent 9c1909e commit 5147555

File tree

7 files changed

+273
-49
lines changed

7 files changed

+273
-49
lines changed

seleniumbase/core/browser_launcher.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,10 @@ def uc_open_with_cdp_mode(driver, url=None):
718718
cdp.is_selected = CDPM.is_selected
719719
cdp.is_element_present = CDPM.is_element_present
720720
cdp.is_element_visible = CDPM.is_element_visible
721+
cdp.is_text_visible = CDPM.is_text_visible
722+
cdp.is_exact_text_visible = CDPM.is_exact_text_visible
723+
cdp.wait_for_text = CDPM.wait_for_text
724+
cdp.wait_for_text_not_visible = CDPM.wait_for_text_not_visible
721725
cdp.wait_for_element_visible = CDPM.wait_for_element_visible
722726
cdp.assert_element = CDPM.assert_element
723727
cdp.assert_element_visible = CDPM.assert_element_visible
@@ -731,6 +735,7 @@ def uc_open_with_cdp_mode(driver, url=None):
731735
cdp.assert_url_contains = CDPM.assert_url_contains
732736
cdp.assert_text = CDPM.assert_text
733737
cdp.assert_exact_text = CDPM.assert_exact_text
738+
cdp.assert_text_not_visible = CDPM.assert_text_not_visible
734739
cdp.assert_true = CDPM.assert_true
735740
cdp.assert_false = CDPM.assert_false
736741
cdp.assert_equal = CDPM.assert_equal
@@ -2280,6 +2285,7 @@ def _set_chrome_options(
22802285
or proxy_string
22812286
):
22822287
chrome_options.add_argument("--ignore-certificate-errors")
2288+
chrome_options.add_argument("--ignore-ssl-errors=yes")
22832289
if not enable_ws:
22842290
chrome_options.add_argument("--disable-web-security")
22852291
if (
@@ -4231,6 +4237,7 @@ def get_local_driver(
42314237
edge_options.add_argument("--log-level=3")
42324238
edge_options.add_argument("--no-first-run")
42334239
edge_options.add_argument("--ignore-certificate-errors")
4240+
edge_options.add_argument("--ignore-ssl-errors=yes")
42344241
if devtools and not headless:
42354242
edge_options.add_argument("--auto-open-devtools-for-tabs")
42364243
edge_options.add_argument("--allow-file-access-from-files")

seleniumbase/core/sb_cdp.py

Lines changed: 105 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def __add_sync_methods(self, element):
6262
lambda destination: self.__mouse_drag(element, destination)
6363
)
6464
element.mouse_move = lambda: self.__mouse_move(element)
65+
element.press_keys = lambda text: self.__press_keys(element, text)
6566
element.query_selector = (
6667
lambda selector: self.__query_selector(element, selector)
6768
)
@@ -211,7 +212,8 @@ def find_element_by_text(self, text, tag_name=None, timeout=None):
211212
element = self.__add_sync_methods(element.parent)
212213
return self.__add_sync_methods(element)
213214
elif (
214-
element.parent.parent
215+
element.parent
216+
and element.parent.parent
215217
and tag_name in element.parent.parent.tag_name.lower()
216218
and text.strip() in element.parent.parent.text
217219
):
@@ -272,7 +274,8 @@ def find_elements_by_text(self, text, tag_name=None):
272274
if element not in updated_elements:
273275
updated_elements.append(element)
274276
elif (
275-
element.parent.parent
277+
element.parent
278+
and element.parent.parent
276279
and tag_name in element.parent.parent.tag_name.lower()
277280
and text.strip() in element.parent.parent.text
278281
):
@@ -445,6 +448,23 @@ def __mouse_move(self, element):
445448
self.loop.run_until_complete(element.mouse_move_async())
446449
)
447450

451+
def __press_keys(self, element, text):
452+
element.scroll_into_view()
453+
submit = False
454+
if text.endswith("\n") or text.endswith("\r"):
455+
submit = True
456+
text = text[:-1]
457+
for key in text:
458+
element.send_keys(key)
459+
time.sleep(0.044)
460+
if submit:
461+
element.send_keys("\r\n")
462+
time.sleep(0.044)
463+
self.__slow_mode_pause_if_set()
464+
return (
465+
self.loop.run_until_complete(self.page.wait())
466+
)
467+
448468
def __query_selector(self, element, selector):
449469
selector = self.__convert_to_css_if_xpath(selector)
450470
element2 = self.loop.run_until_complete(
@@ -1681,21 +1701,79 @@ def is_element_visible(self, selector):
16811701
return True
16821702
return False
16831703

1684-
def wait_for_element_visible(self, selector, timeout=None):
1704+
def is_text_visible(self, text, selector="body"):
1705+
selector = self.__convert_to_css_if_xpath(selector)
1706+
text = text.strip()
1707+
element = None
1708+
try:
1709+
element = self.find_element(selector, timeout=0.1)
1710+
except Exception:
1711+
return False
1712+
with suppress(Exception):
1713+
if text in element.text_all:
1714+
return True
1715+
return False
1716+
1717+
def is_exact_text_visible(self, text, selector="body"):
1718+
selector = self.__convert_to_css_if_xpath(selector)
1719+
text = text.strip()
1720+
element = None
1721+
try:
1722+
element = self.find_element(selector, timeout=0.1)
1723+
except Exception:
1724+
return False
1725+
with suppress(Exception):
1726+
if text == element.text_all.strip():
1727+
return True
1728+
return False
1729+
1730+
def wait_for_text(self, text, selector="body", timeout=None):
16851731
if not timeout:
16861732
timeout = settings.SMALL_TIMEOUT
1733+
start_ms = time.time() * 1000.0
1734+
stop_ms = start_ms + (timeout * 1000.0)
1735+
text = text.strip()
1736+
element = None
16871737
try:
1688-
self.select(selector, timeout=timeout)
1738+
element = self.find_element(selector, timeout=timeout)
16891739
except Exception:
1690-
raise Exception("Element {%s} was not found!" % selector)
1691-
for i in range(30):
1692-
if self.is_element_visible(selector):
1693-
return self.select(selector)
1740+
raise Exception("Element {%s} not found!" % selector)
1741+
for i in range(int(timeout * 10)):
1742+
with suppress(Exception):
1743+
element = self.find_element(selector, timeout=0.1)
1744+
if text in element.text_all:
1745+
return True
1746+
now_ms = time.time() * 1000.0
1747+
if now_ms >= stop_ms:
1748+
break
16941749
time.sleep(0.1)
1695-
raise Exception("Element {%s} was not visible!" % selector)
1750+
raise Exception(
1751+
"Text {%s} not found in {%s}! Actual text: {%s}"
1752+
% (text, selector, element.text_all)
1753+
)
16961754

1697-
def assert_element(self, selector, timeout=None):
1698-
"""Same as assert_element_visible()"""
1755+
def wait_for_text_not_visible(self, text, selector="body", timeout=None):
1756+
if not timeout:
1757+
timeout = settings.SMALL_TIMEOUT
1758+
text = text.strip()
1759+
start_ms = time.time() * 1000.0
1760+
stop_ms = start_ms + (timeout * 1000.0)
1761+
for i in range(int(timeout * 10)):
1762+
if not self.is_text_visible(text, selector):
1763+
return True
1764+
now_ms = time.time() * 1000.0
1765+
if now_ms >= stop_ms:
1766+
break
1767+
time.sleep(0.1)
1768+
plural = "s"
1769+
if timeout == 1:
1770+
plural = ""
1771+
raise Exception(
1772+
"Text {%s} in {%s} was still visible after %s second%s!"
1773+
% (text, selector, timeout, plural)
1774+
)
1775+
1776+
def wait_for_element_visible(self, selector, timeout=None):
16991777
if not timeout:
17001778
timeout = settings.SMALL_TIMEOUT
17011779
try:
@@ -1704,10 +1782,15 @@ def assert_element(self, selector, timeout=None):
17041782
raise Exception("Element {%s} was not found!" % selector)
17051783
for i in range(30):
17061784
if self.is_element_visible(selector):
1707-
return True
1785+
return self.select(selector)
17081786
time.sleep(0.1)
17091787
raise Exception("Element {%s} was not visible!" % selector)
17101788

1789+
def assert_element(self, selector, timeout=None):
1790+
"""Same as assert_element_visible()"""
1791+
self.assert_element_visible(selector, timeout=timeout)
1792+
return True
1793+
17111794
def assert_element_visible(self, selector, timeout=None):
17121795
"""Same as assert_element()"""
17131796
if not timeout:
@@ -1852,29 +1935,9 @@ def assert_url_contains(self, substring):
18521935
raise Exception(error % (expected, actual))
18531936

18541937
def assert_text(self, text, selector="body", timeout=None):
1855-
if not timeout:
1856-
timeout = settings.SMALL_TIMEOUT
1857-
start_ms = time.time() * 1000.0
1858-
stop_ms = start_ms + (timeout * 1000.0)
1859-
text = text.strip()
1860-
element = None
1861-
try:
1862-
element = self.find_element(selector, timeout=timeout)
1863-
except Exception:
1864-
raise Exception("Element {%s} not found!" % selector)
1865-
for i in range(int(timeout * 10)):
1866-
with suppress(Exception):
1867-
element = self.find_element(selector, timeout=0.1)
1868-
if text in element.text_all:
1869-
return True
1870-
now_ms = time.time() * 1000.0
1871-
if now_ms >= stop_ms:
1872-
break
1873-
time.sleep(0.1)
1874-
raise Exception(
1875-
"Text {%s} not found in {%s}! Actual text: {%s}"
1876-
% (text, selector, element.text_all)
1877-
)
1938+
"""Same as wait_for_text()"""
1939+
self.wait_for_text(text, selector=selector, timeout=timeout)
1940+
return True
18781941

18791942
def assert_exact_text(self, text, selector="body", timeout=None):
18801943
if not timeout:
@@ -1904,6 +1967,13 @@ def assert_exact_text(self, text, selector="body", timeout=None):
19041967
% (text, element.text_all, selector)
19051968
)
19061969

1970+
def assert_text_not_visible(self, text, selector="body", timeout=None):
1971+
"""Raises an exception if the text is still visible after timeout."""
1972+
self.wait_for_text_not_visible(
1973+
text, selector=selector, timeout=timeout
1974+
)
1975+
return True
1976+
19071977
def assert_true(self, expression):
19081978
if not expression:
19091979
raise AssertionError("%s is not true" % expression)

seleniumbase/fixtures/base_case.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,8 @@ def is_element_enabled(self, selector, by="css selector"):
14541454

14551455
def is_text_visible(self, text, selector="body", by="css selector"):
14561456
"""Returns whether the text substring is visible in the element."""
1457+
if self.__is_cdp_swap_needed():
1458+
return self.cdp.is_text_visible(text, selector)
14571459
self.wait_for_ready_state_complete()
14581460
time.sleep(0.01)
14591461
selector, by = self.__recalculate_selector(selector, by)
@@ -1464,6 +1466,8 @@ def is_text_visible(self, text, selector="body", by="css selector"):
14641466
def is_exact_text_visible(self, text, selector="body", by="css selector"):
14651467
"""Returns whether the text is exactly equal to the element text.
14661468
(Leading and trailing whitespace is ignored in the verification.)"""
1469+
if self.__is_cdp_swap_needed():
1470+
return self.cdp.is_exact_text_visible(text, selector)
14671471
self.wait_for_ready_state_complete()
14681472
time.sleep(0.01)
14691473
selector, by = self.__recalculate_selector(selector, by)
@@ -9281,7 +9285,8 @@ def set_messenger_theme(
92819285
"bottom_left", "bottom_center", "bottom_right"]
92829286
max_messages: The limit of concurrent messages to display."""
92839287
self.__check_scope()
9284-
self._check_browser()
9288+
if not self.__is_cdp_swap_needed():
9289+
self._check_browser()
92859290
if not theme:
92869291
theme = "default" # "flat"
92879292
if not location:
@@ -9308,7 +9313,8 @@ def post_message(self, message, duration=None, pause=True, style="info"):
93089313
You can also post messages by using =>
93099314
self.execute_script('Messenger().post("My Message")') """
93109315
self.__check_scope()
9311-
self._check_browser()
9316+
if not self.__is_cdp_swap_needed():
9317+
self._check_browser()
93129318
if style not in ["info", "success", "error"]:
93139319
style = "info"
93149320
if not duration:
@@ -10326,6 +10332,10 @@ def wait_for_text_not_visible(
1032610332
if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:
1032710333
timeout = self.__get_new_timeout(timeout)
1032810334
selector, by = self.__recalculate_selector(selector, by)
10335+
if self.__is_cdp_swap_needed():
10336+
return self.cdp.wait_for_text(
10337+
text, selector=selector, timeout=timeout
10338+
)
1032910339
return page_actions.wait_for_text_not_visible(
1033010340
self.driver, text, selector, by, timeout
1033110341
)
@@ -13909,7 +13919,8 @@ def __highlight_element_with_js(self, element, loops, o_bs):
1390913919
js_utils.highlight_element_with_js(self.driver, element, loops, o_bs)
1391013920

1391113921
def __highlight_with_jquery(self, selector, loops, o_bs):
13912-
self.wait_for_ready_state_complete()
13922+
if not self.__is_cdp_swap_needed():
13923+
self.wait_for_ready_state_complete()
1391313924
js_utils.highlight_with_jquery(self.driver, selector, loops, o_bs)
1391413925

1391513926
def __highlight_with_js_2(self, message, selector, o_bs):

0 commit comments

Comments
 (0)