Skip to content

Commit 4dcbf3c

Browse files
committed
Add more methods to SB
1 parent ac3a9be commit 4dcbf3c

File tree

1 file changed

+65
-3
lines changed

1 file changed

+65
-3
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3393,23 +3393,85 @@ def safe_execute_script(self, script, *args, **kwargs):
33933393
self.activate_jquery()
33943394
return self.driver.execute_script(script, *args, **kwargs)
33953395

3396+
def get_gui_element_rect(self, selector, by="css selector"):
3397+
"""Very similar to element.rect, but the x, y coordinates are
3398+
relative to the entire screen, rather than the browser window.
3399+
This is specifically for PyAutoGUI actions on the full screen.
3400+
(Note: There may be complications if iframes are involved.)"""
3401+
element = self.wait_for_element_present(selector, by=by, timeout=1)
3402+
element_rect = element.rect
3403+
e_width = element_rect["width"]
3404+
e_height = element_rect["height"]
3405+
i_x = 0
3406+
i_y = 0
3407+
iframe_switch = False
3408+
if self.__is_in_frame():
3409+
self.switch_to_parent_frame()
3410+
if self.__is_in_frame():
3411+
raise Exception("Nested iframes breaks get_gui_element_rect!")
3412+
iframe_switch = True
3413+
iframe = self.wait_for_element_present("iframe", timeout=1)
3414+
i_x = iframe.rect["x"]
3415+
i_y = iframe.rect["y"]
3416+
window_rect = self.get_window_rect()
3417+
w_bottom_y = window_rect["y"] + window_rect["height"]
3418+
viewport_height = self.execute_script("return window.innerHeight;")
3419+
x = math.ceil(window_rect["x"] + i_x + element_rect["x"])
3420+
y = math.ceil(w_bottom_y - viewport_height + i_y + element_rect["y"])
3421+
if iframe_switch:
3422+
self.switch_to_frame()
3423+
if not self.is_element_present(selector, by=by):
3424+
self.switch_to_parent_frame()
3425+
return ({"height": e_height, "width": e_width, "x": x, "y": y})
3426+
3427+
def get_gui_element_center(self, selector, by="css selector"):
3428+
"""Returns the x, y coordinates of the element's center based
3429+
on the entire GUI / screen, rather than on the browser window.
3430+
This is specifically for PyAutoGUI actions on the full screen.
3431+
(Note: There may be complications if iframes are involved.)"""
3432+
element_rect = self.get_gui_element_rect(selector, by=by)
3433+
x = int(element_rect["x"]) + int(element_rect["width"] / 2) + 1
3434+
y = int(element_rect["y"]) + int(element_rect["height"] / 2) + 1
3435+
return (x, y)
3436+
3437+
def get_window_rect(self):
3438+
self.__check_scope()
3439+
self._check_browser()
3440+
return self.driver.get_window_rect()
3441+
3442+
def get_window_size(self):
3443+
self.__check_scope()
3444+
self._check_browser()
3445+
return self.driver.get_window_size()
3446+
3447+
def get_window_position(self):
3448+
self.__check_scope()
3449+
self._check_browser()
3450+
return self.driver.get_window_position()
3451+
33963452
def set_window_rect(self, x, y, width, height):
33973453
self.__check_scope()
33983454
self._check_browser()
33993455
self.driver.set_window_rect(x, y, width, height)
3400-
self.__demo_mode_pause_if_active()
3456+
self.__demo_mode_pause_if_active(tiny=True)
34013457

34023458
def set_window_size(self, width, height):
34033459
self.__check_scope()
34043460
self._check_browser()
34053461
self.driver.set_window_size(width, height)
3406-
self.__demo_mode_pause_if_active()
3462+
self.__demo_mode_pause_if_active(tiny=True)
3463+
3464+
def set_window_position(self, x, y):
3465+
self.__check_scope()
3466+
self._check_browser()
3467+
self.driver.set_window_position(x, y)
3468+
self.__demo_mode_pause_if_active(tiny=True)
34073469

34083470
def maximize_window(self):
34093471
self.__check_scope()
34103472
self._check_browser()
34113473
self.driver.maximize_window()
3412-
self.__demo_mode_pause_if_active()
3474+
self.__demo_mode_pause_if_active(tiny=True)
34133475

34143476
def switch_to_frame(self, frame="iframe", timeout=None):
34153477
"""Wait for an iframe to appear, and switch to it. This should be

0 commit comments

Comments
 (0)