Refactoring

This commit is contained in:
Michael Mintz 2022-10-03 23:43:37 -04:00
parent d7c2e14b2a
commit b4b2180ab1
8 changed files with 154 additions and 47 deletions

View File

@ -1082,8 +1082,17 @@ def _perform_behave_unconfigure_():
)
find_it_3 = '<td class="col-result">Untested</td>'
swap_with_3 = '<td class="col-result">Unreported</td>'
find_it_4 = 'href="%s"' % constants.Dashboard.get_dash_pie_1()
swap_with_4 = 'href="%s"' % constants.Dashboard.get_dash_pie_2()
if sys.version_info[0] >= 3:
# These use caching to prevent extra method calls
DASH_PIE_PNG_1 = constants.Dashboard.get_dash_pie_1()
DASH_PIE_PNG_2 = constants.Dashboard.get_dash_pie_2()
else:
from seleniumbase.core import encoded_images
DASH_PIE_PNG_1 = encoded_images.get_dash_pie_png1()
DASH_PIE_PNG_2 = encoded_images.get_dash_pie_png2()
find_it_4 = 'href="%s"' % DASH_PIE_PNG_1
swap_with_4 = 'href="%s"' % DASH_PIE_PNG_2
try:
abs_path = os.path.abspath(".")
dashboard_path = os.path.join(abs_path, "dashboard.html")

View File

