|
| 1 | +from robot.api import logger |
| 2 | +from selenium import webdriver |
| 3 | + |
| 4 | +from SeleniumLibrary.base import LibraryComponent, keyword |
| 5 | +from SeleniumLibrary.keywords import BrowserManagementKeywords |
| 6 | +from SeleniumLibrary.keywords.webdrivertools import WebDriverCreator |
| 7 | + |
| 8 | + |
| 9 | +class OpenBrowserExample(LibraryComponent): |
| 10 | + |
| 11 | + def __init__(self, ctx): |
| 12 | + LibraryComponent.__init__(self, ctx) |
| 13 | + self._new_creator = NewWebDriverCreator(self.log_dir) |
| 14 | + |
| 15 | + @keyword |
| 16 | + def open_browser(self, url, browser='firefox', alias=None, |
| 17 | + remote_url=False, desired_capabilities=None, |
| 18 | + ff_profile_dir=None, options=None, service_log_path=None, |
| 19 | + extra_dictionary=None): |
| 20 | + self._new_creator.extra_dictionary = extra_dictionary |
| 21 | + browser_manager = BrowserManagementKeywords(self.ctx) |
| 22 | + browser_manager._make_driver = self._make_driver |
| 23 | + browser_manager.open_browser(url, browser=browser, alias=alias, |
| 24 | + remote_url=remote_url, desired_capabilities=desired_capabilities, |
| 25 | + ff_profile_dir=ff_profile_dir, options=options, |
| 26 | + service_log_path=service_log_path) |
| 27 | + |
| 28 | + def _make_driver(self, browser, desired_capabilities=None, profile_dir=None, |
| 29 | + remote=None, options=None, service_log_path=None): |
| 30 | + driver = self._new_creator.create_driver( |
| 31 | + browser=browser, desired_capabilities=desired_capabilities, remote_url=remote, |
| 32 | + profile_dir=profile_dir, options=options, service_log_path=service_log_path) |
| 33 | + driver.set_script_timeout(self.ctx.timeout) |
| 34 | + driver.implicitly_wait(self.ctx.implicit_wait) |
| 35 | + if self.ctx.speed: |
| 36 | + self._monkey_patch_speed(driver) |
| 37 | + return driver |
| 38 | + |
| 39 | + |
| 40 | +class NewWebDriverCreator(WebDriverCreator): |
| 41 | + |
| 42 | + def create_driver(self, browser, desired_capabilities, remote_url, |
| 43 | + profile_dir=None, options=None, service_log_path=None): |
| 44 | + self.browser_names['seleniumwire'] = 'seleniumwire' |
| 45 | + browser = self._normalise_browser_name(browser) |
| 46 | + creation_method = self._get_creator_method(browser) |
| 47 | + desired_capabilities = self._parse_capabilities(desired_capabilities, browser) |
| 48 | + service_log_path = self._get_log_path(service_log_path) |
| 49 | + options = self.selenium_options.create(self.browser_names.get(browser), options) |
| 50 | + if service_log_path: |
| 51 | + logger.info('Browser driver log file created to: %s' % service_log_path) |
| 52 | + self._create_directory(service_log_path) |
| 53 | + if (creation_method == self.create_firefox |
| 54 | + or creation_method == self.create_headless_firefox): |
| 55 | + return creation_method(desired_capabilities, remote_url, profile_dir, |
| 56 | + options=options, service_log_path=service_log_path) |
| 57 | + if creation_method == self.create_seleniumwire: |
| 58 | + return creation_method(desired_capabilities, remote_url, options=options, |
| 59 | + service_log_path=service_log_path) |
| 60 | + return creation_method(desired_capabilities, remote_url, options=options, |
| 61 | + service_log_path=service_log_path) |
| 62 | + |
| 63 | + def create_seleniumwire(self, desired_capabilities, remote_url, options=None, service_log_path=None): |
| 64 | + logger.info(self.extra_dictionary) |
| 65 | + return webdriver.Chrome() |
0 commit comments