def switch_to_window(self, partial_url='', partial_title=''): """切换窗口 如果窗口数<3,不需要传入参数,切换到当前窗口外的窗口; 如果窗口数>=3,则需要传入参数来确定要跳转到哪个窗口 """ all_windows = self.driver.window_handles if len(all_windows) == 1: logger.warning('only one window!') elif len(all_windows) == 2: other_window = all_windows[1 - all_windows.index(self.current_window)] self.driver.switch_to.window(other_window) else: for window in all_windows: self.driver.switch_to.window(window) if partial_url in self.driver.current_url or partial_title in self.driver.title: break logger.debug(self.driver.current_url, self.driver.title)
def get_current_url(self) -> str: debug("Get current browser") return self.driver.current_url
def enter_url(self, url: str): debug(f"Entering {url}") self.driver.get(url)
def close(self): debug("Close browser") self.driver.quit()
def maximize(self): debug("Maximize browser window") self.driver.maximize_window()
def __init__(self, by: By, loc: str, name: str): self.__by = by self.__loc = loc self.__name = f"{type(self).__name__}, '{name}'" debug(f"Creating instance of {self.__name}")
def login_sucess(self, username, password): logger.debug("login sucess") self.prepare_login(username, password) self.url_should_change(self.url)
def login_incorrect_usr_or_pw(self, username, password): logger.debug("login_incorrect_usr_or_pw") self.prepare_login(username, password) self.check_text_to_be_present_in_element(self.loc_error, "帐号或密码错误")
def login_without_pw(self, username, password): """演示用,实际编程时自动化用例优先覆盖与后台存在数据交互的功能,像这种单纯的前端检测,手工即可""" logger.debug("login_without_pw") self.prepare_login(username, password) self.check_text_to_be_present_in_element(self.loc_error, "请输入密码")