Skip to content

Commit 914027e

Browse files
committed
Update CDP Mode
1 parent 05bb565 commit 914027e

File tree

4 files changed

+121
-50
lines changed

4 files changed

+121
-50
lines changed

seleniumbase/core/log_helper.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
py311_patch2 = constants.PatchPy311.PATCH
1616

1717

18+
def __is_cdp_swap_needed(driver):
19+
"""If the driver is disconnected, use a CDP method when available."""
20+
return shared_utils.is_cdp_swap_needed(driver)
21+
22+
1823
def log_screenshot(test_logpath, driver, screenshot=None, get=False):
1924
screenshot_name = settings.SCREENSHOT_NAME
2025
screenshot_path = os.path.join(test_logpath, screenshot_name)
@@ -356,7 +361,11 @@ def log_page_source(test_logpath, driver, source=None):
356361
page_source = source
357362
else:
358363
try:
359-
page_source = driver.page_source
364+
page_source = None
365+
if __is_cdp_swap_needed(driver):
366+
page_source = driver.cdp.get_page_source()
367+
else:
368+
page_source = driver.page_source
360369
page_source = get_html_source_with_base_href(driver, page_source)
361370
except Exception:
362371
source = constants.Warnings.PAGE_SOURCE_UNDEFINED
@@ -448,7 +457,11 @@ def get_test_name(test):
448457

449458
def get_last_page(driver):
450459
try:
451-
last_page = driver.current_url
460+
last_page = None
461+
if __is_cdp_swap_needed(driver):
462+
last_page = driver.cdp.get_current_url()
463+
else:
464+
last_page = driver.current_url
452465
except Exception:
453466
last_page = "[WARNING! Browser Not Open!]"
454467
if len(last_page) < 5:

seleniumbase/core/sb_cdp.py

+84-44
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,14 @@ def get_event_loop(self):
127127
def add_handler(self, event, handler):
128128
self.page.add_handler(event, handler)
129129

