How to use __create_log_path_as_needed method in SeleniumBase

Best Python code snippet using SeleniumBase

base_case.py

Source:base_case.py Github

copy

Full Screen

...4166 test_id = "%s.%s.%s" % (self.__class__.__module__,4167 self.__class__.__name__,4168 self._testMethodName)4169 return test_id4170 def __create_log_path_as_needed(self, test_logpath):4171 if not os.path.exists(test_logpath):4172 try:4173 os.makedirs(test_logpath)4174 except Exception:4175 pass # Only reachable during multi-threaded runs4176 def save_teardown_screenshot(self):4177 """ (Should ONLY be used at the start of custom tearDown() methods.)4178 This method takes a screenshot of the current web page for a4179 failing test (or when running your tests with --save-screenshot).4180 That way your tearDown() method can navigate away from the last4181 page where the test failed, and still get the correct screenshot4182 before performing tearDown() steps on other pages. If this method4183 is not included in your custom tearDown() method, a screenshot4184 will still be taken after the last step of your tearDown(), where4185 you should be calling "super(SubClassOfBaseCase, self).tearDown()"4186 """4187 if self.__has_exception() or self.save_screenshot_after_test:4188 test_id = self.__get_test_id()4189 test_logpath = self.log_path + "/" + test_id4190 self.__create_log_path_as_needed(test_logpath)4191 self.__set_last_page_screenshot()4192 if self.is_pytest:4193 self.__add_pytest_html_extra()4194 def tearDown(self):4195 """4196 Be careful if a subclass of BaseCase overrides setUp()4197 You'll need to add the following line to the subclass's tearDown():4198 super(SubClassOfBaseCase, self).tearDown()4199 """4200 self.__slow_mode_pause_if_active()4201 has_exception = self.__has_exception()4202 if self.__delayed_assert_failures:4203 print(4204 "\nWhen using self.delayed_assert_*() methods in your tests, "4205 "remember to call self.process_delayed_asserts() afterwards. "4206 "Now calling in tearDown()...\nFailures Detected:")4207 if not has_exception:4208 self.process_delayed_asserts()4209 else:4210 self.process_delayed_asserts(print_only=True)4211 if self.is_pytest:4212 # pytest-specific code4213 test_id = self.__get_test_id()4214 try:4215 with_selenium = self.with_selenium4216 except Exception:4217 sub_class_name = str(4218 self.__class__.__bases__[0]).split('.')[-1].split("'")[0]4219 sub_file_name = str(self.__class__.__bases__[0]).split('.')[-2]4220 sub_file_name = sub_file_name + ".py"4221 class_name = str(self.__class__).split('.')[-1].split("'")[0]4222 file_name = str(self.__class__).split('.')[-2] + ".py"4223 class_name_used = sub_class_name4224 file_name_used = sub_file_name4225 if sub_class_name == "BaseCase":4226 class_name_used = class_name4227 file_name_used = file_name4228 fix_setup = "super(%s, self).setUp()" % class_name_used4229 fix_teardown = "super(%s, self).tearDown()" % class_name_used4230 message = ("You're overriding SeleniumBase's BaseCase setUp() "4231 "method with your own setUp() method, which breaks "4232 "SeleniumBase. You can fix this by going to your "4233 "%s class located in your %s file and adding the "4234 "following line of code AT THE BEGINNING of your "4235 "setUp() method:\n%s\n\nAlso make sure "4236 "you have added the following line of code AT THE "4237 "END of your tearDown() method:\n%s\n"4238 % (class_name_used, file_name_used,4239 fix_setup, fix_teardown))4240 raise Exception(message)4241 if with_selenium:4242 # Save a screenshot if logging is on when an exception occurs4243 if has_exception:4244 self.__add_pytest_html_extra()4245 if self.with_testing_base and not has_exception and (4246 self.save_screenshot_after_test):4247 test_logpath = self.log_path + "/" + test_id4248 self.__create_log_path_as_needed(test_logpath)4249 if not self.__last_page_screenshot_png:4250 self.__set_last_page_screenshot()4251 log_helper.log_screenshot(4252 test_logpath,4253 self.driver,4254 self.__last_page_screenshot_png)4255 self.__add_pytest_html_extra()4256 if self.with_testing_base and has_exception:4257 test_logpath = self.log_path + "/" + test_id4258 self.__create_log_path_as_needed(test_logpath)4259 if ((not self.with_screen_shots) and (4260 not self.with_basic_test_info) and (4261 not self.with_page_source)):4262 # Log everything if nothing specified (if testing_base)4263 if not self.__last_page_screenshot_png:4264 self.__set_last_page_screenshot()4265 log_helper.log_screenshot(4266 test_logpath,4267 self.driver,4268 self.__last_page_screenshot_png)4269 log_helper.log_test_failure_data(4270 self, test_logpath, self.driver, self.browser)4271 log_helper.log_page_source(test_logpath, self.driver)4272 else:4273 if self.with_screen_shots:4274 if not self.__last_page_screenshot_png:4275 self.__set_last_page_screenshot()4276 log_helper.log_screenshot(4277 test_logpath,4278 self.driver,4279 self.__last_page_screenshot_png)4280 if self.with_basic_test_info:4281 log_helper.log_test_failure_data(4282 self, test_logpath, self.driver, self.browser)4283 if self.with_page_source:4284 log_helper.log_page_source(4285 test_logpath, self.driver)4286 # (Pytest) Finally close all open browser windows4287 self.__quit_all_drivers()4288 if self.headless:4289 if self.headless_active:4290 try:4291 self.display.stop()4292 except AttributeError:4293 pass4294 except Exception:4295 pass4296 self.display = None4297 if self.with_db_reporting:4298 if has_exception:4299 self.__insert_test_result(constants.State.ERROR, True)4300 else:4301 self.__insert_test_result(constants.State.PASS, False)4302 runtime = int(time.time() * 1000) - self.execution_start_time4303 self.testcase_manager.update_execution_data(4304 self.execution_guid, runtime)4305 if self.with_s3_logging and has_exception:4306 """ If enabled, upload logs to S3 during test exceptions. """4307 from seleniumbase.core.s3_manager import S3LoggingBucket4308 s3_bucket = S3LoggingBucket()4309 guid = str(uuid.uuid4().hex)4310 path = "%s/%s" % (self.log_path, test_id)4311 uploaded_files = []4312 for logfile in os.listdir(path):4313 logfile_name = "%s/%s/%s" % (guid,4314 test_id,4315 logfile.split(path)[-1])4316 s3_bucket.upload_file(logfile_name,4317 "%s/%s" % (path, logfile))4318 uploaded_files.append(logfile_name)4319 s3_bucket.save_uploaded_file_names(uploaded_files)4320 index_file = s3_bucket.upload_index_file(test_id, guid)4321 print("\n\n*** Log files uploaded: ***\n%s\n" % index_file)4322 logging.info(4323 "\n\n*** Log files uploaded: ***\n%s\n" % index_file)4324 if self.with_db_reporting:4325 self.testcase_manager = TestcaseManager(self.database_env)4326 data_payload = TestcaseDataPayload()4327 data_payload.guid = self.testcase_guid4328 data_payload.logURL = index_file4329 self.testcase_manager.update_testcase_log_url(data_payload)4330 else:4331 # (Nosetests)4332 if has_exception:4333 test_id = self.__get_test_id()4334 test_logpath = self.log_path + "/" + test_id4335 self.__create_log_path_as_needed(test_logpath)4336 log_helper.log_test_failure_data(4337 self, test_logpath, self.driver, self.browser)4338 if len(self._drivers_list) > 0:4339 if not self.__last_page_screenshot_png:4340 self.__set_last_page_screenshot()4341 log_helper.log_screenshot(4342 test_logpath,4343 self.driver,4344 self.__last_page_screenshot_png)4345 log_helper.log_page_source(test_logpath, self.driver)4346 elif self.save_screenshot_after_test:4347 test_id = self.__get_test_id()4348 test_logpath = self.log_path + "/" + test_id4349 self.__create_log_path_as_needed(test_logpath)4350 if not self.__last_page_screenshot_png:4351 self.__set_last_page_screenshot()4352 log_helper.log_screenshot(4353 test_logpath,4354 self.driver,4355 self.__last_page_screenshot_png)4356 if self.report_on:4357 self._last_page_screenshot = self.__last_page_screenshot_png4358 try:4359 self._last_page_url = self.get_current_url()4360 except Exception:4361 self._last_page_url = "(Error: Unknown URL)"4362 # Finally close all open browser windows4363 self.__quit_all_drivers()

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run SeleniumBase automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful