Skip to content

Commit b4b2180

Browse files
committed
Refactoring
1 parent d7c2e14 commit b4b2180

File tree

8 files changed

+154
-47
lines changed

8 files changed

+154
-47
lines changed

seleniumbase/behave/behave_sb.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,8 +1082,17 @@ def _perform_behave_unconfigure_():
10821082
)
10831083
find_it_3 = '<td class="col-result">Untested</td>'
10841084
swap_with_3 = '<td class="col-result">Unreported</td>'
1085-
find_it_4 = 'href="%s"' % constants.Dashboard.get_dash_pie_1()
1086-
swap_with_4 = 'href="%s"' % constants.Dashboard.get_dash_pie_2()
1085+
if sys.version_info[0] >= 3:
1086+
# These use caching to prevent extra method calls
1087+
DASH_PIE_PNG_1 = constants.Dashboard.get_dash_pie_1()
1088+
DASH_PIE_PNG_2 = constants.Dashboard.get_dash_pie_2()
1089+
else:
1090+
from seleniumbase.core import encoded_images
1091+
1092+
DASH_PIE_PNG_1 = encoded_images.get_dash_pie_png1()
1093+
DASH_PIE_PNG_2 = encoded_images.get_dash_pie_png2()
1094+
find_it_4 = 'href="%s"' % DASH_PIE_PNG_1
1095+
swap_with_4 = 'href="%s"' % DASH_PIE_PNG_2
10871096
try:
10881097
abs_path = os.path.abspath(".")
10891098
dashboard_path = os.path.join(abs_path, "dashboard.html")

seleniumbase/core/log_helper.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def log_test_failure_data(test, test_logpath, driver, browser, url=None):
104104
driver_version = None
105105
driver_name = None
106106
duration = None
107+
exc_message = None
107108
try:
108109
browser_version = get_browser_version(driver)
109110
except Exception:
@@ -115,8 +116,7 @@ def log_test_failure_data(test, test_logpath, driver, browser, url=None):
115116
except Exception:
116117
pass
117118
try:
118-
duration = "%.2f" % (time.time() - (sb_config.start_time_ms / 1000.0))
119-
duration = "%ss" % duration
119+
duration = "%.2fs" % (time.time() - (sb_config.start_time_ms / 1000.0))
120120
except Exception:
121121
duration = "(Unknown Duration)"
122122
if browser_version:
@@ -177,36 +177,24 @@ def log_test_failure_data(test, test_logpath, driver, browser, url=None):
177177
traceback_message = "(Unknown Traceback)"
178178
data_to_save.append("Traceback: " + traceback_message)
179179
data_to_save.append("Exception: " + str(exc_message))
180-
if hasattr(test, "is_nosetest") and test.is_nosetest:
181-
# Also save the data for the report
182-
sb_config._report_test_id = test_id
183-
sb_config._report_fail_page = last_page
184-
sb_config._report_duration = duration
185-
sb_config._report_browser = browser_displayed
186-
sb_config._report_driver = driver_displayed
187-
sb_config._report_timestamp = timestamp
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)
192180
else:
193-
the_traceback = None
181+
traceback_message = None
194182
if hasattr(test, "is_behave") and test.is_behave:
195183
if sb_config.behave_scenario.status.name == "failed":
196184
if sb_config.behave_step.error_message:
197-
the_traceback = sb_config.behave_step.error_message
185+
traceback_message = sb_config.behave_step.error_message
198186
else:
199-
the_traceback = "".join(
187+
traceback_message = "".join(
200188
traceback.format_exception(
201189
sys.exc_info()[0],
202190
sys.exc_info()[1],
203191
sys.exc_info()[2],
204192
)
205193
)
206194
if (
207-
not the_traceback
208-
or len(str(the_traceback)) < 30
209-
or the_traceback.endswith("StopIteration\n")
195+
not traceback_message
196+
or len(str(traceback_message)) < 30
197+
or traceback_message.endswith("StopIteration\n")
210198
):
211199
good_stack = []
212200
the_stacks = []
@@ -225,14 +213,26 @@ def log_test_failure_data(test, test_logpath, driver, browser, url=None):
225213
if "/site-packages/pluggy/" not in stack:
226214
if "/site-packages/_pytest/" not in stack:
227215
good_stack.append(stack)
228-
the_traceback = "".join(good_stack)
229-
data_to_save.append("Traceback: " + the_traceback)
216+
traceback_message = "".join(good_stack)
217+
data_to_save.append("Traceback: " + traceback_message)
230218
if hasattr(sys, "last_value"):
231219
last_value = sys.last_value
232220
if last_value:
233221
data_to_save.append("Exception: " + str(last_value))
234222
else:
235-
data_to_save.append("Traceback: " + the_traceback)
223+
data_to_save.append("Traceback: " + traceback_message)
224+
if hasattr(test, "is_nosetest") and test.is_nosetest:
225+
# Also save the data for the report
226+
sb_config._report_test_id = test_id
227+
sb_config._report_fail_page = last_page
228+
sb_config._report_duration = duration
229+
sb_config._report_browser = browser_displayed
230+
sb_config._report_driver = driver_displayed
231+
sb_config._report_timestamp = timestamp
232+
sb_config._report_date = the_date
233+
sb_config._report_time = the_time
234+
sb_config._report_traceback = traceback_message
235+
sb_config._report_exception = exc_message
236236
log_file = codecs.open(basic_file_path, "w+", "utf-8")
237237
log_file.writelines("\r\n".join(data_to_save))
238238
log_file.close()

seleniumbase/core/report_helper.py

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import shutil
44
import sys
55
import time
6+
import traceback
67
from seleniumbase import config as sb_config
78
from seleniumbase.config import settings
89
from seleniumbase.core.style_sheet import get_report_style
@@ -32,7 +33,7 @@ def process_successes(test, test_count, duration):
3233
)
3334