130-
def find_element(
131-
self, selector, best_match=False, timeout=settings.SMALL_TIMEOUT
132-
):
130+
def find_element(self, selector, best_match=False, timeout=None):
133131
"""Similar to select(), but also finds elements by text content.
134132
When using text-based searches, if best_match=False, then will
135133
find the first element with the text. If best_match=True, then
136134
if multiple elements have that text, then will use the element
137135
with the closest text-length to the text being searched for."""
136+
if not timeout:
137+
timeout = settings.SMALL_TIMEOUT
138138
self.__add_light_pause()
139139
selector = self.__convert_to_css_if_xpath(selector)
140140
early_failure = False
@@ -167,12 +167,12 @@ def find_element(
167167
self.__slow_mode_pause_if_set()
168168
return element
169169

170-
def find_element_by_text(
171-
self, text, tag_name=None, timeout=settings.SMALL_TIMEOUT
172-
):
170+
def find_element_by_text(self, text, tag_name=None, timeout=None):
173171
"""Returns an element by matching text.
174172
Optionally, provide a tag_name to narrow down the search to an
175173
element with the given tag. (Eg: a, button, div, script, span)"""
174+
if not timeout:
175+
timeout = settings.SMALL_TIMEOUT
176176
self.__add_light_pause()
177177
time_now = time.time()
178178
self.assert_text(text, timeout=timeout)
@@ -218,7 +218,9 @@ def find_element_by_text(
218218
% (text, tag_name, timeout, plural)
219219
)
220220

221-
def find_all(self, selector, timeout=settings.SMALL_TIMEOUT):
221+
def find_all(self, selector, timeout=None):
222+
if not timeout:
223+
timeout = settings.SMALL_TIMEOUT
222224
self.__add_light_pause()
223225
selector = self.__convert_to_css_if_xpath(selector)
224226
elements = self.loop.run_until_complete(
@@ -272,8 +274,10 @@ def find_elements_by_text(self, text, tag_name=None):
272274
updated_elements.append(element)
273275
return updated_elements
274276

275-
def select(self, selector, timeout=settings.SMALL_TIMEOUT):
277+
def select(self, selector, timeout=None):
276278
"""Similar to find_element(), but without text-based search."""
279+
if not timeout:
280+
timeout = settings.SMALL_TIMEOUT
277281
self.__add_light_pause()
278282
selector = self.__convert_to_css_if_xpath(selector)
279283
if (":contains(" in selector):
@@ -307,7 +311,9 @@ def select(self, selector, timeout=settings.SMALL_TIMEOUT):
307311
self.__slow_mode_pause_if_set()
308312
return element
309313

310-
def select_all(self, selector, timeout=settings.SMALL_TIMEOUT):
314+
def select_all(self, selector, timeout=None):
315+
if not timeout:
316+
timeout = settings.SMALL_TIMEOUT
311317
self.__add_light_pause()
312318
selector = self.__convert_to_css_if_xpath(selector)
313319
elements = self.loop.run_until_complete(
@@ -319,10 +325,14 @@ def select_all(self, selector, timeout=settings.SMALL_TIMEOUT):
319325
updated_elements.append(element)
320326
return updated_elements
321327

322-
def find_elements(self, selector, timeout=settings.SMALL_TIMEOUT):
328+
def find_elements(self, selector, timeout=None):
329+
if not timeout:
330+
timeout = settings.SMALL_TIMEOUT
323331
return self.select_all(selector, timeout=timeout)
324332

325-
def find_visible_elements(self, selector, timeout=settings.SMALL_TIMEOUT):
333+
def find_visible_elements(self, selector, timeout=None):
334+
if not timeout:
335+
timeout = settings.SMALL_TIMEOUT
326336
visible_elements = []
327337
elements = self.select_all(selector, timeout=timeout)
328338
for element in elements:
@@ -587,12 +597,12 @@ def load_cookies(self, *args, **kwargs):
587597
driver.cookies.load(*args, **kwargs)
588598
)
589599

590-
def clear_cookies(self, *args, **kwargs):
600+
def clear_cookies(self):
591601
driver = self.driver
592602
if hasattr(driver, "cdp_base"):
593603
driver = driver.cdp_base
594604
return self.loop.run_until_complete(
595-
driver.cookies.clear(*args, **kwargs)
605+
driver.cookies.clear()
596606
)
597607

598608
def sleep(self, seconds):
@@ -616,7 +626,9 @@ def get_active_element_css(self):
616626
self.page.evaluate(js_code)
617627
)
618628

619-
def click(self, selector, timeout=settings.SMALL_TIMEOUT):
629+
def click(self, selector, timeout=None):
630+
if not timeout:
631+
timeout = settings.SMALL_TIMEOUT
620632
self.__slow_mode_pause_if_set()
621633
element = self.find_element(selector, timeout=timeout)
622634
element.scroll_into_view()
@@ -672,8 +684,10 @@ def click_visible_elements(self, selector, limit=0):
672684
except Exception:
673685
break
674686

675-
def mouse_click(self, selector, timeout=settings.SMALL_TIMEOUT):
687+
def mouse_click(self, selector, timeout=None):
676688
"""(Attempt simulating a mouse click)"""
689+
if not timeout:
690+
timeout = settings.SMALL_TIMEOUT
677691
self.__slow_mode_pause_if_set()
678692
element = self.find_element(selector, timeout=timeout)
679693
element.scroll_into_view()
@@ -771,7 +785,9 @@ def remove_elements(self, selector):
771785
with suppress(Exception):
772786
self.loop.run_until_complete(self.page.evaluate(js_code))
773787

774-
def send_keys(self, selector, text, timeout=settings.SMALL_TIMEOUT):
788+
def send_keys(self, selector, text, timeout=None):
789+
if not timeout:
790+
timeout = settings.SMALL_TIMEOUT
775791
self.__slow_mode_pause_if_set()
776792
element = self.select(selector, timeout=timeout)
777793
element.scroll_into_view()
@@ -781,8 +797,10 @@ def send_keys(self, selector, text, timeout=settings.SMALL_TIMEOUT):
781797
self.__slow_mode_pause_if_set()
782798
self.loop.run_until_complete(self.page.wait())
783799

784-
def press_keys(self, selector, text, timeout=settings.SMALL_TIMEOUT):
800+
def press_keys(self, selector, text, timeout=None):
785801
"""Similar to send_keys(), but presses keys at human speed."""
802+
if not timeout:
803+
timeout = settings.SMALL_TIMEOUT
786804
self.__slow_mode_pause_if_set()
787805
element = self.select(selector, timeout=timeout)
788806
element.scroll_into_view()
@@ -799,8 +817,10 @@ def press_keys(self, selector, text, timeout=settings.SMALL_TIMEOUT):
799817
self.__slow_mode_pause_if_set()
800818
self.loop.run_until_complete(self.page.wait())
801819

802-
def type(self, selector, text, timeout=settings.SMALL_TIMEOUT):
820+
def type(self, selector, text, timeout=None):
803821
"""Similar to send_keys(), but clears the text field first."""
822+
if not timeout:
823+
timeout = settings.SMALL_TIMEOUT
804824
self.__slow_mode_pause_if_set()
805825
element = self.select(selector, timeout=timeout)
806826
element.scroll_into_view()
@@ -812,8 +832,10 @@ def type(self, selector, text, timeout=settings.SMALL_TIMEOUT):
812832
self.__slow_mode_pause_if_set()
813833
self.loop.run_until_complete(self.page.wait())
814834

815-
def set_value(self, selector, text, timeout=settings.SMALL_TIMEOUT):
835+
def set_value(self, selector, text, timeout=None):
816836
"""Similar to send_keys(), but clears the text field first."""
837+
if not timeout:
838+
timeout = settings.SMALL_TIMEOUT
817839
self.__slow_mode_pause_if_set()
818840
selector = self.__convert_to_css_if_xpath(selector)
819841
element = self.select(selector, timeout=timeout)
@@ -1036,7 +1058,9 @@ def get_window_position(self):
10361058
coordinates["y"] = y if y else 0
10371059
return coordinates
10381060

1039-
def get_element_rect(self, selector, timeout=settings.SMALL_TIMEOUT):
1061+
def get_element_rect(self, selector, timeout=None):
1062+
if not timeout:
1063+
timeout = settings.SMALL_TIMEOUT
10401064
selector = self.__convert_to_css_if_xpath(selector)
10411065
self.select(selector, timeout=timeout)
10421066
self.__add_light_pause()
@@ -1049,23 +1073,29 @@ def get_element_rect(self, selector, timeout=settings.SMALL_TIMEOUT):
10491073
)
10501074
return coordinates
10511075

