Skip to content

Commit 344e8a6

Browse files
authored
Merge pull request #118 from seleniumbase/fix-python3-logging-issue
Fix Python 3 Logging Issue
2 parents 78e8b7f + 2690e30 commit 344e8a6

File tree

6 files changed

+57
-23
lines changed

6 files changed

+57
-23
lines changed

examples/gui_test_runner.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
Run by Typing: "python gui_test_runner.py"
44
'''
55

6-
from Tkinter import Tk, Frame, Button, Label
6+
try:
7+
# Python 2
8+
from Tkinter import Tk, Frame, Button, Label
9+
except:
10+
# Python 3
11+
from tkinter import Tk, Frame, Button, Label
712
import os
813

914

seleniumbase/core/log_helper.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,32 @@ def log_screenshot(test_logpath, driver):
1010
driver.get_screenshot_as_file(screenshot_path)
1111

1212

13-
def log_test_failure_data(test_logpath, driver, browser):
13+
def log_test_failure_data(test, test_logpath, driver, browser):
1414
basic_info_name = settings.BASIC_INFO_NAME
1515
basic_file_path = "%s/%s" % (test_logpath, basic_info_name)
1616
log_file = codecs.open(basic_file_path, "w+", "utf-8")
1717
last_page = get_last_page(driver)
1818
data_to_save = []
1919
data_to_save.append("Last_Page: %s" % last_page)
2020
data_to_save.append("Browser: %s " % browser)
21-
data_to_save.append("Traceback: " + ''.join(
22-
traceback.format_exception(sys.exc_info()[0],
23-
sys.exc_info()[1],
24-
sys.exc_info()[2])))
21+
if sys.version.startswith('3') and hasattr(test, '_outcome'):
22+
if test._outcome.errors:
23+
try:
24+
exc_message = test._outcome.errors[0][1][1].msg
25+
traceback_address = test._outcome.errors[0][1][2]
26+
traceback_list = traceback.format_list(
27+
traceback.extract_tb(traceback_address)[1:])
28+
traceback_message = ''.join(traceback_list).strip()
29+
except:
30+
exc_message = "(Unknown Exception)"
31+
traceback_message = "(Unknown Traceback)"
32+
data_to_save.append("Traceback: " + traceback_message)
33+
data_to_save.append("Exception: " + exc_message)
34+
else:
35+
data_to_save.append("Traceback: " + ''.join(
36+
traceback.format_exception(sys.exc_info()[0],
37+
sys.exc_info()[1],
38+
sys.exc_info()[2])))
2539
log_file.writelines("\r\n".join(data_to_save))
2640
log_file.close()
2741

seleniumbase/fixtures/base_case.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,13 +1126,23 @@ def _get_exception_message(self):
11261126
""" This method extracts the message from an exception if there
11271127
was an exception that occurred during the test, assuming
11281128
that the exception was in a try/except block and not thrown. """
1129-
exception_info = sys.exc_info()[1]
1130-
if hasattr(exception_info, 'msg'):
1131-
exc_message = exception_info.msg
1132-
elif hasattr(exception_info, 'message'):
1133-
exc_message = exception_info.message
1129+
if sys.version.startswith('3') and hasattr(self, '_outcome'):
1130+
exception_info = self._outcome.errors
1131+
if exception_info:
1132+
try:
1133+
exc_message = exception_info[0][1][1]
1134+
except:
1135+
exc_message = "(Unknown Exception)"
1136+
else:
1137+
exc_message = "(Unknown Exception)"
11341138
else:
1135-
exc_message = '(Unknown Exception)'
1139+
exception_info = sys.exc_info()[1]
1140+
if hasattr(exception_info, 'msg'):
1141+
exc_message = exception_info.msg
1142+
elif hasattr(exception_info, 'message'):
1143+
exc_message = exception_info.message
1144+
else:
1145+
exc_message = '(Unknown Exception)'
11361146
return exc_message
11371147

11381148
def _package_check(self):
@@ -1405,12 +1415,18 @@ def tearDown(self):
14051415
You'll need to add the following line to the subclass's tearDown():
14061416
super(SubClassOfBaseCase, self).tearDown()
14071417
"""
1418+
has_exception = False
1419+
if sys.version.startswith('3') and hasattr(self, '_outcome'):
1420+
if self._outcome.errors:
1421+
has_exception = True
1422+
else:
1423+
has_exception = sys.exc_info()[1] is not None
14081424
if self.page_check_failures:
14091425
print(
14101426
"\nWhen using self.check_assert_***() methods in your tests, "
14111427
"remember to call self.process_checks() afterwards. "
14121428
"Now calling in tearDown()...\nFailures Detected:")
1413-
if not sys.exc_info()[1]:
1429+
if not has_exception:
14141430
self.process_checks()
14151431
else:
14161432
self.process_checks(print_only=True)
@@ -1428,10 +1444,9 @@ def tearDown(self):
14281444
self._testMethodName)
14291445
if self.with_selenium:
14301446
# Save a screenshot if logging is on when an exception occurs
1431-
is_exception = sys.exc_info()[1] is not None
1432-
if is_exception:
1447+
if has_exception:
14331448
self._add_pytest_html_extra()
1434-
if self.with_testing_base and is_exception:
1449+
if self.with_testing_base and has_exception:
14351450
test_logpath = self.log_path + "/" + test_id
14361451
if not os.path.exists(test_logpath):
14371452
os.makedirs(test_logpath)
@@ -1441,15 +1456,15 @@ def tearDown(self):
14411456
# Log everything if nothing specified (if testing_base)
14421457
log_helper.log_screenshot(test_logpath, self.driver)
14431458
log_helper.log_test_failure_data(
1444-
test_logpath, self.driver, self.browser)
1459+
self, test_logpath, self.driver, self.browser)
14451460
log_helper.log_page_source(test_logpath, self.driver)
14461461
else:
14471462
if self.with_screen_shots:
14481463
log_helper.log_screenshot(
14491464
test_logpath, self.driver)
14501465
if self.with_basic_test_info:
14511466
log_helper.log_test_failure_data(
1452-
test_logpath, self.driver, self.browser)
1467+
self, test_logpath, self.driver, self.browser)
14531468
if self.with_page_source:
14541469
log_helper.log_page_source(
14551470
test_logpath, self.driver)
@@ -1466,14 +1481,14 @@ def tearDown(self):
14661481
self.display.stop()
14671482
self.display = None
14681483
if self.with_db_reporting:
1469-
if sys.exc_info()[1] is not None:
1484+
if has_exception:
14701485
self.__insert_test_result(constants.State.ERROR, True)
14711486
else:
14721487
self.__insert_test_result(constants.State.PASS, False)
14731488
runtime = int(time.time() * 1000) - self.execution_start_time
14741489
self.testcase_manager.update_execution_data(
14751490
self.execution_guid, runtime)
1476-
if self.with_s3_logging and (sys.exc_info()[1] is not None):
1491+
if self.with_s3_logging and has_exception:
14771492
""" After each testcase, upload logs to the S3 bucket. """
14781493
s3_bucket = S3LoggingBucket()
14791494
guid = str(uuid.uuid4().hex)

seleniumbase/plugins/base_plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def __log_all_options_if_none_specified(self, test):
123123
test_logpath = self.options.log_path + "/" + test.id()
124124
log_helper.log_screenshot(test_logpath, test.driver)
125125
log_helper.log_test_failure_data(
126-
test_logpath, test.driver, test.browser)
126+
test, test_logpath, test.driver, test.browser)
127127
log_helper.log_page_source(test_logpath, test.driver)
128128

129129
def addSuccess(self, test, capt):

server_setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
setup(
1010
name='seleniumbase',
11-
version='1.4.3',
11+
version='1.4.4',
1212
description='Test Automation Framework - http://seleniumbase.com',
1313
long_description='Automation Framework for Simple & Reliable Web Testing',
1414
platforms='Mac * Windows * Linux * Docker',

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
setup(
99
name='seleniumbase',
10-
version='1.4.3',
10+
version='1.4.4',
1111
description='Test Automation Framework - http://seleniumbase.com',
1212
long_description='Automation Framework for Simple & Reliable Web Testing',
1313
platforms='Mac * Windows * Linux * Docker',

0 commit comments

Comments
 (0)