33import shutil
44import sys
55import time
6+ import traceback
67from seleniumbase import config as sb_config
78from seleniumbase .config import settings
89from 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
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
0 commit comments