1052-
def get_element_size(self, selector):
1053-
element_rect = self.get_element_rect(selector)
1076+
def get_element_size(self, selector, timeout=None):
1077+
if not timeout:
1078+
timeout = settings.SMALL_TIMEOUT
1079+
element_rect = self.get_element_rect(selector, timeout=timeout)
10541080
coordinates = {}
10551081
coordinates["width"] = element_rect["width"]
10561082
coordinates["height"] = element_rect["height"]
10571083
return coordinates
10581084

1059-
def get_element_position(self, selector):
1060-
element_rect = self.get_element_rect(selector)
1085+
def get_element_position(self, selector, timeout=None):
1086+
if not timeout:
1087+
timeout = settings.SMALL_TIMEOUT
1088+
element_rect = self.get_element_rect(selector, timeout=timeout)
10611089
coordinates = {}
10621090
coordinates["x"] = element_rect["x"]
10631091
coordinates["y"] = element_rect["y"]
10641092
return coordinates
10651093

1066-
def get_gui_element_rect(self, selector):
1094+
def get_gui_element_rect(self, selector, timeout=None):
10671095
"""(Coordinates are relative to the screen. Not the window.)"""
1068-
element_rect = self.get_element_rect(selector)
1096+
if not timeout:
1097+
timeout = settings.SMALL_TIMEOUT
1098+
element_rect = self.get_element_rect(selector, timeout=timeout)
10691099
e_width = element_rect["width"]
10701100
e_height = element_rect["height"]
10711101
window_rect = self.get_window_rect()
@@ -1079,9 +1109,11 @@ def get_gui_element_rect(self, selector):
10791109
y = y + window_rect["scrollY"]
10801110
return ({"height": e_height, "width": e_width, "x": x, "y": y})
10811111

1082-
def get_gui_element_center(self, selector):
1112+
def get_gui_element_center(self, selector, timeout=None):
10831113
"""(Coordinates are relative to the screen. Not the window.)"""
1084-
element_rect = self.get_gui_element_rect(selector)
1114+
if not timeout:
1115+
timeout = settings.SMALL_TIMEOUT
1116+
element_rect = self.get_gui_element_rect(selector, timeout=timeout)
10851117
e_width = element_rect["width"]
10861118
e_height = element_rect["height"]
10871119
e_x = element_rect["x"]
@@ -1629,9 +1661,9 @@ def is_element_visible(self, selector):
16291661
return True
16301662
return False
16311663

