How to use __quit_all_drivers method in SeleniumBase

Best Python code snippet using SeleniumBase

base_case.py

Source:base_case.py Github

copy

Full Screen

...2467 self._html_report_extra.append(extra_url)2468 self._html_report_extra.append(extra_image)2469 except Exception:2470 pass2471 def __quit_all_drivers(self):2472 # Close all open browser windows2473 self._drivers_list.reverse() # Last In, First Out2474 for driver in self._drivers_list:2475 try:2476 driver.quit()2477 except AttributeError:2478 pass2479 except Exception:2480 pass2481 self.driver = None2482 self._drivers_list = []2483 def tearDown(self):2484 """2485 Be careful if a subclass of BaseCase overrides setUp()2486 You'll need to add the following line to the subclass's tearDown():2487 super(SubClassOfBaseCase, self).tearDown()2488 """2489 has_exception = False2490 if sys.version.startswith('3') and hasattr(self, '_outcome'):2491 if hasattr(self._outcome, 'errors') and self._outcome.errors:2492 has_exception = True2493 else:2494 has_exception = sys.exc_info()[1] is not None2495 if self.__page_check_failures:2496 print(2497 "\nWhen using self.delayed_assert_*() methods in your tests, "2498 "remember to call self.process_delayed_asserts() afterwards. "2499 "Now calling in tearDown()...\nFailures Detected:")2500 if not has_exception:2501 self.process_delayed_asserts()2502 else:2503 self.process_delayed_asserts(print_only=True)2504 self.is_pytest = None2505 try:2506 # This raises an exception if the test is not coming from pytest2507 self.is_pytest = pytest.config.option.is_pytest2508 except Exception:2509 # Not using pytest (probably nosetests)2510 self.is_pytest = False2511 if self.is_pytest:2512 # pytest-specific code2513 test_id = "%s.%s.%s" % (self.__class__.__module__,2514 self.__class__.__name__,2515 self._testMethodName)2516 try:2517 with_selenium = self.with_selenium2518 except Exception:2519 sub_class_name = str(2520 self.__class__.__bases__[0]).split('.')[-1].split("'")[0]2521 sub_file_name = str(self.__class__.__bases__[0]).split('.')[-2]2522 sub_file_name = sub_file_name + ".py"2523 class_name = str(self.__class__).split('.')[-1].split("'")[0]2524 file_name = str(self.__class__).split('.')[-2] + ".py"2525 class_name_used = sub_class_name2526 file_name_used = sub_file_name2527 if sub_class_name == "BaseCase":2528 class_name_used = class_name2529 file_name_used = file_name2530 fix_setup = "super(%s, self).setUp()" % class_name_used2531 fix_teardown = "super(%s, self).tearDown()" % class_name_used2532 message = ("You're overriding SeleniumBase's BaseCase setUp() "2533 "method with your own setUp() method, which breaks "2534 "SeleniumBase. You can fix this by going to your "2535 "%s class located in your %s file and adding the "2536 "following line of code AT THE BEGINNING of your "2537 "setUp() method:\n%s\n\nAlso make sure "2538 "you have added the following line of code AT THE "2539 "END of your tearDown() method:\n%s\n"2540 % (class_name_used, file_name_used,2541 fix_setup, fix_teardown))2542 raise Exception(message)2543 if with_selenium:2544 # Save a screenshot if logging is on when an exception occurs2545 if has_exception:2546 self.__add_pytest_html_extra()2547 if self.with_testing_base and has_exception:2548 test_logpath = self.log_path + "/" + test_id2549 if not os.path.exists(test_logpath):2550 os.makedirs(test_logpath)2551 if ((not self.with_screen_shots) and (2552 not self.with_basic_test_info) and (2553 not self.with_page_source)):2554 # Log everything if nothing specified (if testing_base)2555 log_helper.log_screenshot(test_logpath, self.driver)2556 log_helper.log_test_failure_data(2557 self, test_logpath, self.driver, self.browser)2558 log_helper.log_page_source(test_logpath, self.driver)2559 else:2560 if self.with_screen_shots:2561 log_helper.log_screenshot(2562 test_logpath, self.driver)2563 if self.with_basic_test_info:2564 log_helper.log_test_failure_data(2565 self, test_logpath, self.driver, self.browser)2566 if self.with_page_source:2567 log_helper.log_page_source(2568 test_logpath, self.driver)2569 # (Pytest) Finally close all open browser windows2570 self.__quit_all_drivers()2571 if self.headless:2572 if self.headless_active:2573 self.display.stop()2574 self.display = None2575 if self.with_db_reporting:2576 if has_exception:2577 self.__insert_test_result(constants.State.ERROR, True)2578 else:2579 self.__insert_test_result(constants.State.PASS, False)2580 runtime = int(time.time() * 1000) - self.execution_start_time2581 self.testcase_manager.update_execution_data(2582 self.execution_guid, runtime)2583 if self.with_s3_logging and has_exception:2584 """ If enabled, upload logs to S3 during test exceptions. """2585 from seleniumbase.core.s3_manager import S3LoggingBucket2586 s3_bucket = S3LoggingBucket()2587 guid = str(uuid.uuid4().hex)2588 path = "%s/%s" % (self.log_path, test_id)2589 uploaded_files = []2590 for logfile in os.listdir(path):2591 logfile_name = "%s/%s/%s" % (guid,2592 test_id,2593 logfile.split(path)[-1])2594 s3_bucket.upload_file(logfile_name,2595 "%s/%s" % (path, logfile))2596 uploaded_files.append(logfile_name)2597 s3_bucket.save_uploaded_file_names(uploaded_files)2598 index_file = s3_bucket.upload_index_file(test_id, guid)2599 print("\n\n*** Log files uploaded: ***\n%s\n" % index_file)2600 logging.error(2601 "\n\n*** Log files uploaded: ***\n%s\n" % index_file)2602 if self.with_db_reporting:2603 self.testcase_manager = TestcaseManager(self.database_env)2604 data_payload = TestcaseDataPayload()2605 data_payload.guid = self.testcase_guid2606 data_payload.logURL = index_file2607 self.testcase_manager.update_testcase_log_url(data_payload)2608 else:2609 # (Nosetests)2610 if has_exception:2611 test_id = "%s.%s.%s" % (self.__class__.__module__,2612 self.__class__.__name__,2613 self._testMethodName)2614 test_logpath = "latest_logs/" + test_id2615 if not os.path.exists(test_logpath):2616 os.makedirs(test_logpath)2617 log_helper.log_test_failure_data(2618 self, test_logpath, self.driver, self.browser)2619 if len(self._drivers_list) > 0:2620 log_helper.log_screenshot(test_logpath, self.driver)2621 log_helper.log_page_source(test_logpath, self.driver)2622 # Finally close all open browser windows...

