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 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 database plugin is running, 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.logURL = index_file self.testcase_manager.update_testcase_log_url(data_payload)
def __insert_test_result(self, state, err): data_payload = TestcaseDataPayload() data_payload.runtime = int(time.time() * 1000) - self.case_start_time data_payload.guid = self.testcase_guid data_payload.execution_guid = self.execution_guid data_payload.state = state if err: tb_string = traceback.format_exc() if "Message: " in tb_string: data_payload.message = "Message: " + tb_string.split( "Message: ")[-1] elif "Exception: " in tb_string: data_payload.message = tb_string.split("Exception: ")[-1] elif "Error: " in tb_string: data_payload.message = tb_string.split("Error: ")[-1] else: data_payload.message = "Unknown Error: See Stacktrace" self.testcase_manager.update_testcase_data(data_payload)
def startTest(self, test): """ At the start of the test, set the testcase details. """ data_payload = TestcaseDataPayload() self.testcase_guid = str(uuid.uuid4()) data_payload.guid = self.testcase_guid data_payload.execution_guid = self.execution_guid if hasattr(test, "browser"): data_payload.browser = test.browser else: data_payload.browser = "N/A" data_payload.test_address = test.id() application = ApplicationManager.generate_application_string(test) data_payload.env = application.split('.')[0] data_payload.start_time = application.split('.')[1] data_payload.state = constants.State.NOTRUN self.testcase_manager.insert_testcase_data(data_payload) self.case_start_time = int(time.time() * 1000) # Make the testcase guid available to other plugins test.testcase_guid = self.testcase_guid
def __insert_test_result(self, state, test, err=None): data_payload = TestcaseDataPayload() data_payload.runtime = int(time.time() * 1000) - self.case_start_time data_payload.guid = self.testcase_guid data_payload.execution_guid = self.execution_guid data_payload.state = state if err is not None: data_payload.message = err[1].__str__().split( '''-------------------- >> ''' '''begin captured logging''' ''' << --------------------''', 1)[0] self.testcase_manager.update_testcase_data(data_payload)
def startTest(self, test): """At the start of the test, set the testcase details.""" data_payload = TestcaseDataPayload() self.testcase_guid = str(uuid.uuid4()) data_payload.guid = self.testcase_guid data_payload.execution_guid = self.execution_guid if hasattr(test, "browser"): data_payload.browser = test.browser else: data_payload.browser = "N/A" data_payload.testcaseAddress = test.id() application = ApplicationManager.generate_application_string(test) data_payload.env = application.split('.')[0] data_payload.start_time = application.split('.')[1] data_payload.state = constants.State.NOTRUN self.testcase_manager.insert_testcase_data(data_payload) self.case_start_time = int(time.time() * 1000) # Make the testcase guid available to other plugins test.testcase_guid = self.testcase_guid
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)
def setUp(self): """ pytest-specific code Be careful if a subclass of BaseCase overrides setUp() You'll need to add the following line to the subclass setUp() method: super(SubClassOfBaseCase, self).setUp() """ self.is_pytest = None try: # This raises an exception if the test is not coming from pytest self.is_pytest = pytest.config.option.is_pytest except Exception: # Not using pytest (probably nosetests) self.is_pytest = False if self.is_pytest: test_id = "%s.%s.%s" % (self.__class__.__module__, self.__class__.__name__, self._testMethodName) self.with_selenium = pytest.config.option.with_selenium self.headless = pytest.config.option.headless self.headless_active = False self.with_testing_base = pytest.config.option.with_testing_base self.with_db_reporting = pytest.config.option.with_db_reporting self.with_s3_logging = pytest.config.option.with_s3_logging self.with_screen_shots = pytest.config.option.with_screen_shots self.with_basic_test_info = ( pytest.config.option.with_basic_test_info) self.with_page_source = pytest.config.option.with_page_source self.database_env = pytest.config.option.database_env self.log_path = pytest.config.option.log_path self.browser = pytest.config.option.browser self.data = pytest.config.option.data self.demo_mode = pytest.config.option.demo_mode self.demo_sleep = pytest.config.option.demo_sleep if self.with_db_reporting: self.execution_guid = str(uuid.uuid4()) self.testcase_guid = None self.execution_start_time = 0 self.case_start_time = 0 self.application = None self.testcase_manager = None self.error_handled = False self.testcase_manager = TestcaseManager(self.database_env) # exec_payload = ExecutionQueryPayload() exec_payload.execution_start_time = int(time.time() * 1000) self.execution_start_time = exec_payload.execution_start_time exec_payload.guid = self.execution_guid exec_payload.username = getpass.getuser() self.testcase_manager.insert_execution_data(exec_payload) # data_payload = TestcaseDataPayload() self.testcase_guid = str(uuid.uuid4()) data_payload.guid = self.testcase_guid data_payload.execution_guid = self.execution_guid if self.with_selenium: data_payload.browser = self.browser else: data_payload.browser = "N/A" data_payload.testcaseAddress = test_id application = ApplicationManager.generate_application_string( self._testMethodName) data_payload.env = application.split('.')[0] data_payload.start_time = application.split('.')[1] data_payload.state = constants.State.NOTRUN self.testcase_manager.insert_testcase_data(data_payload) self.case_start_time = int(time.time() * 1000) if self.headless: self.display = Display(visible=0, size=(1200, 800)) self.display.start() self.headless_active = True if self.with_selenium: self.driver = browser_launcher.get_driver(self.browser)
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)