Skip to content

Commit 42d5e11

Browse files
committed
Update code for resizing windows
1 parent 5d8be67 commit 42d5e11

File tree

2 files changed

+44
-26
lines changed

2 files changed

+44
-26
lines changed

seleniumbase/core/sb_cdp.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,12 +1029,19 @@ def js_dumps(self, obj_name):
10291029
return self.loop.run_until_complete(self.page.js_dumps(obj_name))
10301030

10311031
def maximize(self):
1032-
if self.get_window()[1].window_state.value == "maximized":
1033-
return
1034-
elif self.get_window()[1].window_state.value == "minimized":
1035-
self.loop.run_until_complete(self.page.maximize())
1036-
time.sleep(0.044)
1037-
return self.loop.run_until_complete(self.page.maximize())
1032+
try:
1033+
if self.get_window()[1].window_state.value == "maximized":
1034+
return
1035+
elif self.get_window()[1].window_state.value == "minimized":
1036+
self.loop.run_until_complete(self.page.maximize())
1037+
time.sleep(0.044)
1038+
return self.loop.run_until_complete(self.page.maximize())
1039+
except Exception:
1040+
with suppress(Exception):
1041+
width = self.evaluate("screen.availWidth;")
1042+
height = self.evaluate("screen.availHeight;")
1043+
self.__set_window_rect(0, 0, width, height)
1044+
return
10381045

10391046
def minimize(self):
10401047
if self.get_window()[1].window_state.value != "minimized":
@@ -1725,18 +1732,18 @@ def gui_click_x_y(self, x, y, timeframe=0.25):
17251732
win_x = window_rect["x"]
17261733
win_y = window_rect["y"]
17271734
scr_width = pyautogui.size().width
1728-
self.maximize()
1729-
self.__add_light_pause()
1730-
win_width = self.get_window_size()["width"]
1735+
win_width = self.evaluate("screen.availWidth;")
17311736
width_ratio = round(float(scr_width) / float(win_width), 2)
17321737
width_ratio += 0.01
17331738
if width_ratio < 0.45 or width_ratio > 2.55:
17341739
width_ratio = 1.01
17351740
sb_config._saved_width_ratio = width_ratio
1736-
self.minimize()
1737-
self.__add_light_pause()
1738-
self.__set_window_rect(win_x, win_y, width, height)
1739-
self.__add_light_pause()
1741+
with suppress(Exception):
1742+
self.get_window() # If this fails, skip the rest
1743+
self.minimize()
1744+
self.__add_light_pause()
1745+
self.__set_window_rect(win_x, win_y, width, height)
1746+
self.__add_light_pause()
17401747
x = x * (width_ratio + 0.03)
17411748
y = y * (width_ratio - 0.03)
17421749
self.bring_active_window_to_front()
@@ -2021,18 +2028,18 @@ def gui_drag_drop_points(self, x1, y1, x2, y2, timeframe=0.35):
20212028
win_x = window_rect["x"]
20222029
win_y = window_rect["y"]
20232030
scr_width = pyautogui.size().width
2024-
self.maximize()
2025-
self.__add_light_pause()
2026-
win_width = self.get_window_size()["width"]
2031+
win_width = self.evaluate("screen.availWidth;")
20272032
width_ratio = round(float(scr_width) / float(win_width), 2)
20282033
width_ratio += 0.01
20292034
if width_ratio < 0.45 or width_ratio > 2.55:
20302035
width_ratio = 1.01
20312036
sb_config._saved_width_ratio = width_ratio
2032-
self.minimize()
2033-
self.__add_light_pause()
2034-
self.__set_window_rect(win_x, win_y, width, height)
2035-
self.__add_light_pause()
2037+
with suppress(Exception):
2038+
self.get_window() # If this fails, skip the rest
2039+
self.minimize()
2040+
self.__add_light_pause()
2041+
self.__set_window_rect(win_x, win_y, width, height)
2042+
self.__add_light_pause()
20362043
x1 = x1 * width_ratio
20372044
y1 = y1 * (width_ratio - 0.02)
20382045
x2 = x2 * width_ratio
@@ -2114,15 +2121,14 @@ def gui_hover_x_y(self, x, y, timeframe=0.25):
21142121
width_ratio = sb_config._saved_width_ratio
21152122
else:
21162123
scr_width = pyautogui.size().width
2117-
self.maximize()
2118-
self.__add_light_pause()
2119-
win_width = self.get_window_size()["width"]
2124+
win_width = self.evaluate("screen.availWidth;")
21202125
width_ratio = round(float(scr_width) / float(win_width), 2)
21212126
width_ratio += 0.01
21222127
if width_ratio < 0.45 or width_ratio > 2.55:
21232128
width_ratio = 1.01
21242129
sb_config._saved_width_ratio = width_ratio
2125-
self.__set_window_rect(win_x, win_y, width, height)
2130+
with suppress(Exception):
2131+
self.__set_window_rect(win_x, win_y, width, height)
21262132
self.__add_light_pause()
21272133
self.bring_active_window_to_front()
21282134
elif (

seleniumbase/fixtures/base_case.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3577,9 +3577,18 @@ def maximize_window(self):
35773577
self.cdp.maximize()
35783578
return
35793579
self._check_browser()
3580-
self.driver.maximize_window()
3580+
try:
3581+
self.driver.maximize_window()
3582+
except Exception:
3583+
with suppress(Exception):
3584+
width = self.execute_script("return screen.availWidth;")
3585+
height = self.execute_script("return screen.availHeight;")
3586+
self.set_window_rect(0, 0, width, height)
35813587
self.__demo_mode_pause_if_active(tiny=True)
35823588

3589+
def maximize(self):
3590+
self.maximize_window()
3591+
35833592
def minimize_window(self):
35843593
self.__check_scope()
35853594
if self.__is_cdp_swap_needed():
@@ -3589,6 +3598,9 @@ def minimize_window(self):
35893598
self.driver.minimize_window()
35903599
self.__demo_mode_pause_if_active(tiny=True)
35913600

3601+
def minimize(self):
3602+
self.minimize_window()
3603+
35923604
def reset_window_size(self):
35933605
self.__check_scope()
35943606
if self.__is_cdp_swap_needed():
@@ -4344,7 +4356,7 @@ def get_new_driver(
43444356
if self.is_chromium():
43454357
try:
43464358
if self.maximize_option:
4347-
self.driver.maximize_window()
4359+
self.maximize_window()
43484360
self.wait_for_ready_state_complete()
43494361
else:
43504362
pass # Now handled in browser_launcher.py

0 commit comments

Comments
 (0)