def afterTest(self, test): """After each testcase, upload logs to the S3 bucket.""" s3_bucket = S3LoggingBucket() guid = str(uuid.uuid4().hex) path = "%s/%s" % (self.options.log_path, test.test.id()) uploaded_files = [] for logfile in os.listdir(path): logfile_name = "%s/%s/%s" % ( guid, test.test.id(), logfile.split(path)[-1], ) s3_bucket.upload_file(logfile_name, "%s/%s" % (path, logfile)) uploaded_files.append(logfile_name) s3_bucket.save_uploaded_file_names(uploaded_files) index_file = s3_bucket.upload_index_file(test.id(), guid) print("\n\n*** Log files uploaded: ***\n%s\n" % index_file) logging.error("\n\n*** Log files uploaded: ***\n%s\n" % index_file) # If the SB database plugin is also being used, # attach a link to the logs index database row. if hasattr(test.test, "testcase_guid"): from seleniumbase.core.testcase_manager import ( TestcaseDataPayload, TestcaseManager, ) self.testcase_manager = TestcaseManager(self.options.database_env) data_payload = TestcaseDataPayload() data_payload.guid = test.test.testcase_guid data_payload.log_url = index_file self.testcase_manager.update_testcase_log_url(data_payload)
def tearDown(self): """ pytest-specific code Be careful if a subclass of BaseCase overrides setUp() You'll need to add the following line to the subclass's tearDown(): super(SubClassOfBaseCase, self).tearDown() """ if self.page_check_failures: # self.process_checks() was not called after checks were made. # We will log those now here, but without raising an exception. exception_output = '' exception_output += "\n*** FAILED CHECKS FOR: %s\n" % self.id() for tb in self.page_check_failures: exception_output += "%s\n" % tb logging.exception(exception_output) if self.is_pytest: test_id = "%s.%s.%s" % (self.__class__.__module__, self.__class__.__name__, self._testMethodName) if self.with_selenium: # Save a screenshot if logging is on when an exception occurs if self.with_testing_base and (sys.exc_info()[1] is not None): test_logpath = self.log_path + "/" + test_id if not os.path.exists(test_logpath): os.makedirs(test_logpath) if ((not self.with_screen_shots) and (not self.with_basic_test_info) and (not self.with_page_source)): # Log everything if nothing specified (if testing_base) log_helper.log_screenshot(test_logpath, self.driver) log_helper.log_test_failure_data( test_logpath, self.driver, self.browser) log_helper.log_page_source(test_logpath, self.driver) else: if self.with_screen_shots: log_helper.log_screenshot(test_logpath, self.driver) if self.with_basic_test_info: log_helper.log_test_failure_data( test_logpath, self.driver, self.browser) if self.with_page_source: log_helper.log_page_source(test_logpath, self.driver) # Finally close the browser self.driver.quit() if self.headless: if self.headless_active: self.display.stop() if self.with_db_reporting: if sys.exc_info()[1] is not None: self.__insert_test_result(constants.State.ERROR, True) else: self.__insert_test_result(constants.State.PASS, False) runtime = int(time.time() * 1000) - self.execution_start_time self.testcase_manager.update_execution_data( self.execution_guid, runtime) if self.with_s3_logging and (sys.exc_info()[1] is not None): """ After each testcase, upload logs to the S3 bucket. """ s3_bucket = S3LoggingBucket() guid = str(uuid.uuid4().hex) path = "%s/%s" % (self.log_path, test_id) uploaded_files = [] for logfile in os.listdir(path): logfile_name = "%s/%s/%s" % (guid, test_id, logfile.split(path)[-1]) s3_bucket.upload_file(logfile_name, "%s/%s" % (path, logfile)) uploaded_files.append(logfile_name) s3_bucket.save_uploaded_file_names(uploaded_files) index_file = s3_bucket.upload_index_file(test_id, guid) print "\n\n*** Log files uploaded: ***\n%s\n" % index_file logging.error("\n\n*** Log files uploaded: ***\n%s\n" % index_file) if self.with_db_reporting: self.testcase_manager = TestcaseManager(self.database_env) data_payload = TestcaseDataPayload() data_payload.guid = self.testcase_guid data_payload.logURL = index_file self.testcase_manager.update_testcase_log_url(data_payload)