Skip to content

Commit 25712d8

Browse files
authored
Merge pull request #2017 from seleniumbase/refactor-logging-and-driver-settings
Refactor logging and driver settings
2 parents 21d6a75 + d2ade91 commit 25712d8

File tree

14 files changed

+179
-68
lines changed

14 files changed

+179
-68
lines changed

examples/raw_parameter_script.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
sb.extension_zip = None
6060
sb.extension_dir = None
6161
sb.database_env = "test"
62-
sb.log_path = "latest_logs"
6362
sb.archive_logs = False
6463
sb.disable_csp = False
6564
sb.disable_ws = False

mkdocs_build/requirements.txt

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,25 @@
22
# Minimum Python version: 3.8 (for generating docs only)
33

44
regex>=2023.8.8
5-
pkginfo>=1.9.6
65
PyYAML>=6.0.1
7-
readme-renderer>=41.0
86
pymdown-extensions>=10.1
9-
importlib-metadata>=6.8.0
107
pipdeptree>=2.13.0
11-
bleach>=6.0.0
12-
docutils>=0.20.1
138
python-dateutil>=2.8.2
14-
tqdm>=4.66.1
15-
nltk>=3.8.1
16-
joblib>=1.3.2
17-
livereload==2.6.3
189
Markdown==3.4.4
10+
markdown2==2.4.10
1911
MarkupSafe==2.1.3
2012
Jinja2==3.1.2
2113
click==8.1.7
2214
ghp-import==2.1.0
23-
lunr==0.7.0.post1
24-
tornado==6.3.3
2515
watchdog==3.0.0
2616
cairocffi==1.6.1
27-
cairosvg==2.7.1
28-
cssselect2==0.7.0
29-
tinycss2==1.2.1
30-
defusedxml==0.7.1
3117
pathspec==0.11.2
18+
Babel==2.12.1
19+
paginate==0.5.6
20+
pyquery==2.0.0
21+
readtime==3.0.0
3222
mkdocs==1.5.2
33-
mkdocs-material==9.1.21
23+
mkdocs-material==9.2.1
3424
mkdocs-exclude-search==0.6.5
3525
mkdocs-simple-hooks==0.1.5
3626
mkdocs-material-extensions==1.1.1

seleniumbase/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# seleniumbase package
2-
__version__ = "4.17.8"
2+
__version__ = "4.17.9"

seleniumbase/behave/behave_sb.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def get_configured_sb(context):
168168
sb.driver_version = None
169169
sb.page_load_strategy = None
170170
sb.database_env = "test"
171-
sb.log_path = "latest_logs" + os.sep
171+
sb.log_path = constants.Logs.LATEST + os.sep
172172
sb.archive_logs = False
173173
sb.disable_js = False
174174
sb.disable_csp = False
@@ -940,7 +940,9 @@ def get_configured_sb(context):
940940
if sb_config.dash_title:
941941
constants.Dashboard.TITLE = sb_config.dash_title.replace("_", " ")
942942

943-
log_helper.log_folder_setup("latest_logs/", sb.archive_logs)
943+
log_helper.log_folder_setup(
944+
constants.Logs.LATEST + "/", sb.archive_logs
945+
)
944946
download_helper.reset_downloads_folder()
945947
proxy_helper.remove_proxy_zip_if_present()
946948
return sb
@@ -1153,7 +1155,9 @@ def _perform_behave_unconfigure_():
11531155
pass
11541156
sb_config.shared_driver = None
11551157
if hasattr(sb_config, "archive_logs"):
1156-
log_helper.archive_logs_if_set("latest_logs/", sb_config.archive_logs)
1158+
log_helper.archive_logs_if_set(
1159+
constants.Logs.LATEST + "/", sb_config.archive_logs
1160+
)
11571161
log_helper.clear_empty_logs()
11581162
# Dashboard post-processing: Disable time-based refresh and stamp complete
11591163
if not hasattr(sb_config, "dashboard") or not sb_config.dashboard:
@@ -1227,7 +1231,9 @@ def do_final_driver_cleanup_as_needed():
12271231

12281232

