Skip to content

Commit f18e4c5

Browse files
committed
Improve test reports
1 parent 7a786b3 commit f18e4c5

File tree

7 files changed

+87
-56
lines changed

7 files changed

+87
-56
lines changed

seleniumbase/core/encoded_images.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
""" Instructions for generating encoded images:
2+
> import base64
3+
> with open("YOUR_FILE.png", "rb") as image_file:
4+
> encoded_string = base64.b64encode(image_file.read())
5+
"""
16
DASH_PIE_PNG_1 = (
27
"data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAEcAAABHCAYAAABVsFofAAAAAXN"
38
"SR0IArs4c6QAABqxJREFUeAHtnOtPFFcUwO/sLo9GUKIkUtNWDK+a2qZGm2LShDFSoeWhqUat"
@@ -126,6 +131,14 @@
126131
"CtZrdwUAJRk/AoTJspXWuCo61KDQjw2oCBgOwE+egH3kfj7umAhCOmwEi050g4nslK+I4W/5C"
127132
"zyeuQ5f3kZ4tNlIZF18vP/AXYR+dvV3FCCAAAAAElFTkSuQmCC"
128133
)
134+
REPORT_FAVICON = (
135+
"data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAAAXN"
136+
"SR0IArs4c6QAAAM9JREFUKBVjnNS8goEUwESKYpBaFiD+z/hTJz2EoM4rM9cw/mcHaYCA2Nl3"
137+
"YUwEvTBFkZGREchnBEIwAGv4z/6f4T8DWAIk+P8/RA6ZBCkAA4QNi1IUkVVgYf9nBwpCNTiLc"
138+
"GNRgSp0FcxF2CC3RhVVAcOjkNtAEYg4hA3kIjRAPYWmCeRdFIDQ8BBsHookmIMmDtXw5s0bTK"
139+
"VoIhCrQBogLHaPUGQVP7avgnA5PMOAjJ87VkO4ZCUNiFa4GRAu3K9o4lA/LJ+xF6KOIEmykwB"
140+
"QHy74EMZM3QAAAABJRU5ErkJggg=="
141+
)
129142
SIDE_BY_SIDE_PNG = (
130143
"data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAAAXN"
131144
"SR0IArs4c6QAAAIVJREFUKBVjvPLvPwMpgIkUxSC1LBANOo0oGq/Ug7hYBUm2gWQNjBBPa/1l"

seleniumbase/core/log_helper.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ def log_test_failure_data(test, test_logpath, driver, browser, url=None):
103103
browser_version = None
104104
driver_version = None
105105
driver_name = None
106+
duration = None
106107
try:
107108
browser_version = get_browser_version(driver)
108109
except Exception:
@@ -113,10 +114,21 @@ def log_test_failure_data(test, test_logpath, driver, browser, url=None):
113114
)
114115
except Exception:
115116
pass
117+
try:
118+
duration = "%.2f" % (time.time() - (sb_config.start_time_ms / 1000.0))
119+
d_len = len(str(duration))
120+
s_len = 12 - d_len
121+
if s_len < 2:
122+
s_len = 2
123+
duration = "%s%s(seconds)" % (duration, s_len * " ")
124+
except Exception:
125+
duration = "(Unknown Duration)"
116126
if browser_version:
117127
headless = ""
118128
if test.headless and browser in ["chrome", "edge", "firefox"]:
119129
headless = " / headless"
130+
if test.headless2 and browser in ["chrome", "edge"]:
131+
headless = " / headless2"
120132
browser_displayed = "%s (%s%s)" % (browser, browser_version, headless)
121133
if driver_name and driver_version:
122134
driver_displayed = "%s (%s)" % (driver_name, driver_version)
@@ -143,6 +155,7 @@ def log_test_failure_data(test, test_logpath, driver, browser, url=None):
143155
data_to_save.append(" Browser: %s" % browser_displayed)
144156
data_to_save.append(" Driver: %s" % driver_displayed)
145157
data_to_save.append("Timestamp: %s" % timestamp)
158+
data_to_save.append(" Duration: %s" % duration)
146159
data_to_save.append(" Date: %s" % the_date)
147160
data_to_save.append(" Time: %s" % the_time)
148161
data_to_save.append(
@@ -165,6 +178,17 @@ def log_test_failure_data(test, test_logpath, driver, browser, url=None):
165178
traceback_message = "(Unknown Traceback)"
166179
data_to_save.append("Traceback: " + traceback_message)
167180
data_to_save.append("Exception: " + str(exc_message))
181+
if hasattr(test, "is_nosetest") and test.is_nosetest:
182+
# Also save the data for the report
183+
sb_config._report_test_id = test_id
184+
sb_config._report_browser = browser_displayed
185+
sb_config._report_driver = driver_displayed
186+
sb_config._report_timestamp = timestamp
187+
sb_config._report_duration = duration
188+
sb_config._report_date = the_date
189+
sb_config._report_time = the_time
190+
sb_config._report_traceback = traceback_message
191+
sb_config._report_exception = str(exc_message)
168192
else:
169193
the_traceback = None
170194
if hasattr(test, "is_behave") and test.is_behave:

seleniumbase/core/report_helper.py

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import codecs
12
import os
23
import shutil
34
import sys
45
import time
6+
from seleniumbase import config as sb_config
57
from seleniumbase.config import settings
68
from seleniumbase.core.style_sheet import style
7-
from seleniumbase.fixtures import page_actions
89

910
LATEST_REPORT_DIR = settings.LATEST_REPORT_DIR
1011
ARCHIVE_DIR = settings.REPORT_ARCHIVE_DIR
@@ -31,16 +32,52 @@ def process_successes(test, test_count, duration):
3132
)
3233

