Skip to content

Commit bc9de47

Browse files
committed
Update options
1 parent 6b6db12 commit bc9de47

File tree

7 files changed

+398
-51
lines changed

7 files changed

+398
-51
lines changed

seleniumbase/core/browser_launcher.py

Lines changed: 109 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -602,16 +602,32 @@ def install_pyautogui_if_missing(driver):
602602
and not (sb_config.headless or sb_config.headless2)
603603
):
604604
from sbvirtualdisplay import Display
605-
try:
605+
xvfb_width = 1366
606+
xvfb_height = 768
607+
if (
608+
hasattr(sb_config, "_xvfb_width")
609+
and sb_config._xvfb_width
610+
and isinstance(sb_config._xvfb_width, int)
611+
and hasattr(sb_config, "_xvfb_height")
612+
and sb_config._xvfb_height
613+
and isinstance(sb_config._xvfb_height, int)
614+
):
615+
xvfb_width = sb_config._xvfb_width
616+
xvfb_height = sb_config._xvfb_height
617+
if xvfb_width < 1024:
618+
xvfb_width = 1024
619+
sb_config._xvfb_width = xvfb_width
620+
if xvfb_height < 768:
621+
xvfb_height = 768
622+
sb_config._xvfb_height = xvfb_height
623+
with suppress(Exception):
606624
xvfb_display = Display(
607625
visible=True,
608-
size=(1366, 768),
626+
size=(xvfb_width, xvfb_height),
609627
backend="xvfb",
610628
use_xauth=True,
611629
)
612630
xvfb_display.start()
613-
except Exception:
614-
pass
615631

616632

