Skip to content

Commit

Permalink
Merge pull request #2014 from seleniumbase/cleaner-logging
Browse files Browse the repository at this point in the history
Cleaner Logging
  • Loading branch information
mdmintz authored Aug 18, 2023
2 parents b3dd883 + 591f86f commit 21d6a75
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 37 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,7 @@ pytest --reruns=1 --reruns-delay=1
<p><div><a href="https://github.com/mdmintz">https://github.com/mdmintz</a></div></p>
<div><a href="https://github.com/seleniumbase/SeleniumBase/"><img src="https://seleniumbase.github.io/cdn/img/fancy_logo_14.png" title="SeleniumBase" width="220" /></a></div> <div><a href="https://github.com/seleniumbase/SeleniumBase/blob/master/LICENSE"><img src="https://img.shields.io/badge/license-MIT-22BBCC.svg" title="SeleniumBase" /></a> <a href="https://gitter.im/seleniumbase/SeleniumBase" target="_blank"><img src="https://img.shields.io/gitter/room/seleniumbase/SeleniumBase.svg" alt="Gitter chat"/></a></div> <div><a href="https://github.com/seleniumbase/SeleniumBase"><img src="https://img.shields.io/badge/tested%20with-SeleniumBase-04C38E.svg" alt="Tested with SeleniumBase" /></a></div> <div><a href="https://seleniumbase.io"><img src="https://img.shields.io/badge/docs-seleniumbase.io-11BBAA.svg" alt="SeleniumBase Docs" /></a></div>
<div><a href="https://pepy.tech/project/seleniumbase" target="_blank"><img src="https://pepy.tech/badge/seleniumbase" alt="SeleniumBase PyPI downloads" /></a></div>
<div><a href="https://pepy.tech/project/seleniumbase" target="_blank"><img src="https://static.pepy.tech/badge/seleniumbase" alt="SeleniumBase PyPI downloads" /></a></div>
<div><a href="https://github.com/seleniumbase/SeleniumBase/stargazers"><img src="https://img.shields.io/github/stars/seleniumbase/seleniumbase.svg?color=19A57B" title="Stargazers" /></a></div>
--------
Expand Down
6 changes: 3 additions & 3 deletions mkdocs_build/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
regex>=2023.8.8
pkginfo>=1.9.6
PyYAML>=6.0.1
readme-renderer>=40.0
readme-renderer>=41.0
pymdown-extensions>=10.1
importlib-metadata>=6.8.0
pipdeptree>=2.13.0
Expand All @@ -18,9 +18,9 @@ livereload==2.6.3
Markdown==3.4.4
MarkupSafe==2.1.3
Jinja2==3.1.2
click==8.1.6
click==8.1.7
ghp-import==2.1.0
lunr==0.6.2
lunr==0.7.0.post1
tornado==6.3.3
watchdog==3.0.0
cairocffi==1.6.1
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ packaging>=21.3;python_version<"3.7"
packaging>=23.1;python_version>="3.7"
setuptools>=59.6.0;python_version<"3.7"
setuptools>=68.0.0;python_version>="3.7" and python_version<"3.8"
setuptools>=68.1.0;python_version>="3.8"
setuptools>=68.1.2;python_version>="3.8"
wheel>=0.37.1;python_version<"3.7"
wheel>=0.41.1;python_version>="3.7"
attrs==22.1.0;python_version<"3.7"
Expand Down
2 changes: 1 addition & 1 deletion seleniumbase/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# seleniumbase package
__version__ = "4.17.7"
__version__ = "4.17.8"
9 changes: 4 additions & 5 deletions seleniumbase/behave/behave_sb.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ def get_configured_sb(context):
if sb_config.dash_title:
constants.Dashboard.TITLE = sb_config.dash_title.replace("_", " ")

