def initial_element(self): try: self.is_loaded() self.page_factory() except NoSuchWindowException(): BasePage.screen_shot(self) assert False pass
def initial_element(self): try: self.is_loaded() self.page_factory() except NoSuchWindowException(): initial_element_error_wrapper(self.driver) raise pass
def switch_to_window(handle_or_name): if handle_or_name == '': browser.current_window = first_window return for window in windows: if window.handle == handle_or_name or window.name == handle_or_name: browser.current_window = window return raise NoSuchWindowException(u'Unable to locate window "' + handle_or_name + '"')
def openUrlNewTab(self, url, close_secondary_tabs=True, title_display=""): self.switchToMainTab(close_secondary_tabs) self.driver.execute_script("window.open()") if len(self.driver.window_handles) > 1: self.driver.switch_to.window(self.driver.window_handles[-1]) return self.openUrl(url, title_display=title_display) else: raise NoSuchWindowException( "Cannot switch to New Tab: target window already closed.")
def get_window_title(self): try: return self.driver.title except NoSuchWindowException as e: logger.error("Unable to switch to window as {}".format(e)) raise NoSuchWindowException( "Unable to switch to window as {}".format(e)) except Exception as e: logger.error("Unable to switch to window as {}".format(e)) raise Exception(e)
def switch_to_window_regex(self, regex): """Switch to a window with a url or window title that is matched by regex""" pat = re.compile(regex) for handle in self._driver.window_handles: self._driver.switch_to.window(handle) if pat.match(self._driver.title) or pat.match( self._driver.current_url): return self raise NoSuchWindowException( "Could not switch to window with title/url '{0}'".format(regex))
def switch_window_by_handles(self, handles): try: self.driver.switch_to.window(handles) logger.info('Switched to window') except NoSuchWindowException as e: logger.error("Unable to switch to window as {}".format(e)) raise NoSuchWindowException( "Unable to switch to window as {}".format(e)) except Exception as e: logger.error("Unable to switch to window as {}".format(e)) raise Exception(e)
def _switch_by_win_ser(self, window_name): if (window_name == 'win_ser_local'): wnd_handlers = self.driver.window_handles if (len(wnd_handlers) > 0): self.driver.switch_to.window(wnd_handlers[0]) else: raise NoSuchWindowException(('Invalid Window ID: %s' % window_name)) else: if (window_name not in self.windows): self.windows[window_name] = self.driver.window_handles[(-1)] self.driver.switch_to.window(self.windows[window_name])
def _switch_by_win_ser(self, window_name): if window_name == "win_ser_local": wnd_handlers = self.driver.window_handles if len(wnd_handlers) > 0: self.driver.switch_to.window(wnd_handlers[0]) else: raise NoSuchWindowException("Invalid Window ID: %s" % window_name) else: if window_name not in self.windows: self.windows[window_name] = self.driver.window_handles[-1] self.driver.switch_to.window(self.windows[window_name])
def _select_last_index(self): try: current_handle = self.get_window_handle() except NoSuchWindowException: current_handle = None handles = self.driver.window_handles self.log.info('Switching to new window.') if handles[-1] == self.driver.current_window_handle: raise NoSuchWindowException('Window with last index is same as ' 'the current window.') self.driver.switch_to.window(handles[-1]) return current_handle
def _select_by_matcher(self, matcher, error): try: starting_handle = self.get_window_handle() except NoSuchWindowException: starting_handle = None for handle in self.driver.window_handles: self.driver.switch_to.window(handle) if matcher(self._get_current_window_info()): return starting_handle if starting_handle: self.driver.switch_to.window(starting_handle) raise NoSuchWindowException(error)
def switch(self, window_name=None): try: if (not window_name): self.driver.switch_to.window(self.driver.window_handles[(-1)]) elif window_name.isdigit(): self._switch_by_idx(int(window_name)) elif window_name.startswith('win_ser_'): self._switch_by_win_ser(window_name) else: self.driver.switch_to.window(window_name) except NoSuchWindowException: raise NoSuchWindowException(('Invalid Window ID: %s' % window_name))
def initial_element(self): try: self.is_loaded() self.page_factory() except NoSuchWindowException(): print(u'控件不在当前页面上.') # screenshot_file = GlobalVar.get_case_name() + "_" + str(time.time()) + "_screenshot.png" # self.driver.save_screenshot(GlobalVar.get_screenshot_path() + screenshot_file) print("错误截图:") # print('<img src="' + GlobalVar.get_screenshot_path() + screenshot_file + '" width="800px" />') # run_info_log(u'控件不在当前页面上.', GlobalVar.get_log_file()) raise pass
def _select_excluded(self, excludes): try: current_handle = self.get_window_handle() except NoSuchWindowException: current_handle = None self.log.info( 'Switching to a window excluded from {}.'.format(excludes)) for handle in self.driver.window_handles: if handle not in excludes: self.driver.switch_to.window(handle) return current_handle raise NoSuchWindowException( 'No window not matching excludes %s found.' % excludes)
def switch_to_child_window(self): try: parent_window = self.driver.current_window_handle handles = self.driver.window_handles self.driver.switch_to.window(handles[len(handles) - 1]) logger.info('Switched to child window') return parent_window except NoSuchWindowException as e: logger.error("Unable to switch to window as {}".format(e)) raise NoSuchWindowException( "Unable to switch to window as {}".format(e)) except Exception as e: logger.error("Unable to switch to window as {}".format(e)) raise Exception(e)
def close_window_regex(self, regex): """Close a window with a url or window title that is matched by regex""" pat = re.compile(regex) for handle in self._driver.window_handles: self._driver.switch_to.window(handle) if pat.match(self._driver.title) or pat.match( self._driver.current_url): self._driver.close() if len(self._driver.window_handles) == 1: self._driver.switch_to.window( self._driver.window_handles[0]) return self raise NoSuchWindowException( "Could not close window with title/url '{0}'".format(regex))
def switch(self, window_name=None): try: if not window_name: # Switch to last window created self.driver.switch_to.window(self.driver.window_handles[-1]) else: if window_name.isdigit(): # Switch to window handler index self._switch_by_idx(int(window_name)) else: if window_name.startswith("win_ser_"): # Switch using window sequential mode self._switch_by_win_ser(window_name) else: # Switch using window name self.driver.switch_to.window(window_name) except NoSuchWindowException: raise NoSuchWindowException("Invalid Window ID: %s" % window_name)
def switch_window_by_title(self, window_title): try: handles = self.driver.window_handles for i in handles: self.driver.switch_to.window(i) if self.get_window_title() == window_title: logger.info('Switched to {} window'.format(window_title)) return True except NoSuchWindowException as e: logger.error("Unable to switch to window as {}".format(e)) raise NoSuchWindowException( "Unable to switch to window as {}".format(e)) except Exception as e: logger.error("Unable to switch to window as {}".format(e)) raise Exception(e)
def _switch_by_win_ser(window_name): driver = _get_driver() if window_name == "win_ser_local": wnd_handlers = driver.window_handles if len(wnd_handlers) > 0: driver.switch_to.window(wnd_handlers[0]) else: raise NoSuchWindowException("Invalid Window ID: %s" % window_name) elif window_name.split("win_ser_")[1].isdigit(): # e.g. win_ser_1 _switch_by_idx(int(window_name.split("win_ser_")[1])) else: windows = get_from_thread_store("windows") if window_name not in windows: windows[window_name] = driver.current_window_handle driver.switch_to.window(windows[window_name])
def switch_window(window_name=None): driver = _get_driver() try: if window_name is None: # Switch to last window created driver.switch_to.window(driver.window_handles[-1]) else: if isinstance(window_name, int) or window_name.isdigit(): # Switch to window handler index _switch_by_idx(int(window_name)) else: if window_name.startswith("win_ser_"): # Switch using window sequential mode _switch_by_win_ser(window_name) else: # Switch using window name driver.switch_to.window(window_name) except NoSuchWindowException: raise NoSuchWindowException("Invalid Window ID: %s" % window_name)
def open_url(url): ''' open url :param url: :return: ''' try: Var.instance.get(url) except NoSuchWindowException: handles = DriverBase.get_window_handles() # 如果浏览器未关闭打开新窗口触发该异常,提示用户切换窗口 if handles: raise NoSuchWindowException( 'no such window, execute the switchToWindow method to switch the window' ) DriverBase.createSession() Var.instance.get(url) except InvalidSessionIdException: DriverBase.createSession() Var.instance.get(url)
def _select_by_default(self, criteria): try: starting_handle = self.get_window_handle() except NoSuchWindowException: starting_handle = None for handle in self.driver.window_handles: # iterate through each window self.driver.switch_to.window(handle) for item in self._get_current_window_info(): # Check for match in the following order: # 1- handle ID (set by selenium) # 2- window id (can be set by user with JavaScript) # 3- window name (can be set by user with JavaScript) # 4- page title # 5- page url if item == criteria: # return on first match return starting_handle if starting_handle: # go back to starting window in case of failure and raise exception self.driver.switch_to.window(starting_handle) raise NoSuchWindowException( "No window matching handle, id, name, title " "or URL '%s' found." % criteria)
def setUp(self): host = get_config("selenium", "host") port = get_config("selenium", "port") browser = get_config("selenium", "browser").lower() if browser == "firefox": # Set profile of Firefox profile = webdriver.FirefoxProfile() profile.set_preference( "intl.accept_languages", "en-us, en, en-us, en" ) profile.set_preference( "extensions.addonnotification.showDaytip", "false" ) # Turn off daily tips of Firefox profile.set_preference( "*****@*****.**", "0" ) # Turn off the tip of Firefox passport self.driver = webdriver.Firefox(profile) elif browser == "ie": self.driver = webdriver.Ie() elif browser == "chrome": self.driver = webdriver.Chrome() else: print_log("The browser is not supported! " "Please check your AFT.ini!", "error") raise NoSuchWindowException("The browser you set is not supported, " "please check your config file.") self.driver.maximize_window() self.driver.implicitly_wait( get_config("selenium", "implicitly_wait_time") ) if port: self.driver.get(host + ":" + port) else: print_log("No port is specified.", "debug") self.driver.get(host)
def _switch_by_idx(self, win_index): wnd_handlers = self.driver.window_handles if ((len(wnd_handlers) <= win_index) and (win_index >= 0)): self.driver.switch_to.window(wnd_handlers[win_index]) else: raise NoSuchWindowException(('Invalid Window ID: %s' % str(win_index)))
def switch_new_window(self, logged_page_name=""): Logger.info("Switch to new window %s" % logged_page_name) handles = self.get_driver().window_handles if len(handles) <= 1: raise NoSuchWindowException("There is no window. Count windows is %s" % len(handles)) self.get_driver().switch_to_window(handles[-1])
def delete_all_cookies(): driver = browser.get_current_browser() if driver is None: raise NoSuchWindowException("Can't delete cookies, no open browser") driver.delete_all_cookies()
def get_cookies(): driver = browser.get_current_browser() if driver is None: raise NoSuchWindowException("Can't list cookies, no open browser") return driver.get_cookies()
def step_impl(context, page): if context.browser.title != page: raise NoSuchWindowException("Страница не открыта")