Full Screen

Full Screen

webdrivertest.py

Source:webdrivertest.py Github

copy

Full Screen

...178 # time.sleep(0.12)179 # element = self.wait_for_element_visible(how, selector, constants.SMALL_TIMEOUT)180 # self.__slow_scroll_to_element(element)181 # time.sleep(0.12)182 def __quit_all_drivers(self):183 shared_drv = runtime_store.get(shared_driver, None)184 if self._reuse_session and shared_drv:185 if len(self._drivers_list) > 0:186 if self._drivers_list[0] != shared_drv:187 if shared_drv in self._drivers_list:188 self._drivers_list.remove(shared_drv)189 self._drivers_list.insert(0, shared_drv)190 self._default_driver = self._drivers_list[0]191 self.switch_to_default_driver()192 if len(self._drivers_list) > 1:193 self._drivers_list = self._drivers_list[1:]194 else:195 self._drivers_list = []196 # Close all open browser windows197 self._drivers_list.reverse() # Last In, First Out198 for driver in self._drivers_list:199 try:200 self.__generate_logs(driver)201 driver.quit()202 except AttributeError:203 pass204 except WebDriverException:205 pass206 self.driver = None207 self._default_driver = None208 self._drivers_list = []209 def __is_in_frame(self):210 return is_in_frame(self.driver)211 def is_chromium(self):212 """Return True if the browser is Chrome, Edge, or Opera."""213 self.__check_scope__()214 chromium = False215 browser_name = self.driver.capabilities["browserName"]216 if browser_name.lower() in ("chrome", "edge", "msedge", "opera"):217 chromium = True218 return chromium219 def ad_block(self):220 """Block ads that appear on the current web page."""221 ...222 def set_time_limit(self, time_limit: OptionalInt = None):223 self.__check_scope__()224 super(WebDriverTest, self).set_time_limit(time_limit)225 def sleep(self, seconds):226 self.__check_scope__()227 limit = runtime_store.get(time_limit, None)228 if limit:229 time.sleep(seconds)230 elif seconds < 0.4:231 check_if_time_limit_exceeded()232 time.sleep(seconds)233 check_if_time_limit_exceeded()234 else:235 start_ms = time.time() * 1000.0236 stop_ms = start_ms + (seconds * 1000.0)237 for x in range(int(seconds * 5)):238 check_if_time_limit_exceeded()239 now_ms = time.time() * 1000.0240 if now_ms >= stop_ms:241 break242 time.sleep(0.2)243 def teardown(self) -> None:244 self.__quit_all_drivers()245 super().teardown()246 def setup(self) -> None:247 super(WebDriverTest, self).setup()248 if self._called_setup:249 return250 # self.addfinalizer(self._generate_driver_logs)251 self._called_setup = True252 self._called_teardown = False253 # self.slow_mode = self.config.getoption("slow_mode", False)254 # self.demo_mode = self.config.getoption("demo_mode", False)255 # self.demo_sleep = sb_config.demo_sleep256 # self.highlights = sb_config.highlights257 # self.time_limit = sb_config._time_limit258 # sb_config.time_limit = sb_config._time_limit # Reset between tests...

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