log_helper.log_folder_setup(sb.log_path, sb.archive_logs)
log_helper.log_folder_setup("latest_logs/", sb.archive_logs)
download_helper.reset_downloads_folder()
proxy_helper.remove_proxy_zip_if_present()
return sb
Expand Down Expand Up @@ -1152,10 +1152,9 @@ def _perform_behave_unconfigure_():
except Exception:
pass
sb_config.shared_driver = None
if hasattr(sb_config, "log_path"):
log_helper.archive_logs_if_set(
sb_config.log_path, sb_config.archive_logs
)
if hasattr(sb_config, "archive_logs"):
log_helper.archive_logs_if_set("latest_logs/", sb_config.archive_logs)
log_helper.clear_empty_logs()
# Dashboard post-processing: Disable time-based refresh and stamp complete
if not hasattr(sb_config, "dashboard") or not sb_config.dashboard:
# Done with "behave_unconfigure" unless using the Dashboard
Expand Down
17 changes: 14 additions & 3 deletions seleniumbase/core/download_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,22 @@ def get_downloads_folder():
def reset_downloads_folder():
"""Clears the downloads folder.
If settings.ARCHIVE_EXISTING_DOWNLOADS is set to True, archives it."""
archived_downloads_folder = os.path.join(os.getcwd(), ARCHIVE_DIR) + os.sep
if os.path.exists(downloads_path) and not os.listdir(downloads_path) == []:
archived_downloads_folder = os.path.join(
downloads_path, "..", ARCHIVE_DIR
)
reset_downloads_folder_assistant(archived_downloads_folder)
if os.path.exists(downloads_path) and os.listdir(downloads_path) == []:
try:
os.rmdir(downloads_path)
except OSError:
pass
if (
os.path.exists(archived_downloads_folder)
and os.listdir(archived_downloads_folder) == []
):
try:
os.rmdir(archived_downloads_folder)
except OSError:
pass


def reset_downloads_folder_assistant(archived_downloads_folder):
Expand Down
15 changes: 15 additions & 0 deletions seleniumbase/core/log_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,3 +563,18 @@ def log_folder_setup(log_path, archive_logs=False):
pass
else:
shutil.rmtree(archived_logs) # (Archive test run later)


