Skip to content

Commit 32d7f4d

Browse files
committed
Add S3 logging code to base_case for pytest
1 parent 06bc213 commit 32d7f4d

File tree

1 file changed

+48
-7
lines changed

1 file changed

+48
-7
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from pyvirtualdisplay import Display
1717
from seleniumbase.config import settings
1818
from seleniumbase.core.application_manager import ApplicationManager
19+
from seleniumbase.core.s3_manager import S3LoggingBucket
1920
from seleniumbase.core.testcase_manager import ExecutionQueryPayload
2021
from seleniumbase.core.testcase_manager import TestcaseDataPayload
2122
from seleniumbase.core.testcase_manager import TestcaseManager
@@ -340,6 +341,11 @@ def setUp(self):
340341
self.headless_active = False
341342
self.with_testing_base = pytest.config.option.with_testing_base
342343
self.with_db_reporting = pytest.config.option.with_db_reporting
344+
self.with_s3_logging = pytest.config.option.with_s3_logging
345+
self.with_screen_shots = pytest.config.option.with_screen_shots
346+
self.with_basic_test_info = (
347+
pytest.config.option.with_basic_test_info)
348+
self.with_page_source = pytest.config.option.with_page_source
343349
self.database_env = pytest.config.option.database_env
344350
self.log_path = pytest.config.option.log_path
345351
self.browser = pytest.config.option.browser
@@ -416,13 +422,24 @@ def tearDown(self):
416422
test_logpath = self.log_path + "/" + test_id
417423
if not os.path.exists(test_logpath):
418424
os.makedirs(test_logpath)
419-
# Handle screenshot logging
420-
log_helper.log_screenshot(test_logpath, self.driver)
421-
# Handle basic test info logging
422-
log_helper.log_test_failure_data(
423-
test_logpath, self.driver, self.browser)
424-
# Handle page source logging
425-
log_helper.log_page_source(test_logpath, self.driver)
425+
if ((not self.with_screen_shots) and
426+
(not self.with_basic_test_info) and
427+
(not self.with_page_source)):
428+
# Log everything if nothing specified (if testing_base)
429+
log_helper.log_screenshot(test_logpath, self.driver)
430+
log_helper.log_test_failure_data(
431+
test_logpath, self.driver, self.browser)
432+
log_helper.log_page_source(test_logpath, self.driver)
433+
else:
434+
if self.with_screen_shots:
435+
log_helper.log_screenshot(
436+
test_logpath, self.driver)
437+
if self.with_basic_test_info:
438+
log_helper.log_test_failure_data(
439+
test_logpath, self.driver, self.browser)
440+
if self.with_page_source:
441+
log_helper.log_page_source(
442+
test_logpath, self.driver)
426443
# Finally close the browser
427444
self.driver.quit()
428445
if self.headless:
@@ -436,3 +453,27 @@ def tearDown(self):
436453
runtime = int(time.time() * 1000) - self.execution_start_time
437454
self.testcase_manager.update_execution_data(
438455
self.execution_guid, runtime)
456+
if self.with_s3_logging and (sys.exc_info()[1] is not None):
457+
""" After each testcase, upload logs to the S3 bucket. """
458+
s3_bucket = S3LoggingBucket()
459+
guid = str(uuid.uuid4().hex)
460+
path = "%s/%s" % (self.log_path, test_id)
461+
uploaded_files = []
462+
for logfile in os.listdir(path):
463+
logfile_name = "%s/%s/%s" % (guid,
464+
test_id,
465+
logfile.split(path)[-1])
466+
s3_bucket.upload_file(logfile_name,
467+
"%s/%s" % (path, logfile))
468+
uploaded_files.append(logfile_name)
469+
s3_bucket.save_uploaded_file_names(uploaded_files)
470+
index_file = s3_bucket.upload_index_file(test_id, guid)
471+
print "\n\n*** Log files uploaded: ***\n%s\n" % index_file
472+
logging.error(
473+
"\n\n*** Log files uploaded: ***\n%s\n" % index_file)
474+
if self.with_db_reporting:
475+
self.testcase_manager = TestcaseManager(self.database_env)
476+
data_payload = TestcaseDataPayload()
477+
data_payload.guid = guid
478+
data_payload.logURL = index_file
479+
self.testcase_manager.update_testcase_log_url(data_payload)

0 commit comments

Comments
 (0)