1632-
def wait_for_element_visible(
1633-
self, selector, timeout=settings.SMALL_TIMEOUT
1634-
):
1664+
def wait_for_element_visible(self, selector, timeout=None):
1665+
if not timeout:
1666+
timeout = settings.SMALL_TIMEOUT
16351667
try:
16361668
self.select(selector, timeout=timeout)
16371669
except Exception:
@@ -1642,8 +1674,10 @@ def wait_for_element_visible(
16421674
time.sleep(0.1)
16431675
raise Exception("Element {%s} was not visible!" % selector)
16441676

1645-
def assert_element(self, selector, timeout=settings.SMALL_TIMEOUT):
1677+
def assert_element(self, selector, timeout=None):
16461678
"""Same as assert_element_visible()"""
1679+
if not timeout:
1680+
timeout = settings.SMALL_TIMEOUT
16471681
try:
16481682
self.select(selector, timeout=timeout)
16491683
except Exception:
@@ -1654,8 +1688,10 @@ def assert_element(self, selector, timeout=settings.SMALL_TIMEOUT):
16541688
time.sleep(0.1)
16551689
raise Exception("Element {%s} was not visible!" % selector)
16561690

1657-
def assert_element_visible(self, selector, timeout=settings.SMALL_TIMEOUT):
1691+
def assert_element_visible(self, selector, timeout=None):
16581692
"""Same as assert_element()"""
1693+
if not timeout:
1694+
timeout = settings.SMALL_TIMEOUT
16591695
try:
16601696
self.select(selector, timeout=timeout)
16611697
except Exception:
@@ -1666,16 +1702,20 @@ def assert_element_visible(self, selector, timeout=settings.SMALL_TIMEOUT):
16661702
time.sleep(0.1)
16671703
raise Exception("Element {%s} was not visible!" % selector)
16681704

1669-
def assert_element_present(self, selector, timeout=settings.SMALL_TIMEOUT):
1705+
def assert_element_present(self, selector, timeout=None):
16701706
"""Assert element is present in the DOM. (Visibility NOT required)"""
1707+
if not timeout:
1708+
timeout = settings.SMALL_TIMEOUT
16711709
try:
16721710
self.select(selector, timeout=timeout)
16731711
except Exception:
16741712
raise Exception("Element {%s} was not found!" % selector)
16751713
return True
16761714

1677-
def assert_element_absent(self, selector, timeout=settings.SMALL_TIMEOUT):
1715+
def assert_element_absent(self, selector, timeout=None):
16781716
"""Assert element is not present in the DOM."""
1717+
if not timeout:
1718+
timeout = settings.SMALL_TIMEOUT
16791719
start_ms = time.time() * 1000.0
16801720
stop_ms = start_ms + (timeout * 1000.0)
16811721
for i in range(int(timeout * 10)):
@@ -1693,10 +1733,10 @@ def assert_element_absent(self, selector, timeout=settings.SMALL_TIMEOUT):
16931733
% (selector, timeout, plural)
16941734
)
16951735

1696-
def assert_element_not_visible(
1697-
self, selector, timeout=settings.SMALL_TIMEOUT
1698-
):
1736+
def assert_element_not_visible(self, selector, timeout=None):
16991737
"""Assert element is not visible on page. (May still be in DOM)"""
1738+
if not timeout:
1739+
timeout = settings.SMALL_TIMEOUT
17001740
start_ms = time.time() * 1000.0
17011741
stop_ms = start_ms + (timeout * 1000.0)
17021742
for i in range(int(timeout * 10)):
@@ -1791,9 +1831,9 @@ def assert_url_contains(self, substring):
17911831
if expected not in actual:
17921832
raise Exception(error % (expected, actual))
17931833

1794-
def assert_text(
1795-
self, text, selector="body", timeout=settings.SMALL_TIMEOUT
1796-
):
1834+
def assert_text(self, text, selector="body", timeout=None):
1835+
if not timeout:
1836+
timeout = settings.SMALL_TIMEOUT
17971837
start_ms = time.time() * 1000.0
17981838
stop_ms = start_ms + (timeout * 1000.0)
17991839
text = text.strip()
@@ -1816,9 +1856,9 @@ def assert_text(
18161856
% (text, selector, element.text_all)
18171857
)
18181858

1819-
def assert_exact_text(
1820-
self, text, selector="body", timeout=settings.SMALL_TIMEOUT
1821-
):
1859+
def assert_exact_text(self, text, selector="body", timeout=None):
1860+
if not timeout:
1861+
timeout = settings.SMALL_TIMEOUT
18221862
start_ms = time.time() * 1000.0
18231863
stop_ms = start_ms + (timeout * 1000.0)
18241864
text = text.strip()

0 commit comments

Comments
 (0)