Skip to content

Commit 59efc8f

Browse files
committed
Refactor logging
1 parent 21d6a75 commit 59efc8f

File tree

9 files changed

+70
-27
lines changed

9 files changed

+70
-27
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

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/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) == []:

seleniumbase/core/log_helper.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,8 @@ def archive_logs_if_set(log_path, archive_logs=False):
517517
else:
518518
if settings.ARCHIVE_EXISTING_LOGS or archive_logs:
519519
if len(os.listdir(log_path)) > 0:
520-
archived_folder = "%s/../archived_logs/" % log_path
521-
archived_folder = os.path.realpath(archived_folder) + "/"
520+
saved_folder = "%s/../%s/" % (log_path, constants.Logs.SAVED)
521+
archived_folder = os.path.realpath(saved_folder) + "/"
522522
log_path = os.path.realpath(log_path) + "/"
523523
if not os.path.exists(archived_folder):
524524
try:
@@ -531,17 +531,25 @@ def archive_logs_if_set(log_path, archive_logs=False):
531531

532532

533533
def log_folder_setup(log_path, archive_logs=False):
534-
"""Handle Logging"""
534+
"""Clean up logs to prepare for another run"""
535535
if log_path.endswith("/"):
536536
log_path = log_path[:-1]
537+
if log_path.startswith("/"):
538+
log_path = log_path[1:]
539+
if constants.Logs.SAVED.endswith("/"):
540+
constants.Logs.SAVED = constants.Logs.SAVED[:-1]
541+
if constants.Logs.SAVED.startswith("/"):
542+
constants.Logs.SAVED = constants.Logs.SAVED[1:]
543+
if len(log_path) < 10 or len(constants.Logs.SAVED) < 10:
544+
return # Prevent accidental deletions if constants are renamed
537545
if not os.path.exists(log_path):
538546
try:
539547
os.makedirs(log_path)
540548
except Exception:
541549
pass # Should only be reachable during multi-threaded runs
542550
else:
543-
archived_folder = "%s/../archived_logs/" % log_path
544-
archived_folder = os.path.realpath(archived_folder) + "/"
551+
saved_folder = "%s/../%s/" % (log_path, constants.Logs.SAVED)
552+
archived_folder = os.path.realpath(saved_folder) + "/"
545553
if not os.path.exists(archived_folder):
546554
try:
547555
os.makedirs(archived_folder)
@@ -566,8 +574,8 @@ def log_folder_setup(log_path, archive_logs=False):
566574

567575

568576
def clear_empty_logs():
569-
latest_logs_dir = os.path.join(os.getcwd(), "latest_logs") + os.sep
570-
archived_folder = os.path.join(os.getcwd(), "archived_logs") + os.sep
577+
latest_logs_dir = os.path.join(os.getcwd(), constants.Logs.LATEST) + os.sep
578+
archived_folder = os.path.join(os.getcwd(), constants.Logs.SAVED) + os.sep
571579
if os.path.exists(latest_logs_dir) and not os.listdir(latest_logs_dir):
572580
try:
573581
os.rmdir(latest_logs_dir)

seleniumbase/fixtures/base_case.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13776,8 +13776,9 @@ def setUp(self, masterqa_mode=False):
1377613776
return # This test already called setUp()
1377713777
self.__called_setup = True
1377813778
self.__called_teardown = False
13779-
self.masterqa_mode = masterqa_mode
1378013779
self.is_pytest = None
13780+
self.log_path = constants.Logs.LATEST
13781+
self.masterqa_mode = masterqa_mode
1378113782
try:
1378213783
# This raises an exception if the test is not coming from pytest
1378313784
self.is_pytest = sb_config.is_pytest
@@ -15007,7 +15008,7 @@ def __process_dashboard(self, has_exception, init=False):
1500715008
else:
1500815009
status += " All tests were skipped!"
1500915010
else:
15010-
latest_logs_dir = "latest_logs/"
15011+
latest_logs_dir = constants.Logs.LATEST + "/"
1501115012
log_msg = "See latest logs for details"
1501215013
if num_failed == 1:
1501315014
status += (

seleniumbase/fixtures/constants.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,23 @@ class PageLoadStrategy:
7272

7373

7474
class Files:
75+
# This is a special downloads folder for files downloaded by tests.
76+
# The "downloaded_files" folder is DELETED when starting new tests.
77+
# Add "--archive-downloads" to save a copy in "archived_files".
78+
# (These folder names should NOT be changed.)
7579
DOWNLOADS_FOLDER = "downloaded_files"
7680
ARCHIVED_DOWNLOADS_FOLDER = "archived_files"
7781

7882

83+
class Logs:
84+
# This is where log files from the latest run get saved.
85+
# The "latest_logs" folder is DELETED when starting new tests.
86+
# Add "--archive-logs" to save a copy of logs in "archived_logs".
87+
# (These folder names should NOT be changed.)
88+
LATEST = "latest_logs"
89+
SAVED = "archived_logs"
90+
91+
7992
class Presentations:
8093
SAVED_FOLDER = "saved_presentations"
8194

seleniumbase/plugins/base_plugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def options(self, parser, env):
132132
"--log_path",
133133
"--log-path",
134134
dest="log_path",
135-
default="latest_logs/",
135+
default=constants.Logs.LATEST + "/",
136136
help="""(DEPRECATED) - This field is NOT EDITABLE anymore.
137137
Log files are saved to the "latest_logs/" folder.""",
138138
)
@@ -193,7 +193,7 @@ def configure(self, options, conf):
193193
self.page_results_list = []
194194
self.test_count = 0
195195
self.import_error = False
196-
log_path = "latest_logs/"
196+
log_path = constants.Logs.LATEST + "/"
197197
archive_logs = options.archive_logs
198198
log_helper.log_folder_setup(log_path, archive_logs)
199199
download_helper.reset_downloads_folder()