3435

35-
def save_test_failure_data(name, folder=None):
36+
def save_test_failure_data(test, name, folder=None):
3637
"""
3738
Saves failure data to the current directory, or to a subfolder if provided.
3839
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):
5051
failure_data_file_path = name
5152
failure_data_file = codecs.open(failure_data_file_path, "w+", "utf-8")
5253
data_to_save = []
54+
if not hasattr(sb_config, "_report_test_id"):
55+
exc_message = "(Unknown Exception)"
56+
traceback_message = ""
57+
if hasattr(sb_config, "_report_traceback"):
58+
traceback_message = str(sb_config._report_traceback)
59+
if hasattr(sb_config, "_report_exception"):
60+
if type(sb_config._report_exception) is tuple:
61+
exc_message = str(sb_config._report_exception[1].message)
62+
else:
63+
exc_message = str(sb_config._report_exception)
64+
data_to_save.append(test.id())
65+
data_to_save.append(
66+
"----------------------------------------------------------------"
67+
)
68+
data_to_save.append("Last Page: %s" % test._last_page_url)
69+
data_to_save.append(" Browser: %s" % test.browser)
70+
data_to_save.append("Timestamp: %s" % get_timestamp()[:-3])
71+
data_to_save.append(
72+
"----------------------------------------------------------------"
73+
)
74+
data_to_save.append("Traceback: %s" % traceback_message)
75+
if sys.version_info[0] >= 3:
76+
data_to_save.append("Exception: %s" % exc_message)
77+
failure_data_file.writelines("\r\n".join(data_to_save))
78+
failure_data_file.close()
79+
return
5380
data_to_save.append(sb_config._report_test_id)
5481
data_to_save.append(
5582
"--------------------------------------------------------------------"
@@ -65,7 +92,8 @@ def save_test_failure_data(name, folder=None):
6592
"--------------------------------------------------------------------"
6693
)
6794
data_to_save.append("Traceback: %s" % sb_config._report_traceback)
68-
data_to_save.append("Exception: %s" % sb_config._report_exception)
95+
if sys.version_info[0] >= 3:
96+
data_to_save.append("Exception: %s" % sb_config._report_exception)
6997
failure_data_file.writelines("\r\n".join(data_to_save))
7098
failure_data_file.close()
7199

@@ -74,10 +102,26 @@ def process_failures(test, test_count, duration):
74102
bad_page_image = "failure_%s.png" % test_count
75103
bad_page_data = "failure_%s.txt" % test_count
76104
screenshot_path = os.path.join(LATEST_REPORT_DIR, bad_page_image)
77-
if hasattr(test, "_last_page_screenshot"):
105+
if hasattr(test, "_last_page_screenshot") and test._last_page_screenshot:
78106
with open(screenshot_path, "wb") as file:
79107
file.write(test._last_page_screenshot)
80-
save_test_failure_data(bad_page_data, folder=LATEST_REPORT_DIR)
108+
elif sys.version_info[0] < 3:
109+
try:
110+
sb_config._report_exception = sys.exc_info()
111+
sb_config._report_traceback = "".join(
112+
traceback.format_exception(
113+
sb_config._report_exception[0],
114+
sb_config._report_exception[1],
115+
sb_config._report_exception[2],
116+
)
117+
)
118+
test._last_page_url = test.driver.current_url
119+
test._last_page_screenshot = test.driver.get_screenshot_as_png()
120+
with open(screenshot_path, "wb") as file:
121+
file.write(test._last_page_screenshot)
122+
except Exception:
123+
pass
124+
save_test_failure_data(test, bad_page_data, folder=LATEST_REPORT_DIR)
81125
exc_message = None
82126
if (
83127
sys.version_info[0] >= 3
@@ -245,6 +289,10 @@ def build_report(
245289
<th>LOCATION OF FAILURE</th>
246290
</tr></thead>"""
247291
display_url = line[4]
292+
actual_url = line[4]
293+
if len(display_url) < 7:
294+
display_url = sb_config._report_fail_page
295+
actual_url = sb_config._report_fail_page
248296
if len(display_url) > 60:
249297
display_url = display_url[0:58] + "..."
250298
line = (
@@ -258,7 +306,7 @@ def build_report(
258306
+ """
259307
&nbsp;&nbsp;
260308
"""
261-
+ '<td><a href="%s">%s</a>' % (line[4], display_url)
309+
+ '<td><a href="%s">%s</a>' % (actual_url, display_url)
262310
)
263311
line = line.replace('"', "")
264312
failure_table += "<tr><td>%s</tr>\n" % line

seleniumbase/core/style_sheet.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
from seleniumbase.fixtures import constants
23

34

@@ -9,10 +10,17 @@ class Saved:
910
def get_report_style():
1011
if hasattr(Saved, "report_style"):
1112
return Saved.report_style
13+
if sys.version_info[0] >= 3:
14+
# Uses caching to prevent extra method calls
15+
REPORT_FAVICON = constants.Report.get_favicon()
16+
else:
17+
from seleniumbase.core import encoded_images
18+
19+
REPORT_FAVICON = encoded_images.get_report_favicon()
1220
title = """<meta id="OGTitle" property="og:title" content="SeleniumBase">
1321
<title>Test Report</title>
1422
<link rel="SHORTCUT ICON"
15-
href="%s" /> """ % constants.Report.get_favicon()
23+
href="%s" /> """ % REPORT_FAVICON
1624

1725
style = (
1826
title

seleniumbase/core/visual_helper.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import sys
23
from seleniumbase.core import log_helper
34
from seleniumbase.fixtures import constants
45

@@ -21,12 +22,19 @@ def visual_baseline_folder_setup():
2122

2223

2324
def get_sbs_head():
25+
if sys.version_info[0] >= 3:
26+
# Uses caching to prevent extra method calls
27+
SIDE_BY_SIDE_PNG = constants.SideBySide.get_favicon()
28+
else:
29+
from seleniumbase.core import encoded_images
30+
31+
SIDE_BY_SIDE_PNG = encoded_images.get_side_by_side_png()
2432
head = (
2533
'<head><meta charset="utf-8">'
2634
'<meta name="viewport" content="shrink-to-fit=no">'
2735
'<link rel="shortcut icon" href="%s">'
2836
"<title>Visual Comparison</title>"
29-
"</head>" % (constants.SideBySide.get_favicon())
37+
"</head>" % (SIDE_BY_SIDE_PNG)
3038
)
3139
return head
3240

seleniumbase/fixtures/base_case.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13481,14 +13481,20 @@ def __process_dashboard(self, has_exception, init=False):
1348113481
pie_file = codecs.open(pie_path, "w+", encoding="utf-8")
1348213482
pie_file.writelines(dash_pie)
1348313483
pie_file.close()
13484+
if python3:
13485+
DASH_PIE_PNG_1 = constants.Dashboard.get_dash_pie_1()
13486+
else:
13487+
from seleniumbase.core import encoded_images
13488+
13489+
DASH_PIE_PNG_1 = encoded_images.get_dash_pie_png1()
1348413490
head = (
1348513491
'<head><meta charset="utf-8">'
1348613492
'<meta name="viewport" content="shrink-to-fit=no">'
1348713493
'<link rel="shortcut icon" href="%s">'
1348813494
"%s"
1348913495
"<title>Dashboard</title>"
1349013496
"%s</head>"
13491-
% (constants.Dashboard.get_dash_pie_1(), auto_refresh_html, style)
13497+
% (DASH_PIE_PNG_1, auto_refresh_html, style)
1349213498
)
1349313499
table_html = (
1349413500
"<div></div>"

seleniumbase/plugins/base_plugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ def __log_all_options_if_none_specified(self, test):
279279
def addSuccess(self, test, capt):
280280
if self.report_on:
281281
self.duration = str(
282-
"%0.3fs" % (float(time.time()) - float(self.start_time))
282+
"%.2fs" % (float(time.time()) - float(self.start_time))
283283
)
284284
self.successes.append(test.id())
285285
self.page_results_list.append(
@@ -291,7 +291,7 @@ def addSuccess(self, test, capt):
291291
def add_fails_or_errors(self, test):
292292
if self.report_on:
293293
self.duration = str(
294-
"%0.3fs" % (float(time.time()) - float(self.start_time))
294+
"%.2fs" % (float(time.time()) - float(self.start_time))
295295
)
296296
if test.id() == "nose.failure.Failure.runTest":
297297
print(">>> ERROR: Could not locate tests to run!")

0 commit comments

Comments
 (0)