def clear_empty_logs():
latest_logs_dir = os.path.join(os.getcwd(), "latest_logs") + os.sep
archived_folder = os.path.join(os.getcwd(), "archived_logs") + os.sep
if os.path.exists(latest_logs_dir) and not os.listdir(latest_logs_dir):
try:
os.rmdir(latest_logs_dir)
except OSError:
pass
if os.path.exists(archived_folder) and not os.listdir(archived_folder):
try:
os.rmdir(archived_folder)
except OSError:
pass
10 changes: 5 additions & 5 deletions seleniumbase/fixtures/base_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ def click(
self.__shadow_click(selector, timeout)
return
if self.__needs_minimum_wait() or self.browser == "safari":
time.sleep(0.022)
time.sleep(0.03)
element = page_actions.wait_for_element_visible(
self.driver,
selector,
Expand Down Expand Up @@ -628,20 +628,20 @@ def click(
except Exception:
pass
if self.__needs_minimum_wait() or self.browser == "safari":
time.sleep(0.026)
time.sleep(0.03)
try:
if self.driver.current_url != pre_action_url:
self.__ad_block_as_needed()
self.__disable_beforeunload_as_needed()
if self.__needs_minimum_wait():
time.sleep(0.026)
time.sleep(0.03)
except Exception:
try:
self.wait_for_ready_state_complete()
except Exception:
pass
if self.__needs_minimum_wait():
time.sleep(0.026)
time.sleep(0.03)
else:
time.sleep(0.08)
if self.demo_mode:
Expand Down Expand Up @@ -12595,7 +12595,7 @@ def __click_with_offset(
self.__scroll_to_element(element, selector, by)
self.wait_for_ready_state_complete()
if self.__needs_minimum_wait():
time.sleep(0.025)
time.sleep(0.03)
if self.demo_mode and mark is None:
mark = True
if mark:
Expand Down
3 changes: 2 additions & 1 deletion seleniumbase/fixtures/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ class MultiBrowser:
DRIVER_FIXING_LOCK = Files.DOWNLOADS_FOLDER + "/driver_fixing.lock"
DRIVER_REPAIRED = Files.DOWNLOADS_FOLDER + "/driver_fixed.lock"
CERT_FIXING_LOCK = Files.DOWNLOADS_FOLDER + "/cert_fixing.lock"
DOWNLOAD_FILE_LOCK = Files.DOWNLOADS_FOLDER + "/download_file.lock"
DOWNLOAD_FILE_LOCK = Files.DOWNLOADS_FOLDER + "/downloading.lock"
FILE_IO_LOCK = Files.DOWNLOADS_FOLDER + "/file_io.lock"


class SavedCookies:
Expand Down
18 changes: 9 additions & 9 deletions seleniumbase/fixtures/page_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,10 @@ def _download_file_to(file_url, destination_folder, new_file_name=None):


def _save_data_as(data, destination_folder, file_name):
download_file_lock = fasteners.InterProcessLock(
constants.MultiBrowser.DOWNLOAD_FILE_LOCK
file_io_lock = fasteners.InterProcessLock(
constants.MultiBrowser.FILE_IO_LOCK
)
with download_file_lock:
with file_io_lock:
out_file = codecs.open(
os.path.join(destination_folder, file_name), "w+", encoding="utf-8"
)
Expand All @@ -268,10 +268,10 @@ def _save_data_as(data, destination_folder, file_name):


def _append_data_to_file(data, destination_folder, file_name):
download_file_lock = fasteners.InterProcessLock(
constants.MultiBrowser.DOWNLOAD_FILE_LOCK
file_io_lock = fasteners.InterProcessLock(
constants.MultiBrowser.FILE_IO_LOCK
)
with download_file_lock:
with file_io_lock:
existing_data = ""
if os.path.exists(os.path.join(destination_folder, file_name)):
with open(os.path.join(destination_folder, file_name), "r") as f:
Expand All @@ -286,10 +286,10 @@ def _append_data_to_file(data, destination_folder, file_name):


def _get_file_data(folder, file_name):
download_file_lock = fasteners.InterProcessLock(
constants.MultiBrowser.DOWNLOAD_FILE_LOCK
file_io_lock = fasteners.InterProcessLock(
constants.MultiBrowser.FILE_IO_LOCK
)
with download_file_lock:
with file_io_lock:
if not os.path.exists(os.path.join(folder, file_name)):
raise Exception("File not found!")
with open(os.path.join(folder, file_name), "r") as f:
Expand Down
1 change: 1 addition & 0 deletions seleniumbase/plugins/base_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ def finalize(self, result):
log_helper.archive_logs_if_set(
self.options.log_path, self.options.archive_logs
)
log_helper.clear_empty_logs()
if self.report_on:
if not self.import_error:
report_helper.add_bad_page_log_file(self.page_results_list)
Expand Down
9 changes: 4 additions & 5 deletions seleniumbase/plugins/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1708,7 +1708,7 @@ def pytest_configure(config):
from seleniumbase.core import download_helper
from seleniumbase.core import proxy_helper

log_helper.log_folder_setup(sb_config.log_path, sb_config.archive_logs)
log_helper.log_folder_setup("latest_logs/", sb_config.archive_logs)
download_helper.reset_downloads_folder()
proxy_helper.remove_proxy_zip_if_present()

Expand Down Expand Up @@ -1810,7 +1810,7 @@ def pytest_collection_finish(session):
from seleniumbase.core import download_helper
from seleniumbase.core import proxy_helper

log_helper.log_folder_setup(sb_config.log_path, sb_config.archive_logs)
log_helper.log_folder_setup("latest_logs/", sb_config.archive_logs)
download_helper.reset_downloads_folder()
proxy_helper.remove_proxy_zip_if_present()
if sb_config.dashboard and len(session.items) > 0:
Expand Down Expand Up @@ -2016,9 +2016,8 @@ def _perform_pytest_unconfigure_():
pass
sb_config.shared_driver = None
if hasattr(sb_config, "log_path") and sb_config.item_count > 0:
log_helper.archive_logs_if_set(
sb_config.log_path, sb_config.archive_logs
)
log_helper.archive_logs_if_set("latest_logs/", sb_config.archive_logs)
log_helper.clear_empty_logs()
# Dashboard post-processing: Disable time-based refresh and stamp complete
if not hasattr(sb_config, "dashboard") or not sb_config.dashboard:
# Done with "pytest_unconfigure" unless using the Dashboard
Expand Down
5 changes: 3 additions & 2 deletions seleniumbase/plugins/sb_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def SB(
interval=None, # SECONDS (Autoplay interval for SB Slides & Tour steps.)
time_limit=None, # SECONDS (Safely fail tests that exceed the time limit.)
):
import os
import sys
import time
import traceback
Expand Down Expand Up @@ -809,7 +810,6 @@ def SB(
terminal_width = shared_utils.get_terminal_width()
if test:
import colorama
import os

colorama.init(autoreset=True)
c1 = colorama.Fore.GREEN
Expand All @@ -834,7 +834,8 @@ def SB(
from seleniumbase.core import download_helper
from seleniumbase.core import proxy_helper

log_helper.log_folder_setup(sb_config.log_path)
log_helper.log_folder_setup("latest_logs/")
log_helper.clear_empty_logs()
download_helper.reset_downloads_folder()
if not sb_config.multi_proxy:
proxy_helper.remove_proxy_zip_if_present()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
'packaging>=23.1;python_version>="3.7"',
'setuptools>=59.6.0;python_version<"3.7"',
'setuptools>=68.0.0;python_version>="3.7" and python_version<"3.8"',
'setuptools>=68.1.0;python_version>="3.8"',
'setuptools>=68.1.2;python_version>="3.8"',
'wheel>=0.37.1;python_version<"3.7"',
'wheel>=0.41.1;python_version>="3.7"',
'attrs==22.1.0;python_version<"3.7"',
Expand Down

0 comments on commit 21d6a75

Please sign in to comment.