617633
def get_configured_pyautogui(pyautogui_copy):
@@ -1669,18 +1685,49 @@ def _set_chrome_options(
16691685
chrome_options.add_experimental_option(
16701686
"mobileEmulation", emulator_settings
16711687
)
1688+
# Handle Window Position
1689+
if (headless or headless2) and IS_WINDOWS:
1690+
# https://stackoverflow.com/a/78999088/7058266
1691+
chrome_options.add_argument("--window-position=-2400,-2400")
1692+
else:
1693+
if (
1694+
hasattr(settings, "WINDOW_START_X")
1695+
and isinstance(settings.WINDOW_START_X, int)
1696+
and hasattr(settings, "WINDOW_START_Y")
1697+
and isinstance(settings.WINDOW_START_Y, int)
1698+
):
1699+
chrome_options.add_argument(
1700+
"--window-position=%s,%s" % (
1701+
settings.WINDOW_START_X, settings.WINDOW_START_Y
1702+
)
1703+
)
1704+
# Handle Window Size
16721705
if headless or headless2:
1673-
chrome_options.add_argument(
1674-
"--window-size=%s,%s" % (
1675-
settings.HEADLESS_START_WIDTH, settings.HEADLESS_START_HEIGHT
1706+
if (
1707+
hasattr(settings, "HEADLESS_START_WIDTH")
1708+
and isinstance(settings.HEADLESS_START_WIDTH, int)
1709+
and hasattr(settings, "HEADLESS_START_HEIGHT")
1710+
and isinstance(settings.HEADLESS_START_HEIGHT, int)
1711+
):
1712+
chrome_options.add_argument(
1713+
"--window-size=%s,%s" % (
1714+
settings.HEADLESS_START_WIDTH,
1715+
settings.HEADLESS_START_HEIGHT,
1716+
)
16761717
)
1677-
)
16781718
else:
1679-
chrome_options.add_argument(
1680-
"--window-size=%s,%s" % (
1681-
settings.CHROME_START_WIDTH, settings.CHROME_START_HEIGHT
1719+
if (
1720+
hasattr(settings, "CHROME_START_WIDTH")
1721+
and isinstance(settings.CHROME_START_WIDTH, int)
1722+
and hasattr(settings, "CHROME_START_HEIGHT")
1723+
and isinstance(settings.CHROME_START_HEIGHT, int)
1724+
):
1725+
chrome_options.add_argument(
1726+
"--window-size=%s,%s" % (
1727+
settings.CHROME_START_WIDTH,
1728+
settings.CHROME_START_HEIGHT,
1729+
)
16821730
)
1683-
)
16841731
if (
16851732
not proxy_auth
16861733
and not disable_csp
@@ -1858,8 +1905,12 @@ def _set_chrome_options(
18581905
binary_location = binary_loc
18591906
extra_disabled_features = []
18601907
if chromium_arg:
1861-
# Can be a comma-separated list of Chromium args
1862-
chromium_arg_list = chromium_arg.split(",")
1908+
# Can be a comma-separated list of Chromium args or a list
1909+
chromium_arg_list = None
1910+
if isinstance(chromium_arg, (list, tuple)):
1911+
chromium_arg_list = chromium_arg
1912+
else:
1913+
chromium_arg_list = chromium_arg.split(",")
18631914
for chromium_arg_item in chromium_arg_list:
18641915
chromium_arg_item = chromium_arg_item.strip()
18651916
if not chromium_arg_item.startswith("--"):
@@ -3422,20 +3473,49 @@ def get_local_driver(
34223473
edge_options.add_experimental_option(
34233474
"mobileEmulation", emulator_settings
34243475
)
3476+
# Handle Window Position
3477+
if (headless or headless2) and IS_WINDOWS:
3478+
# https://stackoverflow.com/a/78999088/7058266
3479+
edge_options.add_argument("--window-position=-2400,-2400")
3480+
else:
3481+
if (
3482+
hasattr(settings, "WINDOW_START_X")
3483+
and isinstance(settings.WINDOW_START_X, int)
3484+
and hasattr(settings, "WINDOW_START_Y")
3485+
and isinstance(settings.WINDOW_START_Y, int)
3486+
):
3487+
edge_options.add_argument(
3488+
"--window-position=%s,%s" % (
3489+
settings.WINDOW_START_X, settings.WINDOW_START_Y
3490+
)
3491+
)
3492+
# Handle Window Size
34253493
if headless or headless2:
3426-
edge_options.add_argument(
3427-
"--window-size=%s,%s" % (
3428-
settings.HEADLESS_START_WIDTH,
3429-
settings.HEADLESS_START_HEIGHT,
3494+
if (
3495+
hasattr(settings, "HEADLESS_START_WIDTH")
3496+
and isinstance(settings.HEADLESS_START_WIDTH, int)
3497+
and hasattr(settings, "HEADLESS_START_HEIGHT")
3498+
and isinstance(settings.HEADLESS_START_HEIGHT, int)
3499+
):
3500+
edge_options.add_argument(
3501+
"--window-size=%s,%s" % (
3502+
settings.HEADLESS_START_WIDTH,
3503+
settings.HEADLESS_START_HEIGHT,
3504+
)
34303505
)
3431-
)
34323506
else:
3433-
edge_options.add_argument(
3434-
"--window-size=%s,%s" % (
3435-
settings.CHROME_START_WIDTH,
3436-
settings.CHROME_START_HEIGHT,
3507+
if (
3508+
hasattr(settings, "CHROME_START_WIDTH")
3509+
and isinstance(settings.CHROME_START_WIDTH, int)
3510+
and hasattr(settings, "CHROME_START_HEIGHT")
3511+
and isinstance(settings.CHROME_START_HEIGHT, int)
3512+
):
3513+
edge_options.add_argument(
3514+
"--window-size=%s,%s" % (
3515+
settings.CHROME_START_WIDTH,
3516+
settings.CHROME_START_HEIGHT,
3517+
)
34373518
)
3438-
)
34393519
if user_data_dir and not is_using_uc(undetectable, browser_name):
34403520
abs_path = os.path.abspath(user_data_dir)
34413521
edge_options.add_argument("--user-data-dir=%s" % abs_path)
@@ -3569,7 +3649,11 @@ def get_local_driver(
35693649
set_binary = False
35703650
if chromium_arg:
35713651
# Can be a comma-separated list of Chromium args
3572-
chromium_arg_list = chromium_arg.split(",")
3652+
chromium_arg_list = None
3653+
if isinstance(chromium_arg, (list, tuple)):
3654+
chromium_arg_list = chromium_arg
3655+
else:
3656+
chromium_arg_list = chromium_arg.split(",")
35733657
for chromium_arg_item in chromium_arg_list:
35743658
chromium_arg_item = chromium_arg_item.strip()
35753659
if not chromium_arg_item.startswith("--"):

seleniumbase/core/settings_parser.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ def set_settings(settings_file):
113113
settings.RAISE_INVALID_PROXY_STRING_EXCEPTION = override_settings[
114114
key
115115
]
116+
elif key == "WINDOW_START_X":
117+
settings.WINDOW_START_X = override_settings[key]
118+
elif key == "WINDOW_START_Y":
119+
settings.WINDOW_START_Y = override_settings[key]
116120
elif key == "CHROME_START_WIDTH":
117121
settings.CHROME_START_WIDTH = override_settings[key]
118122
elif key == "CHROME_START_HEIGHT":

seleniumbase/fixtures/base_case.py

Lines changed: 85 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13802,9 +13802,13 @@ def __activate_virtual_display_as_needed(self):
1380213802
if self.undetectable and not (self.headless or self.headless2):
1380313803
import Xlib.display
1380413804
try:
13805+
if not self._xvfb_width:
13806+
self._xvfb_width = 1366
13807+
if not self._xvfb_height:
13808+
self._xvfb_height = 768
1380513809
self._xvfb_display = Display(
1380613810
visible=True,
13807-
size=(1366, 768),
13811+
size=(self._xvfb_width, self._xvfb_height),
1380813812
backend="xvfb",
1380913813
use_xauth=True,
1381013814
)
@@ -14553,6 +14557,7 @@ def setUp(self, masterqa_mode=False):
1455314557
sb_config.headless_active = False
1455414558
self.headed = sb_config.headed
1455514559
self.xvfb = sb_config.xvfb
14560+
self.xvfb_metrics = sb_config.xvfb_metrics
1455614561
self.locale_code = sb_config.locale_code
1455714562
self.interval = sb_config.interval
1455814563
self.start_page = sb_config.start_page
@@ -14644,31 +14649,8 @@ def setUp(self, masterqa_mode=False):
1464414649
self.use_wire = sb_config.use_wire
1464514650
self.external_pdf = sb_config.external_pdf
1464614651
self._final_debug = sb_config.final_debug
14652+
self.window_position = sb_config.window_position
1464714653
self.window_size = sb_config.window_size
14648-
window_size = self.window_size
14649-
if window_size:
14650-
if window_size.count(",") != 1:
14651-
message = (
14652-
'\n\n window_size expects a "width,height" string!'
14653-
'\n (Your input was: "%s")\n' % window_size
14654-
)
14655-
raise Exception(message)
14656-
window_size = window_size.replace(" ", "")
14657-
width = None
14658-
height = None
14659-
try:
14660-
width = int(window_size.split(",")[0])
14661-
height = int(window_size.split(",")[1])
14662-
except Exception:
14663-
message = (
14664-
'\n\n Expecting integer values for "width,height"!'
14665-
'\n (window_size input was: "%s")\n' % window_size
14666-
)
14667-
raise Exception(message)
14668-
settings.CHROME_START_WIDTH = width
14669-
settings.CHROME_START_HEIGHT = height
14670-
settings.HEADLESS_START_WIDTH = width
14671-
settings.HEADLESS_START_HEIGHT = height
1467214654
self.maximize_option = sb_config.maximize_option
1467314655
self.save_screenshot_after_test = sb_config.save_screenshot
1467414656
self.no_screenshot_after_test = sb_config.no_screenshot
@@ -14832,9 +14814,87 @@ def setUp(self, masterqa_mode=False):
1483214814
self.mobile_emulator = True
1483314815
except Exception:
1483414816
raise Exception(exception_string)
14817+
14818+
window_position = self.window_position
14819+
if window_position:
14820+
if window_position.count(",") != 1:
14821+
message = (
14822+
'\n\n window_position expects an "x,y" string!'
14823+
'\n (Your input was: "%s")\n' % window_position
14824+
)
14825+
raise Exception(message)
14826+
window_position = window_position.replace(" ", "")
14827+
win_x = None
14828+
win_y = None
14829+
try:
14830+
win_x = int(window_position.split(",")[0])
14831+
win_y = int(window_position.split(",")[1])
14832+
except Exception:
14833+
message = (
14834+
'\n\n Expecting integer values for "x,y"!'
14835+
'\n (window_position input was: "%s")\n'
14836+
% window_position
14837+
)
14838+
raise Exception(message)
14839+
settings.WINDOW_START_X = win_x
14840+
settings.WINDOW_START_Y = win_y
14841+
14842+
window_size = self.window_size
14843+
if window_size:
14844+
if window_size.count(",") != 1:
14845+
message = (
14846+
'\n\n window_size expects a "width,height" string!'
14847+
'\n (Your input was: "%s")\n' % window_size
14848+
)
14849+
raise Exception(message)
14850+
window_size = window_size.replace(" ", "")
14851+
width = None
14852+
height = None
14853+
try:
14854+
width = int(window_size.split(",")[0])
14855+
height = int(window_size.split(",")[1])
14856+
except Exception:
14857+
message = (
14858+
'\n\n Expecting integer values for "width,height"!'
14859+
'\n (window_size input was: "%s")\n' % window_size
14860+
)
14861+
raise Exception(message)
14862+
settings.CHROME_START_WIDTH = width
14863+
settings.CHROME_START_HEIGHT = height
14864+
settings.HEADLESS_START_WIDTH = width
14865+
settings.HEADLESS_START_HEIGHT = height
14866+
14867+
if self.xvfb_metrics:
14868+
metrics_string = self.xvfb_metrics
14869+
metrics_string = metrics_string.replace(" ", "")
14870+
metrics_list = metrics_string.split(",")[0:2]
14871+
exception_string = (
14872+
"Invalid input for xvfb_metrics!\n"
14873+
"Expecting a comma-separated string\n"
14874+
"with integer values for Width/Height.\n"
14875+
'Eg. --xvfb-metrics="1920,1080".\n'
14876+
"(Minimum: 1024,768) (Default: 1366,768)"
14877+
)
14878+
if len(metrics_list) != 2:
14879+
raise Exception(exception_string)
14880+
try:
14881+
self._xvfb_width = int(metrics_list[0])
14882+
self._xvfb_height = int(metrics_list[1])
14883+
# The minimum width,height is: 1024,768
14884+
if self._xvfb_width < 1024:
14885+
self._xvfb_width = 1024
14886+
sb_config._xvfb_width = self._xvfb_width
14887+
if self._xvfb_height < 768:
14888+
self._xvfb_height = 768
14889+
sb_config._xvfb_height = self._xvfb_height
14890+
self.xvfb = True
14891+
except Exception:
14892+
raise Exception(exception_string)
14893+
1483514894
if self.mobile_emulator and not self.user_agent:
1483614895
# Use a Pixel user agent by default if not specified
1483714896
self.user_agent = constants.Mobile.AGENT
14897+
1483814898
if self.browser in ["firefox", "ie", "safari"]:
1483914899
# The Recorder Mode browser extension is only for Chrome/Edge.
1484014900
if self.recorder_mode:

0 commit comments

Comments
 (0)