3334

34-
def process_failures(test, test_count, browser_type, duration):
35+
def save_test_failure_data(name, folder=None):
36+
"""
37+
Saves failure data to the current directory, or to a subfolder if provided.
38+
If {name} does not end in ".txt", it will get added to it.
39+
If the folder provided doesn't exist, it will get created.
40+
"""
41+
if not name.endswith(".txt"):
42+
name = name + ".txt"
43+
if folder:
44+
abs_path = os.path.abspath(".")
45+
file_path = os.path.join(abs_path, folder)
46+
if not os.path.exists(file_path):
47+
os.makedirs(file_path)
48+
failure_data_file_path = os.path.join(file_path, name)
49+
else:
50+
failure_data_file_path = name
51+
failure_data_file = codecs.open(failure_data_file_path, "w+", "utf-8")
52+
data_to_save = []
53+
data_to_save.append(sb_config._report_test_id)
54+
data_to_save.append(
55+
"--------------------------------------------------------------------"
56+
)
57+
data_to_save.append("Last Page: %s" % sb_config._fail_page)
58+
data_to_save.append(" Browser: %s" % sb_config._report_browser)
59+
data_to_save.append(" Driver: %s" % sb_config._report_driver)
60+
data_to_save.append("Timestamp: %s" % sb_config._report_timestamp)
61+
data_to_save.append(" Duration: %s" % sb_config._report_duration)
62+
data_to_save.append(" Date: %s" % sb_config._report_date)
63+
data_to_save.append(" Time: %s" % sb_config._report_time)
64+
data_to_save.append(
65+
"--------------------------------------------------------------------"
66+
)
67+
data_to_save.append("Traceback: %s" % sb_config._report_traceback)
68+
data_to_save.append("Exception: %s" % sb_config._report_exception)
69+
failure_data_file.writelines("\r\n".join(data_to_save))
70+
failure_data_file.close()
71+
72+
73+
def process_failures(test, test_count, duration):
3574
bad_page_image = "failure_%s.png" % test_count
3675
bad_page_data = "failure_%s.txt" % test_count
3776
screenshot_path = os.path.join(LATEST_REPORT_DIR, bad_page_image)
3877
if hasattr(test, "_last_page_screenshot"):
3978
with open(screenshot_path, "wb") as file:
4079
file.write(test._last_page_screenshot)
41-
page_actions.save_test_failure_data(
42-
test.driver, bad_page_data, browser_type, folder=LATEST_REPORT_DIR
43-
)
80+
save_test_failure_data(bad_page_data, folder=LATEST_REPORT_DIR)
4481
exc_message = None
4582
if (
4683
sys.version_info[0] >= 3

seleniumbase/core/style_sheet.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
from seleniumbase.fixtures import constants
2+
13
title = """<meta id="OGTitle" property="og:title" content="SeleniumBase">
24
<title>Test Report</title>
35
<link rel="SHORTCUT ICON"
4-
href="https://seleniumbase.io/img/favicon.ico" /> """
6+
href="%s" /> """ % constants.Report.FAVICON
57

68
style = (
79
title

seleniumbase/fixtures/constants.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ class PipInstall:
9090
LOCKFILE = Files.DOWNLOADS_FOLDER + "/pipinstall.lock"
9191

9292

93+
class Report:
94+
FAVICON = encoded_images.REPORT_FAVICON
95+
96+
9397
class SideBySide:
9498
HTML_FILE = "side_by_side.html"
9599
SIDE_BY_SIDE_PNG = encoded_images.SIDE_BY_SIDE_PNG

seleniumbase/fixtures/page_actions.py

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import codecs
2424
import os
25-
import sys
2625
import time
2726
from selenium.common.exceptions import ElementNotInteractableException
2827
from selenium.common.exceptions import ElementNotVisibleException
@@ -1127,53 +1126,6 @@ def save_page_source(driver, name, folder=None):
11271126
html_file.close()
11281127

11291128

1130-
def _get_last_page(driver):
1131-
try:
1132-
last_page = driver.current_url
1133-
except Exception:
1134-
last_page = "[WARNING! Browser Not Open!]"
1135-
if len(last_page) < 5:
1136-
last_page = "[WARNING! Browser Not Open!]"
1137-
return last_page
1138-
1139-
1140-
def save_test_failure_data(driver, name, browser_type=None, folder=None):
1141-
"""
1142-
Saves failure data to the current directory, or to a subfolder if provided.
1143-
If {name} does not end in ".txt", it will get added to it.
1144-
If {browser_type} is provided, the logs will include that.
1145-
If the folder provided doesn't exist, it will get created.
1146-
"""
1147-
import traceback
1148-
1149-
if not name.endswith(".txt"):
1150-
name = name + ".txt"
1151-
if folder:
1152-
abs_path = os.path.abspath(".")
1153-
file_path = os.path.join(abs_path, folder)
1154-
if not os.path.exists(file_path):
1155-
os.makedirs(file_path)
1156-
failure_data_file_path = os.path.join(file_path, name)
1157-
else:
1158-
failure_data_file_path = name
1159-
failure_data_file = codecs.open(failure_data_file_path, "w+", "utf-8")
1160-
last_page = _get_last_page(driver)
1161-
data_to_save = []
1162-
data_to_save.append("Last_Page: %s" % last_page)
1163-
if browser_type:
1164-
data_to_save.append("Browser: %s " % browser_type)
1165-
data_to_save.append(
1166-
"Traceback: "
1167-
+ "".join(
1168-
traceback.format_exception(
1169-
sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]
1170-
)
1171-
)
1172-
)
1173-
failure_data_file.writelines("\r\n".join(data_to_save))
1174-
failure_data_file.close()
1175-
1176-
11771129
def wait_for_and_accept_alert(driver, timeout=settings.LARGE_TIMEOUT):
11781130
"""
11791131
Wait for and accept an alert. Returns the text from the alert.

seleniumbase/plugins/base_plugin.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,9 @@ def add_fails_or_errors(self, test):
299299
self.import_error = True
300300
return
301301
self.failures.append(test.id())
302-
br = self.options.browser
303302
self.page_results_list.append(
304303
report_helper.process_failures(
305-
test, self.test_count, br, self.duration
304+
test, self.test_count, self.duration
306305
)
307306
)
308307

0 commit comments

Comments
 (0)