12291233
def _perform_behave_terminal_summary_():
1230-
latest_logs_dir = os.path.join(os.getcwd(), "latest_logs" + os.sep)
1234+
latest_logs_dir = os.path.join(
1235+
os.getcwd(), constants.Logs.LATEST + os.sep
1236+
)
12311237
dash_path = os.path.join(os.getcwd(), "dashboard.html")
12321238
equals_len = len("Dashboard: ") + len(dash_path)
12331239
try:

seleniumbase/core/browser_launcher.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from seleniumbase import drivers # webdriver storage folder for SeleniumBase
1919
from seleniumbase import extensions # browser extensions storage folder
2020
from seleniumbase.config import settings
21+
from seleniumbase.core import detect_b_ver
2122
from seleniumbase.core import download_helper
2223
from seleniumbase.core import proxy_helper
2324
from seleniumbase.fixtures import constants
@@ -190,8 +191,10 @@ def has_cf(text):
190191
if (
191192
"<title>Just a moment...</title>" in text
192193
or "<title>403 Forbidden</title>" in text
194+
or "Permission Denied</title>" in text
193195
or 'id="challenge-error-text"' in text
194196
or 'action="/?__cf_chl_f_tk' in text
197+
or 'src="chromedriver.js"' in text
195198
or 'id="challenge-form"' in text
196199
or "window._cf_chl_opt" in text
197200
):
@@ -218,11 +221,11 @@ def uc_special_open_if_cf(driver, url, proxy_string=None):
218221
pass
219222
if special:
220223
with driver:
221-
time.sleep(0.2)
224+
time.sleep(0.18)
222225
driver.execute_script('window.open("%s","_blank");' % url)
223226
driver.close()
224227
driver.switch_to.window(driver.window_handles[-1])
225-
time.sleep(0.2)
228+
time.sleep(0.02)
226229
else:
227230
driver.open(url) # The original one
228231
else:
@@ -233,9 +236,9 @@ def uc_special_open_if_cf(driver, url, proxy_string=None):
233236
def uc_open(driver, url):
234237
if (url.startswith("http:") or url.startswith("https:")):
235238
with driver:
236-
time.sleep(0.2)
239+
time.sleep(0.18)
237240
driver.open(url)
238-
time.sleep(0.2)
241+
time.sleep(0.02)
239242
else:
240243
driver.open(url) # The original one
241244
return None
@@ -244,11 +247,11 @@ def uc_open(driver, url):
244247
def uc_open_with_tab(driver, url):
245248
if (url.startswith("http:") or url.startswith("https:")):
246249
with driver:
247-
time.sleep(0.2)
250+
time.sleep(0.18)
248251
driver.execute_script('window.open("%s","_blank");' % url)
249252
driver.close()
250253
driver.switch_to.window(driver.window_handles[-1])
251-
time.sleep(0.2)
254+
time.sleep(0.02)
252255
else:
253256
driver.open(url) # The original one
254257
return None
@@ -312,7 +315,7 @@ def get_valid_binary_names_for_browser(browser):
312315
else:
313316
raise Exception("Could not determine OS, or unsupported!")
314317
else:
315-
raise Exception("Invalid combination for os browser binaries!")
318+
raise Exception("Invalid combination for OS browser binaries!")
316319

317320

