def _handle_deprecated_locators(self, locator): if not (is_string(locator) or locator is None): return locator normalized = locator.lower() if is_string(locator) else locator if normalized in self._deprecated_locators: new = self._deprecated_locators[normalized] logger.warn("Using '%s' as window locator is deprecated. " "Use '%s' instead." % (locator, new)) return new return locator
def get_window_handles(self, browser): if is_string(browser) and browser == "ALL": handles = [] current_index = self.drivers.current_index for index, driver in enumerate(self.drivers, 1): self.drivers.switch(index) handles.extend(self.driver.window_handles) self.drivers.switch(current_index) return handles elif is_string(browser) and browser == "CURRENT": return self.driver.window_handles else: current_index = self.drivers.current_index self.drivers.switch(browser) handles = self.driver.window_handles self.drivers.switch(current_index) return handles
def get_window_infos(self, browser="CURRENT"): try: current_index = self.drivers.current_index except AttributeError: current_index = None if is_string(browser) and browser.upper() == "ALL": infos = [] for index, driver in enumerate(self.drivers, 1): self.drivers.switch(index) infos.extend(self._get_window_infos()) self.drivers.switch(current_index) return infos elif is_string(browser) and browser.upper() == "CURRENT": return self._get_window_infos() else: self.drivers.switch(browser) infos = self._get_window_infos() self.drivers.switch(current_index) return infos
def _select(self, locator): if not is_string(locator): self._select_by_excludes(locator) elif locator.upper() == 'CURRENT': pass elif locator.upper() == 'MAIN': self._select_main_window() elif locator.upper() == 'NEW': self._select_by_last_index() else: strategy, locator = self._parse_locator(locator) self._strategies[strategy](locator)
def _select(self, locator): if not is_string(locator): self._select_by_excludes(locator) elif locator.upper() == "CURRENT": pass elif locator.upper() == "MAIN": self._select_main_window() elif locator.upper() == "NEW": self._select_by_last_index() else: strategy, locator = self._parse_locator(locator) self._strategies[strategy](locator)
def select(self, locator): locator = self._handle_deprecated_locators(locator) if not is_string(locator): self._select_by_excludes(self.browser, locator) elif locator.upper() == 'CURRENT': pass elif locator.upper() == 'MAIN': self._select_main_window(self.browser) elif locator.upper() == 'NEW': self._select_by_last_index(self.browser) else: strategy, locator = self._parse_locator(locator) self._strategies[strategy](self.browser, locator)
def create(self, browser, options): if is_falsy(options): return None selenium_options = self._import_options(browser) if not is_string(options): return options options = self._parse(options) selenium_options = selenium_options() for option in options: for key in option: attr = getattr(selenium_options, key) if callable(attr): attr(*option[key]) else: setattr(selenium_options, key, *option[key]) return selenium_options
def switch_window(self, locator="MAIN", timeout=None, browser="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 resolve_keyword(name): if is_noney(name) or is_string(name) and name.upper() == "NOTHING": return None return name
def test_is_string(self): strings = ['1', 'foo', ' ', u'', ''] for item in strings: self.assertTrue(is_string(item))
def test_is_not_string(self): strings = [1, 2.345, None, False] for item in strings: self.assertFalse(is_string(item))
def resolve_keyword(name): if is_falsy(name) or is_string(name) and name.upper() == 'NOTHING': return None return name