def __init__(self, timeout=5.0, implicit_wait=0.0, run_on_failure='Capture Page Screenshot', screenshot_root_directory=None): self.timeout = timestr_to_secs(timeout) self.implicit_wait = timestr_to_secs(implicit_wait) self.speed = 0.0 self.run_on_failure_keyword \ = RunOnFailureKeywords.resolve_keyword(run_on_failure) self._running_on_failure_keyword = False self.screenshot_root_directory = screenshot_root_directory libraries = [ AlertKeywords(self), BrowserManagementKeywords(self), CookieKeywords(self), ElementKeywords(self), FormElementKeywords(self), FrameKeywords(self), JavaScriptKeywords(self), RunOnFailureKeywords(self), ScreenshotKeywords(self), SelectElementKeywords(self), TableElementKeywords(self), WaitingKeywords(self), WindowKeywords(self), KeyboardKeywords(self) ] self._drivers = WebDriverCache() DynamicCore.__init__(self, libraries) self.ROBOT_LIBRARY_LISTENER = LibraryListener() self._element_finder = ElementFinder(self)
def __init__(self, timeout=5.0, implicit_wait=0.0, run_on_failure='Capture Page Screenshot', screenshot_root_directory=None): """SeleniumLibrary can be imported with optional arguments. `timeout` is the default timeout used to wait for all waiting actions. It can be later set with `Set Selenium Timeout`. 'implicit_wait' is the implicit timeout that Selenium waits when looking for elements. It can be later set with `Set Selenium Implicit Wait`. See `WebDriver: Advanced Usage`__ section of the SeleniumHQ documentation for more information about WebDriver's implicit wait functionality. __ http://seleniumhq.org/docs/04_webdriver_advanced.html#explicit-and-implicit-waits `run_on_failure` specifies the name of a keyword (from any available libraries) to execute when a SeleniumLibrary keyword fails. By default `Capture Page Screenshot` will be used to take a screenshot of the current page. Using the value "Nothing" will disable this feature altogether. See `Register Keyword To Run On Failure` keyword for more information about this functionality. `screenshot_root_directory` specifies the default root directory that screenshots should be stored in. If not provided the default directory will be where robotframework places its logfile. Examples: | Library `|` SeleniumLibrary `|` 15 | # Sets default timeout to 15 seconds | | Library `|` SeleniumLibrary `|` 0 `|` 5 | # Sets default timeout to 0 seconds and default implicit_wait to 5 seconds | | Library `|` SeleniumLibrary `|` 5 `|` run_on_failure=Log Source | # Sets default timeout to 5 seconds and runs `Log Source` on failure | | Library `|` SeleniumLibrary `|` implicit_wait=5 `|` run_on_failure=Log Source | # Sets default implicit_wait to 5 seconds and runs `Log Source` on failure | | Library `|` SeleniumLibrary `|` timeout=10 `|` run_on_failure=Nothing | # Sets default timeout to 10 seconds and does nothing on failure | """ self.timeout = timestr_to_secs(timeout) self.implicit_wait = timestr_to_secs(implicit_wait) self.speed = 0.0 self.run_on_failure_keyword \ = RunOnFailureKeywords.resolve_keyword(run_on_failure) self._running_on_failure_keyword = False self.screenshot_root_directory = screenshot_root_directory libraries = [ AlertKeywords(self), BrowserManagementKeywords(self), RunOnFailureKeywords(self), ElementKeywords(self), TableElementKeywords(self), FormElementKeywords(self), SelectElementKeywords(self), JavaScriptKeywords(self), CookieKeywords(self), ScreenshotKeywords(self), WaitingKeywords(self) ] self._browsers = BrowserCache() DynamicCore.__init__(self, libraries) self.ROBOT_LIBRARY_LISTENER = LibraryListener() self.element_finder = ElementFinder(self) self._table_element_finder = TableElementFinder(self)
def __init__(self, timeout=5.0, implicit_wait=0.0, run_on_failure='Capture Page Screenshot', screenshot_root_directory=None, plugins=None, event_firing_webdriver=None): """SeleniumLibrary can be imported with several optional arguments. - ``timeout``: Default value for `timeouts` used with ``Wait ...`` keywords. - ``implicit_wait``: Default value for `implicit wait` used when locating elements. - ``run_on_failure``: Default action for the `run-on-failure functionality`. - ``screenshot_root_directory``: Location where possible screenshots are created. If not given, the directory where the log file is written is used. - ``plugins``: Allows extending the SeleniumLibrary with external Python classes. - ``event_firing_webdriver``: Class for wrapping Selenium with [https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.event_firing_webdriver.html#module-selenium.webdriver.support.event_firing_webdriver|EventFiringWebDriver] """ self.timeout = timestr_to_secs(timeout) self.implicit_wait = timestr_to_secs(implicit_wait) self.speed = 0.0 self.run_on_failure_keyword \ = RunOnFailureKeywords.resolve_keyword(run_on_failure) self._running_on_failure_keyword = False self.screenshot_root_directory = screenshot_root_directory self._element_finder = ElementFinder(self) self._plugin_keywords = [] libraries = [ AlertKeywords(self), BrowserManagementKeywords(self), CookieKeywords(self), ElementKeywords(self), FormElementKeywords(self), FrameKeywords(self), JavaScriptKeywords(self), RunOnFailureKeywords(self), ScreenshotKeywords(self), SelectElementKeywords(self), TableElementKeywords(self), WaitingKeywords(self), WindowKeywords(self) ] if is_truthy(plugins): plugin_libs = self._parse_plugins(plugins) libraries = libraries + plugin_libs self._drivers = WebDriverCache() DynamicCore.__init__(self, libraries) self.ROBOT_LIBRARY_LISTENER = LibraryListener() if is_truthy(event_firing_webdriver): self.event_firing_webdriver = self._parse_listener( event_firing_webdriver) else: self.event_firing_webdriver = None self._running_keyword = None
def __init__(self, timeout=5.0, implicit_wait=0.0, run_on_failure='Capture Page Screenshot', screenshot_root_directory=None): """SeleniumLibrary can be imported with several optional arguments. - ``timeout``: Default value for `timeouts` used with ``Wait ...`` keywords. - ``implicit_wait``: Default value for `implicit wait` used when locating elements. - ``run_on_failure``: Default action for the `run-on-failure functionality`. - ``screenshot_root_directory``: Location where possible screenshots are created. If not given, the directory where the log file is written is used. """ self.timeout = timestr_to_secs(timeout) self.implicit_wait = timestr_to_secs(implicit_wait) self.speed = 0.0 self.run_on_failure_keyword \ = RunOnFailureKeywords.resolve_keyword(run_on_failure) self._running_on_failure_keyword = False self.screenshot_root_directory = screenshot_root_directory libraries = [ GerenciamentoDeBrowser(self), ] self._drivers = WebDriverCache() DynamicCore.__init__(self, libraries) self.ROBOT_LIBRARY_LISTENER = LibraryListener() self._element_finder = ElementFinder(self)
def __init__(self, timeout=5.0, implicit_wait=0.0, run_on_failure='Capture Page Screenshot', screenshot_root_directory=None, demo=False): """SeleniumLibrary can be imported with several optional arguments. - ``timeout``: Default value for `timeouts` used with ``Wait ...`` keywords. - ``implicit_wait``: Default value for `implicit wait` used when locating elements. - ``run_on_failure``: Default action for the `run-on-failure functionality`. - ``screenshot_root_directory``: Location where possible screenshots are created. If not given, the directory where the log file is written is used. - ``Demo`` Flash the element before click, input and etc actions. Set value True if you want it. """ self.timeout = timestr_to_secs(timeout) self.implicit_wait = timestr_to_secs(implicit_wait) self.speed = 0.0 self.run_on_failure_keyword \ = RunOnFailureKeywords.resolve_keyword(run_on_failure) self._running_on_failure_keyword = False self.screenshot_root_directory = screenshot_root_directory self.demo = demo libraries = [ AlertKeywords(self), BrowserManagementKeywords(self), CookieKeywords(self), ElementKeywords(self), FormElementKeywords(self), FrameKeywords(self), JavaScriptKeywords(self), RunOnFailureKeywords(self), ScreenshotKeywords(self), SelectElementKeywords(self), TableElementKeywords(self), WaitingKeywords(self), WindowKeywords(self) ] self._drivers = WebDriverCache() DynamicCore.__init__(self, libraries) self.ROBOT_LIBRARY_LISTENER = LibraryListener() self._element_finder = ElementFinder(self)
def set_browser_implicit_wait(self, value): """Sets the implicit wait value used by Selenium. Same as `Set Selenium Implicit Wait` but only affects the current browser. """ self.driver.implicitly_wait(timestr_to_secs(value))
def _wait_until_no_error(self, timeout, wait_func, *args): timeout = timestr_to_secs(timeout) if is_truthy( timeout) else self.ctx.timeout maxtime = time.time() + timeout while True: timeout_error = wait_func(*args) if not timeout_error: return if time.time() > maxtime: raise AssertionError(timeout_error) time.sleep(0.2)
def __init__(self, timeout=5.0, implicit_wait=0.0, run_on_failure='Capture Page Screenshot', screenshot_root_directory=None): """SeleniumLibrary can be imported with several optional arguments. - ``timeout``: Default value for `timeouts` used with ``Wait ...`` keywords. - ``implicit_wait``: Default value for `implicit wait` used when locating elements. - ``run_on_failure``: Default action for the `run-on-failure functionality`. - ``screenshot_root_directory``: Location where possible screenshots are created. If not given, the directory where the log file is written is used. """ self.timeout = timestr_to_secs(timeout) self.implicit_wait = timestr_to_secs(implicit_wait) self.speed = 0.0 self.run_on_failure_keyword \ = RunOnFailureKeywords.resolve_keyword(run_on_failure) self._running_on_failure_keyword = False self.screenshot_root_directory = screenshot_root_directory libraries = [ AlertKeywords(self), BrowserManagementKeywords(self), RunOnFailureKeywords(self), ElementKeywords(self), TableElementKeywords(self), FormElementKeywords(self), SelectElementKeywords(self), JavaScriptKeywords(self), CookieKeywords(self), ScreenshotKeywords(self), WaitingKeywords(self) ] self._browsers = BrowserCache() DynamicCore.__init__(self, libraries) self.ROBOT_LIBRARY_LISTENER = LibraryListener() self.element_finder = ElementFinder(self) self.table_element_finder = TableElementFinder(self)
def set_browser_implicit_wait(self, seconds): """Sets current browser's implicit wait in seconds. From selenium 2 function 'Sets a sticky timeout to implicitly wait for an element to be found, or a command to complete. This method only needs to be called one time per session.' Example: | Set Browser Implicit Wait | 10 seconds | See also `Set Selenium Implicit Wait`. """ self.browser.implicitly_wait(timestr_to_secs(seconds))
def set_selenium_implicit_wait(self, seconds): """Sets Selenium 2's default implicit wait in seconds and sets the implicit wait for all open browsers. From selenium 2 function 'Sets a sticky timeout to implicitly wait for an element to be found, or a command to complete. This method only needs to be called one time per session.' Example: | ${orig wait} = | Set Selenium Implicit Wait | 10 seconds | | Perform AJAX call that is slow | | Set Selenium Implicit Wait | ${orig wait} | """ old_wait = self.get_selenium_implicit_wait() self.ctx.implicit_wait = timestr_to_secs(seconds) for browser in self.browsers.get_open_browsers(): browser.implicitly_wait(self.ctx.implicit_wait) return old_wait
def set_selenium_speed(self, value): """Sets the delay that is waited after each Selenium command. The value can be given as a number that is considered to be seconds or as a human-readable string like ``1 second``. The previous value is returned and can be used to restore the original value later if needed. See the `Selenium Speed` section above for more information. Example: | `Set Selenium Speed` | 0.5 seconds | """ old_speed = self.get_selenium_speed() self.ctx.speed = timestr_to_secs(value) for driver in self.drivers.active_drivers: self._monkey_patch_speed(driver) return old_speed
def set_selenium_speed(self, value): """Sets the delay that is waited after each Selenium command. The value can be given as a number that is considered to be seconds or as a human readable string like ``1 second``. The previous value is returned and can be used to restore the original value later if needed. See the `Selenium Speed` section above for more information. Example: | `Set Selenium Speed` | 0.5 seconds | """ old_speed = self.get_selenium_speed() self.ctx.speed = timestr_to_secs(value) for driver in self.drivers.active_drivers: self._monkey_patch_speed(driver) return old_speed
def set_selenium_timeout(self, value): """Sets the timeout that is used by various keywords. The value can be given as a number that is considered to be seconds or as a human-readable string like ``1 second``. The previous value is returned and can be used to restore the original value later if needed. See the `Timeout` section above for more information. Example: | ${orig timeout} = | `Set Selenium Timeout` | 15 seconds | | `Open page that loads slowly` | | `Set Selenium Timeout` | ${orig timeout} | """ old_timeout = self.get_selenium_timeout() self.ctx.timeout = timestr_to_secs(value) for driver in self.drivers.active_drivers: driver.set_script_timeout(self.ctx.timeout) return old_timeout
def set_selenium_timeout(self, value): """Sets the timeout that is used by various keywords. The value can be given as a number that is considered to be seconds or as a human readable string like ``1 second``. The previous value is returned and can be used to restore the original value later if needed. See the `Timeout` section above for more information. Example: | ${orig timeout} = | `Set Selenium Timeout` | 15 seconds | | `Open page that loads slowly` | | `Set Selenium Timeout` | ${orig timeout} | """ old_timeout = self.get_selenium_timeout() self.ctx.timeout = timestr_to_secs(value) for driver in self.drivers.active_drivers: driver.set_script_timeout(self.ctx.timeout) return old_timeout
def set_selenium_timeout(self, seconds): """Sets the timeout in seconds used by various keywords. There are several `Wait ...` keywords that take timeout as an argument. All of these timeout arguments are optional. The timeout used by all of them can be set globally using this keyword. See `Timeouts` for more information about timeouts. The previous timeout value is returned by this keyword and can be used to set the old value back later. The default timeout is 5 seconds, but it can be altered in `importing`. Example: | ${orig timeout} = | Set Selenium Timeout | 15 seconds | | Open page that loads slowly | | Set Selenium Timeout | ${orig timeout} | """ old_timeout = self.get_selenium_timeout() self.ctx.timeout = timestr_to_secs(seconds) for browser in self.browsers.get_open_browsers(): browser.set_script_timeout(self.ctx.timeout) return old_timeout
def set_selenium_speed(self, seconds): """Sets the delay in seconds that is waited after each Selenium command. This is useful mainly in slowing down the test execution to be able to view the execution. `seconds` may be given in Robot Framework time format. Returns the previous speed value in seconds. One keyword may execute one or many Selenium commands and therefore one keyword may slow down more than the ``seconds`` argument defines. Example if delay is set to 1 second and because `Click Element` executes two Selenium commands, then the total delay will be 2 seconds. But because `Page Should Contain Element` executes only one selenium command, then the total delay will be 1 second. Example: | Set Selenium Speed | .5 seconds | """ old_speed = self.get_selenium_speed() self.ctx.speed = timestr_to_secs(seconds) for browser in self.browsers.browsers: self._monkey_patch_speed(browser) return old_speed
def set_selenium_implicit_wait(self, value): """Sets the implicit wait value used by Selenium. The value can be given as a number that is considered to be seconds or as a human-readable string like ``1 second``. The previous value is returned and can be used to restore the original value later if needed. This keyword sets the implicit wait for all opened browsers. Use `Set Browser Implicit Wait` to set it only to the current browser. See the `Implicit wait` section above for more information. Example: | ${orig wait} = | `Set Selenium Implicit Wait` | 10 seconds | | `Perform AJAX call that is slow` | | `Set Selenium Implicit Wait` | ${orig wait} | """ old_wait = self.get_selenium_implicit_wait() self.ctx.implicit_wait = timestr_to_secs(value) for driver in self.drivers.active_drivers: driver.implicitly_wait(self.ctx.implicit_wait) return old_wait
def set_selenium_implicit_wait(self, value): """Sets the implicit wait value used by Selenium. The value can be given as a number that is considered to be seconds or as a human readable string like ``1 second``. The previous value is returned and can be used to restore the original value later if needed. This keyword sets the implicit wait for all opened browsers. Use `Set Browser Implicit Wait` to set it only to the current browser. See the `Implicit wait` section above for more information. Example: | ${orig wait} = | `Set Selenium Implicit Wait` | 10 seconds | | `Perform AJAX call that is slow` | | `Set Selenium Implicit Wait` | ${orig wait} | """ old_wait = self.get_selenium_implicit_wait() self.ctx.implicit_wait = timestr_to_secs(value) for driver in self.drivers.active_drivers: driver.implicitly_wait(self.ctx.implicit_wait) return old_wait
def select_window(self, locator='MAIN', timeout=None): """Selects browser window matching ``locator``. If the window is found, all subsequent commands use the selected window, until this keyword is used again. If the window is not found, this keyword fails. The previous window handle is returned, and can be used to return back to it later. Notice that in this context _window_ means a pop-up window opened when doing something on an existing window. It is not possible to select windows opened with `Open Browser`, `Switch Browser` must be used instead. Notice also that alerts should be handled with `Handle Alert` or other alert related keywords. The ``locator`` can be specified using different strategies somewhat similarly as when `locating elements` on pages. - By default the ``locator`` is matched against window handle, name, title, and URL. Matching is done in that order and the the first matching window is selected. - The ``locator`` can specify an explicit strategy by using format ``strategy:value`` (recommended) or ``strategy=value``. Supported strategies are ``name``, ``title`` and ``url``, which match windows using name, title, and URL, respectively. Additionally, ``default`` can be used to explicitly use the default strategy explained above. - If the ``locator`` is ``NEW`` (case-insensitive), the latest opened window is selected. It is an error if this is the same as the current window. - If the ``locator`` is ``MAIN`` (default, case-insensitive), the main window is selected. - If the ``locator`` is ``CURRENT`` (case-insensitive), nothing is done. This effectively just returns the current window handle. - If the ``locator`` is not a string, it is expected to be a list of window handles _to exclude_. Such a list of excluded windows can be get from `Get Window Handles` prior to doing an action that opens a new window. The ``timeout`` is used to specify how long keyword will poll to select the new window. The ``timeout`` is new in SeleniumLibrary 3.2. Example: | `Click Link` | popup1 | | # Open new window | | `Select Window` | example | | # Select window using default strategy | | `Title Should Be` | Pop-up 1 | | | `Click Button` | popup2 | | # Open another window | | ${handle} = | `Select Window` | NEW | # Select latest opened window | | `Title Should Be` | Pop-up 2 | | | `Select Window` | ${handle} | | # Select window using handle | | `Title Should Be` | Pop-up 1 | | | `Select Window` | MAIN | | # Select the main window | | `Title Should Be` | Main | | | ${excludes} = | `Get Window Handles` | | # Get list of current windows | | `Click Link` | popup3 | | # Open one more window | | `Select Window` | ${excludes} | | # Select window using excludes | | `Title Should Be` | Pop-up 3 | | *NOTE:* - The ``strategy:value`` syntax is only supported by SeleniumLibrary 3.0 and newer. - Prior to SeleniumLibrary 3.0 matching windows by name, title and URL was case-insensitive. - Earlier versions supported aliases ``None``, ``null`` and the empty string for selecting the main window, and alias ``self`` for selecting the current window. Support for these aliases were removed in SeleniumLibrary 3.2. """ epoch = time.time() timeout = epoch if is_falsy(timeout) else timestr_to_secs(timeout) + epoch try: return self.driver.current_window_handle except NoSuchWindowException: pass finally: self._window_manager.select(locator, timeout)
def get_timeout(self, timeout=None): if is_noney(timeout): return self.ctx.timeout return timestr_to_secs(timeout)
def switch_window( self, locator: Union[list, str] = "MAIN", timeout: Optional[str] = None, browser: str = "CURRENT", ): """Switches to browser window matching ``locator``. If the window is found, all subsequent commands use the selected window, until this keyword is used again. If the window is not found, this keyword fails. The previous windows handle is returned and can be used to switch back to it later. Notice that alerts should be handled with `Handle Alert` or other alert related keywords. The ``locator`` can be specified using different strategies somewhat similarly as when `locating elements` on pages. - By default, the ``locator`` is matched against window handle, name, title, and URL. Matching is done in that order and the first matching window is selected. - The ``locator`` can specify an explicit strategy by using the format ``strategy:value`` (recommended) or ``strategy=value``. Supported strategies are ``name``, ``title``, and ``url``. These matches windows using their name, title, or URL, respectively. Additionally, ``default`` can be used to explicitly use the default strategy explained above. - If the ``locator`` is ``NEW`` (case-insensitive), the latest opened window is selected. It is an error if this is the same as the current window. - If the ``locator`` is ``MAIN`` (default, case-insensitive), the main window is selected. - If the ``locator`` is ``CURRENT`` (case-insensitive), nothing is done. This effectively just returns the current window handle. - If the ``locator`` is not a string, it is expected to be a list of window handles _to exclude_. Such a list of excluded windows can be got from `Get Window Handles` before doing an action that opens a new window. The ``timeout`` is used to specify how long keyword will poll to select the new window. The ``timeout`` is new in SeleniumLibrary 3.2. Example: | `Click Link` | popup1 | | # Open new window | | `Switch Window` | example | | # Select window using default strategy | | `Title Should Be` | Pop-up 1 | | | `Click Button` | popup2 | | # Open another window | | ${handle} = | `Switch Window` | NEW | # Select latest opened window | | `Title Should Be` | Pop-up 2 | | | `Switch Window` | ${handle} | | # Select window using handle | | `Title Should Be` | Pop-up 1 | | | `Switch Window` | MAIN | | # Select the main window | | `Title Should Be` | Main | | | ${excludes} = | `Get Window Handles` | | # Get list of current windows | | `Click Link` | popup3 | | # Open one more window | | `Switch Window` | ${excludes} | | # Select window using excludes | | `Title Should Be` | Pop-up 3 | | The ``browser`` argument allows with ``index_or_alias`` to implicitly switch to a specific browser when switching to a window. See `Switch Browser` - If the ``browser`` is ``CURRENT`` (case-insensitive), no other browser is selected. *NOTE:* - The ``strategy:value`` syntax is only supported by SeleniumLibrary 3.0 and newer. - Prior to SeleniumLibrary 3.0 matching windows by name, title and URL was case-insensitive. - Earlier versions supported aliases ``None``, ``null`` and the empty string for selecting the main window, and alias ``self`` for selecting the current window. Support for these aliases was removed in SeleniumLibrary 3.2. """ epoch = time.time() timeout = epoch if is_falsy( timeout) else timestr_to_secs(timeout) + epoch try: return self.driver.current_window_handle except NoSuchWindowException: pass finally: if not is_string(browser) or not browser.upper() == "CURRENT": self.drivers.switch(browser) self._window_manager.select(locator, timeout)
def get_timeout(self, timeout: Optional[str] = None) -> float: if is_noney(timeout): return self.ctx.timeout return timestr_to_secs(timeout)
def my_open_browser(self, url, browser='firefox', alias=None, remote_url='http://127.0.0.1:4444', downloaddir='X:/TEMP', ff_profile_dir='F:/workspace/selenium-profile', paramsfile='params.json'): """Opens a new browser instance to the given ``url``. The ``browser`` argument specifies which browser to use, and the supported browser are listed in the table below. The browser names are case-insensitive and some browsers have multiple supported names. | = Browser = | = Name(s) = | | Firefox | firefox, ff | | Google Chrome | googlechrome, chrome, gc | To be able to actually use one of these browsers, you need to have a matching Selenium browser driver available. See the [https://github.com/robotframework/SeleniumLibrary#browser-drivers| project documentation] for more details. Headless Firefox and Headless Chrome are new additions in SeleniumLibrary 3.1.0 and require Selenium 3.8.0 or newer. Optional ``alias`` is an alias given for this browser instance and it can be used for switching between browsers. An alternative approach for switching is using an index returned by this keyword. These indices start from 1, are incremented when new browsers are opened, and reset back to 1 when `Close All Browsers` is called. See `Switch Browser` for more information and examples. Optional ``remote_url`` is the URL for a [https://github.com/SeleniumHQ/selenium/wiki/Grid2|Selenium Grid]. Optional ``desired_capabilities`` can be used to configure, for example, logging preferences for a browser or a browser and operating system when using [http://saucelabs.com|Sauce Labs]. Desired capabilities can be given either as a Python dictionary or as a string in format ``key1:value1,key2:value2``. [https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities| Selenium documentation] lists possible capabilities that can be enabled. Optional ``ff_profile_dir`` is the path to the Firefox profile directory if you wish to overwrite the default profile Selenium uses. Notice that prior to SeleniumLibrary 3.0, the library contained its own profile that was used by default. Examples: | `My Open Browser` | http://example.com | Chrome | remote_url=http://127.0.0.1:9515 | downloaddir=X:${/}TEMP | paramsfile=params.json | `My Open Browser` | http://example.com | Firefox | remote_url=http://127.0.0.1:4444 | alias=Firefox | If the provided configuration options are not enough, it is possible to use `Create Webdriver` to customize browser initialization even more. Applying ``desired_capabilities`` argument also for local browser is new in SeleniumLibrary 3.1. """ logger.info( "browser[%s], url[%s], alias[%s], remote_url[%s], downloaddir[%s], ff_profile[%s], paramsfile[%s]" % (browser, url, alias, remote_url, downloaddir, ff_profile_dir, paramsfile)) if (browser.lower() == 'firefox'): driver = self._start_firefox(remote_url, downloaddir, ff_profile_dir) elif (browser.lower() == 'chrome'): driver = self._start_chrome(remote_url, downloaddir) else: raise NameError("browser name[%s] is not in [chrome, firefox]" % (browser)) logger.info("ctx.timeout[%s], ctx.implicit_wait[%s]" % (self.ctx.timeout, self.ctx.implicit_wait)) driver.set_script_timeout(timestr_to_secs(self.ctx.timeout)) driver.implicitly_wait(timestr_to_secs(self.ctx.implicit_wait)) if self.ctx.speed: self._monkey_patch_speed(driver) try: driver.get(url) except Exception: self.ctx.register_driver(driver, alias) self.debug("Opened browser with session id %s but failed " "to open url '%s'." % (driver.session_id, url)) raise self.debug('Opened browser with session id %s.' % driver.session_id) self._save_browser_params(driver, paramsfile) return self.ctx.register_driver(driver, alias)
def _format_timeout(self, timeout): timeout = timestr_to_secs(timeout) if is_truthy( timeout) else self.ctx.timeout return secs_to_timestr(timeout)
def select_window(self, locator='MAIN', timeout=None): """Selects browser window matching ``locator``. If the window is found, all subsequent commands use the selected window, until this keyword is used again. If the window is not found, this keyword fails. The previous window handle is returned, and can be used to return back to it later. Notice that in this context _window_ means a pop-up window opened when doing something on an existing window. It is not possible to select windows opened with `Open Browser`, `Switch Browser` must be used instead. Notice also that alerts should be handled with `Handle Alert` or other alert related keywords. The ``locator`` can be specified using different strategies somewhat similarly as when `locating elements` on pages. - By default the ``locator`` is matched against window handle, name, title, and URL. Matching is done in that order and the the first matching window is selected. - The ``locator`` can specify an explicit strategy by using format ``strategy:value`` (recommended) or ``strategy=value``. Supported strategies are ``name``, ``title`` and ``url``, which match windows using name, title, and URL, respectively. Additionally, ``default`` can be used to explicitly use the default strategy explained above. - If the ``locator`` is ``NEW`` (case-insensitive), the latest opened window is selected. It is an error if this is the same as the current window. - If the ``locator`` is ``MAIN`` (default, case-insensitive), the main window is selected. - If the ``locator`` is ``CURRENT`` (case-insensitive), nothing is done. This effectively just returns the current window handle. - If the ``locator`` is not a string, it is expected to be a list of window handles _to exclude_. Such a list of excluded windows can be get from `Get Window Handles` prior to doing an action that opens a new window. The ``timeout`` is used to specify how long keyword will poll to select the new window. The ``timeout`` is new in SeleniumLibrary 3.2. Example: | `Click Link` | popup1 | | # Open new window | | `Select Window` | example | | # Select window using default strategy | | `Title Should Be` | Pop-up 1 | | | `Click Button` | popup2 | | # Open another window | | ${handle} = | `Select Window` | NEW | # Select latest opened window | | `Title Should Be` | Pop-up 2 | | | `Select Window` | ${handle} | | # Select window using handle | | `Title Should Be` | Pop-up 1 | | | `Select Window` | MAIN | | # Select the main window | | `Title Should Be` | Main | | | ${excludes} = | `Get Window Handles` | | # Get list of current windows | | `Click Link` | popup3 | | # Open one more window | | `Select Window` | ${excludes} | | # Select window using excludes | | `Title Should Be` | Pop-up 3 | | *NOTE:* - The ``strategy:value`` syntax is only supported by SeleniumLibrary 3.0 and newer. - Earlier versions supported aliases ``None``, ``null`` and the empty string for selecting the main window, and alias ``self`` for selecting the current window. These aliases were deprecated in SeleniumLibrary 3.0. - Prior to SeleniumLibrary 3.0 matching windows by name, title and URL was case-insensitive. """ epoch = time.time() timeout = epoch if is_falsy( timeout) else timestr_to_secs(timeout) + epoch try: return self.driver.current_window_handle except NoSuchWindowException: pass finally: self._window_manager.select(locator, timeout)
def __init__(self, timeout=5.0, implicit_wait=0.0, run_on_failure='Capture Page Screenshot', screenshot_root_directory=None, plugins=None): """SeleniumLibrary can be imported with several optional arguments. - ``timeout``: Default value for `timeouts` used with ``Wait ...`` keywords. - ``implicit_wait``: Default value for `implicit wait` used when locating elements. - ``run_on_failure``: Default action for the `run-on-failure functionality`. - ``screenshot_root_directory``: Location where possible screenshots are created. If not given, the directory where the log file is written is used. - ``plugins``: Allows extending the SeleniumLibrary with external Python classes. """ self.timeout = timestr_to_secs(timeout) self.implicit_wait = timestr_to_secs(implicit_wait) self.speed = 0.0 self.run_on_failure_keyword \ = RunOnFailureKeywords.resolve_keyword(run_on_failure) self._running_on_failure_keyword = False self.screenshot_root_directory = screenshot_root_directory self._element_finder = ElementFinder(self) self._plugin_keywords = [] libraries = [ AlertKeywords(self), BrowserManagementKeywords(self), CookieKeywords(self), ElementKeywords(self), FormElementKeywords(self), FrameKeywords(self), JavaScriptKeywords(self), RunOnFailureKeywords(self), ScreenshotKeywords(self), SelectElementKeywords(self), TableElementKeywords(self), WaitingKeywords(self), WindowKeywords(self) ] if is_truthy(plugins): parsed_plugins = self._string_to_modules(plugins) for index, plugin in enumerate( self._import_modules(parsed_plugins)): if not isclass(plugin): message = "Importing test library: '%s' failed." % parsed_plugins[ index].plugin raise DataError(message) plugin = plugin(self, *parsed_plugins[index].args, **parsed_plugins[index].kw_args) if not isinstance(plugin, LibraryComponent): message = 'Plugin does not inherit SeleniumLibrary.base.LibraryComponent' raise PluginError(message) self._store_plugin_keywords(plugin) libraries.append(plugin) self._drivers = WebDriverCache() DynamicCore.__init__(self, libraries) self.ROBOT_LIBRARY_LISTENER = LibraryListener()