318321
def _repair_chromedriver(chrome_options, headless_options, mcv=None):
@@ -870,7 +873,12 @@ def _set_chrome_options(
870873
)
871874
except Exception:
872875
pass
873-
if len(chromium_arg_item) >= 3:
876+
if "set-binary" in chromium_arg_item and not binary_location:
877+
br_app = "google-chrome"
878+
binary_loc = detect_b_ver.get_binary_location(br_app)
879+
if os.path.exists(binary_loc):
880+
binary_location = binary_loc
881+
elif len(chromium_arg_item) >= 3:
874882
chrome_options.add_argument(chromium_arg_item)
875883
if devtools and not headless:
876884
chrome_options.add_argument("--auto-open-devtools-for-tabs")
@@ -1166,6 +1174,8 @@ def get_driver(
11661174
"that includes the driver filename at the end of it!"
11671175
"\n(Will use default settings...)\n" % binary_location
11681176
)
1177+
# Example of a valid binary location path - MacOS:
1178+
# "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
11691179
binary_location = None
11701180
else:
11711181
binary_name = binary_location.split("/")[-1].split("\\")[-1]
@@ -2251,8 +2261,6 @@ def get_local_driver(
22512261
use_version = "latest"
22522262
major_edge_version = None
22532263
try:
2254-
from seleniumbase.core import detect_b_ver
2255-
22562264
if binary_location:
22572265
try:
22582266
major_edge_version = (
@@ -2480,8 +2488,6 @@ def get_local_driver(
24802488
)
24812489
edge_options.add_argument("--disable-browser-side-navigation")
24822490
edge_options.add_argument("--disable-translate")
2483-
if binary_location:
2484-
edge_options.binary_location = binary_location
24852491
if not enable_ws:
24862492
edge_options.add_argument("--disable-web-security")
24872493
edge_options.add_argument("--homepage=about:blank")
@@ -2585,8 +2591,15 @@ def get_local_driver(
25852591
chromium_arg_item = "-" + chromium_arg_item
25862592
else:
25872593
chromium_arg_item = "--" + chromium_arg_item
2588-
if len(chromium_arg_item) >= 3:
2594+
if "set-binary" in chromium_arg_item and not binary_location:
2595+
br_app = "edge"
2596+
binary_loc = detect_b_ver.get_binary_location(br_app)
2597+
if os.path.exists(binary_loc):
2598+
binary_location = binary_loc
2599+
elif len(chromium_arg_item) >= 3:
25892600
edge_options.add_argument(chromium_arg_item)
2601+
if binary_location:
2602+
edge_options.binary_location = binary_location
25902603
if selenium4_or_newer:
25912604
try:
25922605
service = EdgeService(
@@ -2884,8 +2897,6 @@ def get_local_driver(
28842897
major_chrome_version = None
28852898
if selenium4_or_newer:
28862899
try:
2887-
from seleniumbase.core import detect_b_ver
2888-
28892900
if binary_location:
28902901
try:
28912902
major_chrome_version = (

seleniumbase/core/detect_b_ver.py

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,68 @@ def windows_browser_apps_to_cmd(*apps):
106106
return '%s -NoProfile "%s"' % (powershell, script)
107107

108108

109+
def get_binary_location(browser_type):
110+
cmd_mapping = {
111+
ChromeType.GOOGLE: {
112+
OSType.LINUX: linux_browser_apps_to_cmd(
113+
"google-chrome-stable",
114+
"google-chrome",
115+
"chrome",
116+
"chromium",
117+
"chromium-browser",
118+
"google-chrome-beta",
119+
"google-chrome-dev",
120+
"google-chrome-unstable",
121+
),
122+
OSType.MAC: r"/Applications/Google Chrome.app"
123+
r"/Contents/MacOS/Google Chrome",
124+
OSType.WIN: windows_browser_apps_to_cmd(
125+
r'(Get-Item -Path "$env:PROGRAMFILES\Google\Chrome'
126+
r'\Application\chrome.exe")',
127+
r'(Get-Item -Path "$env:PROGRAMFILES (x86)\Google\Chrome'
128+
r'\Application\chrome.exe")',
129+
r'(Get-Item -Path "$env:LOCALAPPDATA\Google\Chrome'
130+
r'\Application\chrome.exe")',
131+
),
132+
},
133+
ChromeType.MSEDGE: {
134+
OSType.LINUX: linux_browser_apps_to_cmd(
135+
"microsoft-edge-stable",
136+
"microsoft-edge",
137+
"microsoft-edge-beta",
138+
"microsoft-edge-dev",
139+
),
140+
OSType.MAC: r"/Applications/Microsoft Edge.app"
141+
r"/Contents/MacOS/Microsoft Edge",
142+
OSType.WIN: windows_browser_apps_to_cmd(
143+
# stable edge
144+
r'(Get-Item -Path "$env:PROGRAMFILES\Microsoft\Edge'
145+
r'\Application\msedge.exe")',
146+
r'(Get-Item -Path "$env:PROGRAMFILES (x86)\Microsoft'
147+
r'\Edge\Application\msedge.exe")',
148+
# beta edge
149+
r'(Get-Item -Path "$env:LOCALAPPDATA\Microsoft\Edge Beta'
150+
r'\Application\msedge.exe")',
151+
r'(Get-Item -Path "$env:PROGRAMFILES\Microsoft\Edge Beta'
152+
r'\Application\msedge.exe")',
153+
r'(Get-Item -Path "$env:PROGRAMFILES (x86)\Microsoft\Edge Beta'
154+
r'\Application\msedge.exe")',
155+
# dev edge
156+
r'(Get-Item -Path "$env:LOCALAPPDATA\Microsoft\Edge Dev'
157+
r'\Application\msedge.exe")',
158+
r'(Get-Item -Path "$env:PROGRAMFILES\Microsoft\Edge Dev'
159+
r'\Application\msedge.exe")',
160+
r'(Get-Item -Path "$env:PROGRAMFILES (x86)\Microsoft\Edge Dev'
161+
r'\Application\msedge.exe")',
162+
# canary edge
163+
r'(Get-Item -Path "$env:LOCALAPPDATA\Microsoft\Edge SxS'
164+
r'\Application\msedge.exe")',
165+
),
166+
},
167+
}
168+
return cmd_mapping[browser_type][os_name()]
169+
170+
109171
def get_browser_version_from_binary(binary_location):
110172
try:
111173
if binary_location.count(r"\ ") != binary_location.count(" "):
@@ -118,18 +180,19 @@ def get_browser_version_from_binary(binary_location):
118180
return None
119181

120182

121-
def get_browser_version_from_os(browser_type=None):
183+
def get_browser_version_from_os(browser_type):
122184
"""Return installed browser version."""
123185
cmd_mapping = {
124186
ChromeType.GOOGLE: {
125187
OSType.LINUX: linux_browser_apps_to_cmd(
126-
"google-chrome",
127188
"google-chrome-stable",
189+
"google-chrome",
128190
"chrome",
129191
"chromium",
192+
"chromium-browser",
130193
"google-chrome-beta",
131194
"google-chrome-dev",
132-
"chromium-browser",
195+
"google-chrome-unstable",
133196
),
134197
OSType.MAC: r"/Applications/Google\ Chrome.app"
135198
r"/Contents/MacOS/Google\ Chrome --version",
@@ -149,8 +212,8 @@ def get_browser_version_from_os(browser_type=None):
149212
},
150213
ChromeType.MSEDGE: {
151214
OSType.LINUX: linux_browser_apps_to_cmd(
152-
"microsoft-edge",
153215
"microsoft-edge-stable",
216+
"microsoft-edge",
154217
"microsoft-edge-beta",
155218
"microsoft-edge-dev",
156219
),

seleniumbase/core/download_helper.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
# The "downloads_folder" is cleaned out at the start of each pytest run,
1212
# but there is an option to save existing files in "archived_files".
1313
DOWNLOADS_DIR = constants.Files.DOWNLOADS_FOLDER
14-
ARCHIVE_DIR = constants.Files.ARCHIVED_DOWNLOADS_FOLDER
15-
1614
abs_path = os.path.abspath(".")
1715
downloads_path = os.path.join(abs_path, DOWNLOADS_DIR)
1816

@@ -24,7 +22,19 @@ def get_downloads_folder():
2422
def reset_downloads_folder():
2523
"""Clears the downloads folder.
2624
If settings.ARCHIVE_EXISTING_DOWNLOADS is set to True, archives it."""
27-
archived_downloads_folder = os.path.join(os.getcwd(), ARCHIVE_DIR) + os.sep
25+
downloads_dir = constants.Files.DOWNLOADS_FOLDER
26+
archive_dir = constants.Files.ARCHIVED_DOWNLOADS_FOLDER
27+
if downloads_dir.endswith("/"):
28+
downloads_dir = downloads_dir[:-1]
29+
if downloads_dir.startswith("/"):
30+
downloads_dir = downloads_dir[1:]
31+
if archive_dir.endswith("/"):
32+
archive_dir = archive_dir[:-1]
33+
if archive_dir.startswith("/"):
34+
archive_dir = archive_dir[1:]
35+
if len(downloads_dir) < 10 or len(archive_dir) < 10:
36+
return # Prevent accidental deletions if constants are renamed
37+
archived_downloads_folder = os.path.join(os.getcwd(), archive_dir) + os.sep
2838
if os.path.exists(downloads_path) and not os.listdir(downloads_path) == []:
2939
reset_downloads_folder_assistant(archived_downloads_folder)
3040
if os.path.exists(downloads_path) and os.listdir(downloads_path) == []:

0 commit comments

Comments
 (0)