@ -104,6 +104,7 @@ def log_test_failure_data(test, test_logpath, driver, browser, url=None):
driver_version = None
driver_name = None
duration = None
exc_message = None
try:
browser_version = get_browser_version(driver)
except Exception:
@ -115,8 +116,7 @@ def log_test_failure_data(test, test_logpath, driver, browser, url=None):
except Exception:
pass
try:
duration = "%.2f" % (time.time() - (sb_config.start_time_ms / 1000.0))
duration = "%ss" % duration
duration = "%.2fs" % (time.time() - (sb_config.start_time_ms / 1000.0))
except Exception:
duration = "(Unknown Duration)"
if browser_version:
@ -177,26 +177,14 @@ def log_test_failure_data(test, test_logpath, driver, browser, url=None):
traceback_message = "(Unknown Traceback)"
data_to_save.append("Traceback: " + traceback_message)
data_to_save.append("Exception: " + str(exc_message))
if hasattr(test, "is_nosetest") and test.is_nosetest:
# Also save the data for the report
sb_config._report_test_id = test_id
sb_config._report_fail_page = last_page
sb_config._report_duration = duration
sb_config._report_browser = browser_displayed
sb_config._report_driver = driver_displayed
sb_config._report_timestamp = timestamp
sb_config._report_date = the_date
sb_config._report_time = the_time
sb_config._report_traceback = traceback_message
sb_config._report_exception = str(exc_message)
else:
the_traceback = None
traceback_message = None
if hasattr(test, "is_behave") and test.is_behave:
if sb_config.behave_scenario.status.name == "failed":
if sb_config.behave_step.error_message:
the_traceback = sb_config.behave_step.error_message
traceback_message = sb_config.behave_step.error_message
else:
the_traceback = "".join(
traceback_message = "".join(
traceback.format_exception(
sys.exc_info()[0],
sys.exc_info()[1],
@ -204,9 +192,9 @@ def log_test_failure_data(test, test_logpath, driver, browser, url=None):
)
)
if (
not the_traceback
or len(str(the_traceback)) < 30
or the_traceback.endswith("StopIteration\n")
not traceback_message
or len(str(traceback_message)) < 30
or traceback_message.endswith("StopIteration\n")
):
good_stack = []
the_stacks = []
@ -225,14 +213,26 @@ def log_test_failure_data(test, test_logpath, driver, browser, url=None):
if "/site-packages/pluggy/" not in stack:
if "/site-packages/_pytest/" not in stack:
good_stack.append(stack)
the_traceback = "".join(good_stack)
data_to_save.append("Traceback: " + the_traceback)
traceback_message = "".join(good_stack)
data_to_save.append("Traceback: " + traceback_message)
if hasattr(sys, "last_value"):
last_value = sys.last_value
if last_value:
data_to_save.append("Exception: " + str(last_value))
else:
data_to_save.append("Traceback: " + the_traceback)
data_to_save.append("Traceback: " + traceback_message)
if hasattr(test, "is_nosetest") and test.is_nosetest:
# Also save the data for the report
sb_config._report_test_id = test_id
sb_config._report_fail_page = last_page
sb_config._report_duration = duration
sb_config._report_browser = browser_displayed
sb_config._report_driver = driver_displayed
sb_config._report_timestamp = timestamp
sb_config._report_date = the_date
sb_config._report_time = the_time
sb_config._report_traceback = traceback_message
sb_config._report_exception = exc_message
log_file = codecs.open(basic_file_path, "w+", "utf-8")
log_file.writelines("\r\n".join(data_to_save))
log_file.close()

View File

@ -3,6 +3,7 @@ import os
import shutil
import sys
import time
import traceback
from seleniumbase import config as sb_config
from seleniumbase.config import settings
from seleniumbase.core.style_sheet import get_report_style
@ -32,7 +33,7 @@ def process_successes(test, test_count, duration):
)
def save_test_failure_data(name, folder=None):
def save_test_failure_data(test, name, folder=None):
"""
Saves failure data to the current directory, or to a subfolder if provided.
If {name} does not end in ".txt", it will get added to it.
@ -50,6 +51,32 @@ def save_test_failure_data(name, folder=None):
failure_data_file_path = name
failure_data_file = codecs.open(failure_data_file_path, "w+", "utf-8")
data_to_save = []
if not hasattr(sb_config, "_report_test_id"):
exc_message = "(Unknown Exception)"
traceback_message = ""
if hasattr(sb_config, "_report_traceback"):
traceback_message = str(sb_config._report_traceback)
if hasattr(sb_config, "_report_exception"):
if type(sb_config._report_exception) is tuple:
exc_message = str(sb_config._report_exception[1].message)
else:
exc_message = str(sb_config._report_exception)
data_to_save.append(test.id())
data_to_save.append(
"----------------------------------------------------------------"
)
data_to_save.append("Last Page: %s" % test._last_page_url)
data_to_save.append(" Browser: %s" % test.browser)
data_to_save.append("Timestamp: %s" % get_timestamp()[:-3])
data_to_save.append(
"----------------------------------------------------------------"
)
data_to_save.append("Traceback: %s" % traceback_message)
if sys.version_info[0] >= 3:
data_to_save.append("Exception: %s" % exc_message)
failure_data_file.writelines("\r\n".join(data_to_save))
failure_data_file.close()
return
data_to_save.append(sb_config._report_test_id)
data_to_save.append(
"--------------------------------------------------------------------"
@ -65,7 +92,8 @@ def save_test_failure_data(name, folder=None):
"--------------------------------------------------------------------"
)
data_to_save.append("Traceback: %s" % sb_config._report_traceback)
data_to_save.append("Exception: %s" % sb_config._report_exception)
if sys.version_info[0] >= 3:
data_to_save.append("Exception: %s" % sb_config._report_exception)
failure_data_file.writelines("\r\n".join(data_to_save))
failure_data_file.close()
@ -74,10 +102,26 @@ def process_failures(test, test_count, duration):
bad_page_image = "failure_%s.png" % test_count
bad_page_data = "failure_%s.txt" % test_count
screenshot_path = os.path.join(LATEST_REPORT_DIR, bad_page_image)
if hasattr(test, "_last_page_screenshot"):
if hasattr(test, "_last_page_screenshot") and test._last_page_screenshot:
with open(screenshot_path, "wb") as file:
file.write(test._last_page_screenshot)
save_test_failure_data(bad_page_data, folder=LATEST_REPORT_DIR)
elif sys.version_info[0] < 3:
try:
sb_config._report_exception = sys.exc_info()
sb_config._report_traceback = "".join(
traceback.format_exception(
sb_config._report_exception[0],
sb_config._report_exception[1],
sb_config._report_exception[2],
)
)
test._last_page_url = test.driver.current_url
test._last_page_screenshot = test.driver.get_screenshot_as_png()
with open(screenshot_path, "wb") as file:
file.write(test._last_page_screenshot)
except Exception:
pass
save_test_failure_data(test, bad_page_data, folder=LATEST_REPORT_DIR)
exc_message = None
if (
sys.version_info[0] >= 3
@ -245,6 +289,10 @@ def build_report(
<th>LOCATION OF FAILURE</th>
</tr></thead>"""
display_url = line[4]
actual_url = line[4]
if len(display_url) < 7:
display_url = sb_config._report_fail_page
actual_url = sb_config._report_fail_page
if len(display_url) > 60:
display_url = display_url[0:58] + "..."
line = (
@ -258,7 +306,7 @@ def build_report(
+ """
&nbsp;&nbsp;
"""
+ '<td><a href="%s">%s</a>' % (line[4], display_url)
+ '<td><a href="%s">%s</a>' % (actual_url, display_url)
)
line = line.replace('"', "")
failure_table += "<tr><td>%s</tr>\n" % line

View File

@ -1,3 +1,4 @@
import sys
from seleniumbase.fixtures import constants
@ -9,10 +10,17 @@ class Saved:
def get_report_style():
if hasattr(Saved, "report_style"):
return Saved.report_style
if sys.version_info[0] >= 3:
# Uses caching to prevent extra method calls
REPORT_FAVICON = constants.Report.get_favicon()
else:
from seleniumbase.core import encoded_images
REPORT_FAVICON = encoded_images.get_report_favicon()
title = """<meta id="OGTitle" property="og:title" content="SeleniumBase">
<title>Test Report</title>
<link rel="SHORTCUT ICON"
href="%s" /> """ % constants.Report.get_favicon()
href="%s" /> """ % REPORT_FAVICON
style = (
title

View File

@ -1,4 +1,5 @@
import os
import sys
from seleniumbase.core import log_helper
from seleniumbase.fixtures import constants
@ -21,12 +22,19 @@ def visual_baseline_folder_setup():
def get_sbs_head():
if sys.version_info[0] >= 3:
# Uses caching to prevent extra method calls
SIDE_BY_SIDE_PNG = constants.SideBySide.get_favicon()
else:
from seleniumbase.core import encoded_images
SIDE_BY_SIDE_PNG = encoded_images.get_side_by_side_png()
head = (
'<head><meta charset="utf-8">'
'<meta name="viewport" content="shrink-to-fit=no">'
'<link rel="shortcut icon" href="%s">'
"<title>Visual Comparison</title>"
"</head>" % (constants.SideBySide.get_favicon())
"</head>" % (SIDE_BY_SIDE_PNG)
)
return head

View File

@ -13481,6 +13481,12 @@ class BaseCase(unittest.TestCase):
pie_file = codecs.open(pie_path, "w+", encoding="utf-8")
pie_file.writelines(dash_pie)
pie_file.close()
if python3:
DASH_PIE_PNG_1 = constants.Dashboard.get_dash_pie_1()
else:
from seleniumbase.core import encoded_images
DASH_PIE_PNG_1 = encoded_images.get_dash_pie_png1()
head = (
'<head><meta charset="utf-8">'
'<meta name="viewport" content="shrink-to-fit=no">'
@ -13488,7 +13494,7 @@ class BaseCase(unittest.TestCase):
"%s"
"<title>Dashboard</title>"
"%s</head>"
% (constants.Dashboard.get_dash_pie_1(), auto_refresh_html, style)
% (DASH_PIE_PNG_1, auto_refresh_html, style)
)
table_html = (
"<div></div>"

View File

@ -279,7 +279,7 @@ class Base(Plugin):
def addSuccess(self, test, capt):
if self.report_on:
self.duration = str(
"%0.3fs" % (float(time.time()) - float(self.start_time))
"%.2fs" % (float(time.time()) - float(self.start_time))
)
self.successes.append(test.id())
self.page_results_list.append(
@ -291,7 +291,7 @@ class Base(Plugin):
def add_fails_or_errors(self, test):
if self.report_on:
self.duration = str(
"%0.3fs" % (float(time.time()) - float(self.start_time))
"%.2fs" % (float(time.time()) - float(self.start_time))
)
if test.id() == "nose.failure.Failure.runTest":
print(">>> ERROR: Could not locate tests to run!")

View File

@ -15,6 +15,9 @@ from seleniumbase.fixtures import constants
is_windows = False
if sys.platform in ["win32", "win64", "x64"]:
is_windows = True
python3 = True
if sys.version_info[0] < 3:
python3 = False
sys_argv = sys.argv
pytest_plugins = ["pytester"] # Adds the "testdir" fixture
@ -1403,7 +1406,7 @@ def pytest_configure(config):
or " -n=" in arg_join
or "-c" in sys_argv
or (
sys.version_info[0] >= 3
python3
and "addopts" in config.inicfg.keys()
and (
"-n=" in config.inicfg["addopts"]
@ -1416,7 +1419,7 @@ def pytest_configure(config):
"--html" in sys_argv
or " --html=" in arg_join
or (
sys.version_info[0] >= 3
python3
and "addopts" in config.inicfg.keys()
and (
"--html=" in config.inicfg["addopts"]
@ -1776,16 +1779,33 @@ def _perform_pytest_unconfigure_():
)
find_it_3 = '<td class="col-result">Untested</td>'
swap_with_3 = '<td class="col-result">Unreported</td>'
find_it_4 = 'href="%s"' % constants.Dashboard.get_dash_pie_1()
swap_with_4 = 'href="%s"' % constants.Dashboard.get_dash_pie_2()
if python3:
# These use caching to prevent extra method calls
DASH_PIE_PNG_1 = constants.Dashboard.get_dash_pie_1()
DASH_PIE_PNG_2 = constants.Dashboard.get_dash_pie_2()
DASH_PIE_PNG_3 = constants.Dashboard.get_dash_pie_3()
else:
from seleniumbase.core import encoded_images
DASH_PIE_PNG_1 = encoded_images.get_dash_pie_png1()
DASH_PIE_PNG_2 = encoded_images.get_dash_pie_png2()
DASH_PIE_PNG_3 = encoded_images.get_dash_pie_png3()
find_it_4 = 'href="%s"' % DASH_PIE_PNG_1
swap_with_4 = 'href="%s"' % DASH_PIE_PNG_2
try:
abs_path = os.path.abspath(".")
dashboard_path = os.path.join(abs_path, "dashboard.html")
# Part 1: Finalizing the dashboard / integrating html report
if os.path.exists(dashboard_path):
the_html_d = None
with open(dashboard_path, "r", encoding="utf-8") as f:
the_html_d = f.read()
if python3:
with open(dashboard_path, "r", encoding="utf-8") as f:
the_html_d = f.read()
else:
import io
with io.open(dashboard_path, "r", encoding="utf-8") as f:
the_html_d = f.read()
if sb_config._multithreaded and "-c" in sys_argv:
# Threads have "-c" in sys.argv, except for the last
raise Exception('Break out of "try" block.')
@ -1814,7 +1834,7 @@ def _perform_pytest_unconfigure_():
the_html_d = the_html_d.replace(
"</head>",
'</head><link rel="shortcut icon" '
'href="%s">' % constants.Dashboard.get_dash_pie_3(),
'href="%s">' % DASH_PIE_PNG_3,
)
the_html_d = the_html_d.replace("<html>", '<html lang="en">')
the_html_d = the_html_d.replace(
@ -1826,14 +1846,22 @@ def _perform_pytest_unconfigure_():
if sb_config._dash_final_summary:
the_html_d += sb_config._dash_final_summary
time.sleep(0.1) # Add time for "livejs" to detect changes
with open(dashboard_path, "w", encoding="utf-8") as f:
f.write(the_html_d) # Finalize the dashboard
if python3:
with open(dashboard_path, "w", encoding="utf-8") as f:
f.write(the_html_d) # Finalize the dashboard
else:
with io.open(dashboard_path, "w", encoding="utf-8") as f:
f.write(the_html_d) # Finalize the dashboard
time.sleep(0.1) # Add time for "livejs" to detect changes
the_html_d = the_html_d.replace(
"</head>", "</head><!-- Dashboard Report Done -->"
)
with open(dashboard_path, "w", encoding="utf-8") as f:
f.write(the_html_d) # Finalize the dashboard
if python3:
with open(dashboard_path, "w", encoding="utf-8") as f:
f.write(the_html_d) # Finalize the dashboard
else:
with io.open(dashboard_path, "w", encoding="utf-8") as f:
f.write(the_html_d) # Finalize the dashboard
# Part 2: Appending a pytest html report with dashboard data
html_report_path = None
if sb_config._html_report_name:
@ -1863,7 +1891,7 @@ def _perform_pytest_unconfigure_():
the_html_r = the_html_r.replace(
"</head>",
'</head><link rel="shortcut icon" href='
'"%s">' % constants.Dashboard.get_dash_pie_3(),
'"%s">' % DASH_PIE_PNG_3,
)
if sb_config._dash_final_summary:
the_html_r += sb_config._dash_final_summary