seleniumbase/plugins/pytest_plugin.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ def pytest_addoption(parser):
323323
"--log_path",
324324
"--log-path",
325325
dest="log_path",
326-
default="latest_logs/",
326+
default=constants.Logs.LATEST + "/",
327327
help="""(DEPRECATED) - This value is NOT EDITABLE anymore.
328328
Log files are saved to the "latest_logs/" folder.""",
329329
)
@@ -1487,7 +1487,7 @@ def pytest_configure(config):
14871487
sb_config.settings_file = config.getoption("settings_file")
14881488
sb_config.user_data_dir = config.getoption("user_data_dir")
14891489
sb_config.database_env = config.getoption("database_env")
1490-
sb_config.log_path = "latest_logs/" # (No longer editable!)
1490+
sb_config.log_path = constants.Logs.LATEST + "/"
14911491
sb_config.archive_logs = config.getoption("archive_logs")
14921492
if config.getoption("archive_downloads"):
14931493
settings.ARCHIVE_EXISTING_DOWNLOADS = True
@@ -1708,7 +1708,9 @@ def pytest_configure(config):
17081708
from seleniumbase.core import download_helper
17091709
from seleniumbase.core import proxy_helper
17101710

1711-
log_helper.log_folder_setup("latest_logs/", sb_config.archive_logs)
1711+
log_helper.log_folder_setup(
1712+
constants.Logs.LATEST + "/", sb_config.archive_logs
1713+
)
17121714
download_helper.reset_downloads_folder()
17131715
proxy_helper.remove_proxy_zip_if_present()
17141716

@@ -1810,7 +1812,9 @@ def pytest_collection_finish(session):
18101812
from seleniumbase.core import download_helper
18111813
from seleniumbase.core import proxy_helper
18121814

1813-
log_helper.log_folder_setup("latest_logs/", sb_config.archive_logs)
1815+
log_helper.log_folder_setup(
1816+
constants.Logs.LATEST + "/", sb_config.archive_logs
1817+
)
18141818
download_helper.reset_downloads_folder()
18151819
proxy_helper.remove_proxy_zip_if_present()
18161820
if sb_config.dashboard and len(session.items) > 0:
@@ -1958,7 +1962,7 @@ def pytest_terminal_summary(terminalreporter):
19581962
return
19591963
if not sb_config._multithreaded and not sb_config._sbase_detected:
19601964
return
1961-
latest_logs_dir = os.path.join(os.getcwd(), "latest_logs") + os.sep
1965+
latest_logs_dir = os.path.join(os.getcwd(), constants.Logs.LATEST) + os.sep
19621966
if (
19631967
"failed" in terminalreporter.stats.keys()
19641968
and os.path.exists(latest_logs_dir)
@@ -2016,7 +2020,9 @@ def _perform_pytest_unconfigure_():
20162020
pass
20172021
sb_config.shared_driver = None
20182022
if hasattr(sb_config, "log_path") and sb_config.item_count > 0:
2019-
log_helper.archive_logs_if_set("latest_logs/", sb_config.archive_logs)
2023+
log_helper.archive_logs_if_set(
2024+
constants.Logs.LATEST + "/", sb_config.archive_logs
2025+
)
20202026
log_helper.clear_empty_logs()
20212027
# Dashboard post-processing: Disable time-based refresh and stamp complete
20222028
if not hasattr(sb_config, "dashboard") or not sb_config.dashboard:

seleniumbase/plugins/sb_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ def SB(
636636
sb_config.extension_zip = extension_zip
637637
sb_config.extension_dir = extension_dir
638638
sb_config.database_env = "test"
639-
sb_config.log_path = "latest_logs"
639+
sb_config.log_path = constants.Logs.LATEST
640640
sb_config.archive_logs = archive_logs
641641
sb_config.disable_csp = disable_csp
642642
sb_config.disable_ws = disable_ws
@@ -834,7 +834,7 @@ def SB(
834834
from seleniumbase.core import download_helper
835835
from seleniumbase.core import proxy_helper
836836

837-
log_helper.log_folder_setup("latest_logs/")
837+
log_helper.log_folder_setup(constants.Logs.LATEST + "/")
838838
log_helper.clear_empty_logs()
839839
download_helper.reset_downloads_folder()
840840
if not sb_config.multi_proxy:

0 commit comments

Comments
 (0)