class LoginOdoo(BasePage): # 配置文件读取元素 do_conf = ParseConFile() # 用户名 http://ac-sit.bpit.aqara.com/web/login username = do_conf.get_locators_or_account('OdooLoginPageElements', 'username') #密码 password = do_conf.get_locators_or_account('OdooLoginPageElements', 'password') #登录按钮 loginBtn = do_conf.get_locators_or_account('OdooLoginPageElements', 'loginBtn') def login(self, username, password): """登录流程""" self.open_url() self.input_username(username) self.input_password(password) self.click_login_btn() def open_url(self): return self.load_url('http://mrp-sit.bpit.aqara.com/') def input_username(self, username): return self.send_keys(*LoginOdoo.username, username) def input_password(self, password): return self.send_keys(*LoginOdoo.password, password) def click_login_btn(self): return self.click(*LoginOdoo.loginBtn)
class ContactPage(BasePage): # 配置文件读取元素 do_conf = ParseConFile() # 新键联系人按钮 new_contact_btn = do_conf.get_locators_or_account('ContactPageElements', 'new_contact') # 姓名输入框 name = do_conf.get_locators_or_account('ContactPageElements', 'name') # 电子邮箱输入框 mail = do_conf.get_locators_or_account('ContactPageElements', 'mail') # 标记为星级 star = do_conf.get_locators_or_account('ContactPageElements', 'star') # 电话号码输入框 phone = do_conf.get_locators_or_account('ContactPageElements', 'phone') # 备注输入框 comment = do_conf.get_locators_or_account('ContactPageElements', 'comment') # 确定按钮 commit = do_conf.get_locators_or_account('ContactPageElements', 'commit') # 添加失败的提示信息 error_tip = do_conf.get_locators_or_account('ContactPageElements', 'tooltip') def add_contact(self, name, mail, star, phone, comment): """添加联系人""" self.click_new_contact_btn() self.input_name(name) self.input_mail(mail) if star == '1': self.select_str() self.input_phone(phone) self.input_comment(comment) self.click_commit_btn() def click_new_contact_btn(self): return self.click(*ContactPage.new_contact_btn) def input_name(self, name): return self.send_keys(*ContactPage.name, name) def input_mail(self, mail): return self.send_keys(*ContactPage.mail, mail) def select_str(self): return self.click(*ContactPage.star) def input_phone(self, phone): return self.send_keys(*ContactPage.phone, phone) def input_comment(self, comment): return self.send_keys(*ContactPage.comment, comment) def click_commit_btn(self): return self.click(*ContactPage.commit) def get_error_text(self): return self.get_element_text(*ContactPage.error_tip)
class QualityEvaluatePage(BasePage): do_conf = ParseConFile() new_project = do_conf.get_locators_or_account( 'PersonalQualityPageElements', 'new_project') extract_btn = do_conf.get_locators_or_account( 'PersonalQualityPageElements', 'extract_btn') save_btn = do_conf.get_locators_or_account('PersonalQualityPageElements', 'save_btn') generate_report = do_conf.get_locators_or_account( 'PersonalQualityPageElements', 'generate_report') project_names = do_conf.get_locators_or_account( 'PersonalQualityPageElements', 'project_names') savepro_name = do_conf.get_locators_or_account( 'PersonalQualityPageElements', 'savepro_name') savepro_confirm = do_conf.get_locators_or_account( 'PersonalQualityPageElements', 'savepro_confirm') view_report = do_conf.get_locators_or_account( 'PersonalQualityPageElements', 'view_report') export_btn = do_conf.get_locators_or_account('PersonalQualityPageElements', 'export_btn') download_btn = do_conf.get_locators_or_account( 'PersonalQualityPageElements', 'download_btn') def create_pro(self, projectname): # 点击新建评价项目 self.click(*self.new_project) # 选择条件后进行抽取 self.click(*self.extract_btn) # 保存项目 self.click(*self.save_btn) # 生成报表 self.click(*self.generate_report) # 填写项目名称 self.send_keys(*self.savepro_name, projectname) # 点击确定 self.click(*self.savepro_confirm) # 点击查看报表 self.click(*self.view_report) # 点击导出 self.click(*self.export_btn) # 点击下载 self.click(*self.download_btn) def get_projectnames(self): """获取列表所有项目名称,由于没有处理翻页,这里只统计了第一页的数据""" names = self.find_elements(*self.project_names) return [ele.text for ele in names] def project_result(self, projecetname): """判断评价项目是否创建成功""" if projecetname in self.get_projectnames(): return 'pass' else: return 'fail'
class TruckPage(BasePage): do_conf = ParseConFile() #导入按钮 uploadfile_btn = do_conf.get_locators_or_account('TruckPageElements', 'uploadfile') upload_tip = do_conf.get_locators_or_account('TruckPageElements', 'uploadmsg') def uploadfile(self): #导入文件 return self.send_keys(DATA_UploadFile, *TruckPage.uploadfile_btn) def get_uploadmsg(self): return self.get_element_text(*TruckPage.upload_tip)
class HomePage(BasePage): # 配置文件读取元素 do_conf = ParseConFile() # 首页 homePage = do_conf.get_locators_or_account('HomePageElements', 'homePage') # 通讯录 mailList = do_conf.get_locators_or_account('HomePageElements', 'mailList') # 应用中心 applicationCenter = do_conf.get_locators_or_account( 'HomePageElements', 'applicationCenter') # 收件箱 inBox = do_conf.get_locators_or_account('HomePageElements', 'inBox') def select_menu(self, menu='mailList'): if menu == "mailList": self.click_address_list_menu() elif menu == 'homePage': self.click_home_page_menu() elif menu == 'applicationCenter': self.click_application_center_menu() elif menu == 'inBox': self.click_in_box_menu() else: raise ValueError('''菜单选择错误! homePage->首页 mailList->通讯录 applicationCenter->应用中心 inBox->收件箱''') def click_home_page_menu(self): return self.click(*HomePage.homePage) def click_address_list_menu(self): return self.click(*HomePage.mailList) def click_application_center_menu(self): return self.click(*HomePage.applicationCenter) def click_in_box_menu(self): return self.click(*HomePage.inBox)
class PlanSettingPage(BasePage): do_conf = ParseConFile() addplan_btn = do_conf.get_locators_or_account('PlanSettingPageElements', 'addplan_btn') save_btn = do_conf.get_locators_or_account('PlanSettingPageElements', 'save_btn') planname = do_conf.get_locators_or_account('PlanSettingPageElements', 'planname') def add_plan(self, planname): """新增审方方案流程""" self.click_addplan_btn() self.input_planname(planname) self.click_save_btn() def input_planname(self, planname): return self.send_keys(*PlanSettingPage.planname, planname) def click_addplan_btn(self): return self.click(*PlanSettingPage.addplan_btn) def click_save_btn(self): return self.click(*PlanSettingPage.save_btn)
class AuthSettingPage(BasePage): do_conf = ParseConFile() add_btn = do_conf.get_locators_or_account('AuthSettingPageElements', 'add_btn') pharname = do_conf.get_locators_or_account('AuthSettingPageElements', 'pharname') pharname_first = do_conf.get_locators_or_account('AuthSettingPageElements', 'pharname_first') edit = do_conf.get_locators_or_account('AuthSettingPageElements', 'edit') delete = do_conf.get_locators_or_account('AuthSettingPageElements', 'delete') def click_add_btn(self): return self.click(*self.add_btn) def click_pharname(self): return self.click(*self.pharname) def click_pharname_first(self): return self.click(*self.pharname_first) def click_delete(self): return self.click(*self.delete)
class BasePage(object): """结合显示等待封装一些selenium内置方法""" cf = ParseConFile() excel = ParseExcel() def __init__(self, driver, timeout=30): self.byDic = { 'id': By.ID, 'name': By.NAME, 'class_name': By.CLASS_NAME, 'xpath': By.XPATH, 'link_text': By.LINK_TEXT, 'css selector': By.CSS_SELECTOR } self.driver = driver self.outTime = timeout def find_element(self, by, locator): """ find alone element :param by: eg: id, name, xpath, css..... :param locator: id, name, xpath for str :return: element object """ try: print('[Info:Starting find the element "{}" by "{}"!]'.format( locator, by)) element = WD( self.driver, self.outTime).until(lambda x: x.find_element(by, locator)) except TimeoutException as t: print('error: found "{}" timeout!'.format(locator), t) else: return element def find_elements(self, by, locator): """ find group elements :param by: eg: id, name, xpath, css..... :param locator: eg: id, name, xpath for str :return: elements object """ try: print('[Info:start find the elements "{}" by "{}"!]'.format( locator, by)) elements = WD( self.driver, self.outTime).until(lambda x: x.find_elements(by, locator)) except TimeoutException as t: print('error: found "{}" timeout!'.format(locator), t) else: return elements def is_element_exist(self, by, locator): """ assert element if exist :param by: eg: id, name, xpath, css..... :param locator: eg: id, name, xpath for str :return: if element return True else return false """ if by.lower() in self.byDic: try: WD(self.driver, self.outTime). \ until(ec.visibility_of_element_located((self.byDic[by], locator))) except TimeoutException: print('Error: element "{}" not exist'.format(locator)) return False return True else: print('the "{}" error!'.format(by)) def is_click(self, by, locator): if by.lower() in self.byDic: try: element = WD(self.driver, self.outTime). \ until(ec.element_to_be_clickable((self.byDic[by], locator))) except TimeoutException: print("元素不可以点击") else: return element else: print('the "{}" error!'.format(by)) def is_alert(self): """ assert alert if exsit :return: alert obj """ try: re = WD(self.driver, self.outTime).until(ec.alert_is_present()) except (TimeoutException, NoAlertPresentException): print("error:no found alert") else: return re def switch_to_frame(self, by, locator): """判断frame是否存在,存在就跳到frame""" print('info:switching to iframe "{}"'.format(locator)) if by.lower() in self.byDic: try: WD(self.driver, self.outTime). \ until(ec.frame_to_be_available_and_switch_to_it((self.byDic[by], locator))) except TimeoutException as t: print('error: found "{}" timeout!切换frame失败'.format(locator), t) else: print('the "{}" error!'.format(by)) def switch_to_default_frame(self): """返回默认的frame""" print('info:switch back to default iframe') try: self.driver.switch_to.default_content() except Exception as e: print(e) def get_alert_text(self): """获取alert的提示信息""" alert = self.is_alert() if alert: return alert.text else: return None def get_element_text(self, by, locator, name=None): """获取某一个元素的text信息""" try: element = self.find_element(by, locator) if name: return element.get_attribute(name) else: return element.text except AttributeError: print('get "{}" text failed return None'.format(locator)) def load_url(self, url): """加载url""" print('info: string upload url "{}"'.format(url)) self.driver.get(url) def get_source(self): """获取页面源码""" return self.driver.page_source def send_keys(self, by, locator, value=''): """写数据""" print('info:input "{}"'.format(value)) try: element = self.find_element(by, locator) element.send_keys(value) except AttributeError as e: print(e) def clear(self, by, locator): """清理数据""" print('info:clearing value') try: element = self.find_element(by, locator) element.clear() except AttributeError as e: print(e) def click(self, by, locator): """点击某个元素""" print('info:click "{}"'.format(locator)) element = self.is_click(by, locator) if element: element.click() else: print('the "{}" unclickable!') @staticmethod def sleep(num=0): """强制等待""" print('info:sleep "{}" minutes'.format(num)) time.sleep(num) def ctrl_v(self, value): """ctrl + V 粘贴""" print('info:pasting "{}"'.format(value)) ClipBoard.set_text(value) self.sleep(3) KeyBoard.two_keys('ctrl', 'v') @staticmethod def enter_key(): """enter 回车键""" print('info:keydown enter') KeyBoard.one_key('enter') def wait_element_to_be_located(self, by, locator): """显示等待某个元素出现,且可见""" print('info:waiting "{}" to be located'.format(locator)) try: return WD(self.driver, self.outTime).until( ec.presence_of_element_located((self.byDic[by], locator))) except TimeoutException as t: print('error: found "{}" timeout!'.format(locator), t) def get_page_source(self): return self.get_source() def is_true_url(self, route): """判断跳转后的页面是否是预期的页面""" now_url = self.driver.current_url if route in now_url: print("跳转后页面正确") else: print("跳转后页面错误")
class LoginPage(BasePage): # 配置文件读取元素 do_conf = ParseConFile() # 选择密码登录的按钮 password_login_btn = do_conf.get_locators_or_account( 'LoginPageElements', 'password_login_btn') # 登录框外的iframe frame = do_conf.get_locators_or_account('LoginPageElements', 'frame') # 用户名输入框 username = do_conf.get_locators_or_account('LoginPageElements', 'username') # 密码输入框 password = do_conf.get_locators_or_account('LoginPageElements', 'password') # 登录按钮 loginBtn = do_conf.get_locators_or_account('LoginPageElements', 'loginBtn') # 登录失败的提示信息 # error_head = do_conf.get_locators_or_account('LoginPageElements', 'errorHead') userError_low = do_conf.get_locators_or_account('LoginPageElements', 'userError_low') userError_empty = do_conf.get_locators_or_account('LoginPageElements', 'userError_empty') passwordError_low = do_conf.get_locators_or_account( 'LoginPageElements', 'passwordError_low') passwordError_empty = do_conf.get_locators_or_account( 'LoginPageElements', 'passwordError_empty') # 登录成功后的用户显示元素 loginIcon = do_conf.get_locators_or_account('HomePageElements', 'loginIcon') def login(self, username, password): """登录流程""" self.open_url() print("!11111111.ceshi") self.click_password_login_btn() # self.switch_login_frame() self.clear_username() self.input_username(username) self.clear_password() self.input_password(password) try: self.click_login_btn() except: print("登录按钮置灰,不可用") def open_url(self): return self.load_url('http://daido.sitetest1.com/') #return self.load_url('https://mail.126.com') def click_password_login_btn(self): return self.click(*LoginPage.password_login_btn) def switch_login_frame(self): return self.switch_to_frame(*LoginPage.frame) def clear_username(self): return self.clear(*LoginPage.username) def input_username(self, username): self.clear_username() return self.send_keys(*LoginPage.username, username) def clear_password(self): return self.clear(*LoginPage.password) def input_password(self, password): self.clear_password() return self.send_keys(*LoginPage.password, password) def click_login_btn(self): return self.click(*LoginPage.loginBtn) def switch_default_frame(self): return self.switch_to_default_frame() # def get_error_text(self): # # return self.get_element_text(*LoginPage.error_head) # return self.get_element_text(*LoginPage.error_head) def get_user_error_text(self): # return self.get_element_text(*LoginPage.error_head) if self.get_element_text(*LoginPage.userError_low) != "": return self.get_element_text(*LoginPage.userError_low) else: return self.get_element_text(*LoginPage.userError_empty) def get_text(self, xpath): # return self.get_element_text(*LoginPage.error_head) print("xpath111", xpath) return self.get_element_text(xpath) def get_password_error_text(self): # return self.get_element_text(*LoginPage.error_head) if self.get_element_text(*LoginPage.passwordError_low) != "": return self.get_element_text(*LoginPage.passwordError_low) else: return self.get_element_text(*LoginPage.passwordError_empty) def get_login_success_account(self): return self.get_element_text(*LoginPage.loginIcon)
class LoginPage(BasePage): # 配置文件读取元素 do_conf = ParseConFile() # 选择密码登录的按钮 password_login_btn = do_conf.get_locators_or_account( 'LoginPageElements', 'password_login_btn') # 登录框外的iframe frame = do_conf.get_locators_or_account('LoginPageElements', 'frame') # 用户名输入框 username = do_conf.get_locators_or_account('LoginPageElements', 'username') # 密码输入框 password = do_conf.get_locators_or_account('LoginPageElements', 'password') # 登录按钮 loginBtn = do_conf.get_locators_or_account('LoginPageElements', 'loginBtn') # 登录失败的提示信息 error_head = do_conf.get_locators_or_account('LoginPageElements', 'errorHead') # 登录成功后的用户显示元素 account = do_conf.get_locators_or_account('HomePageElements', 'account') def login(self, username, password): """登录流程""" self.open_url() self.click_password_login_btn() self.switch_login_frame() self.input_username(username) self.input_password(password) self.click_login_btn() def open_url(self): return self.load_url('https://mail.163.com') def click_password_login_btn(self): return self.click(*LoginPage.password_login_btn) def switch_login_frame(self): return self.switch_to_frame(*LoginPage.frame) def clear_username(self): return self.clear(*LoginPage.username) def input_username(self, username): self.clear_username() return self.send_keys(*LoginPage.username, username) def clear_password(self): return self.clear(*LoginPage.password) def input_password(self, password): self.clear_password() return self.send_keys(*LoginPage.password, password) def click_login_btn(self): return self.click(*LoginPage.loginBtn) def switch_default_frame(self): return self.switch_to_default_frame() def get_error_text(self): return self.get_element_text(*LoginPage.error_head) def get_login_success_account(self): return self.get_element_text(*LoginPage.account)
class BasePage(object): """ 结合显示等待封装一些selenium 内置方法 """ cf = ParseConFile() excel = ParseExcel() def __init__(self, driver, outTime=30): self.byDic = { 'id': By.ID, 'name': By.NAME, 'class_name': By.CLASS_NAME, 'xpath': By.XPATH, 'link_text': By.LINK_TEXT } self.driver = driver self.outTime = outTime def findElement(self, by, locator): """ find alone element :param by: eg: id, name, xpath, css..... :param locator: id, name, xpath for str :return: element object """ try: print('[Info:Starting find the element "{}" by "{}"!]'.format(locator, by)) element = wd(self.driver, self.outTime).until(lambda x : x.find_element(by, locator)) except TimeoutException as t: print('error: found "{}" timeout!'.format(locator), t) except NoSuchWindowException as e: print('error: no such "{}"'.format(locator), e) except Exception as e: raise e else: # print('[Info:Had found the element "{}" by "{}"!]'.format(locator, by)) return element def findElements(self, by, locator): """ find group elements :param by: eg: id, name, xpath, css..... :param locator: eg: id, name, xpath for str :return: elements object """ try: print('[Info:start find the elements "{}" by "{}"!]'.format(locator, by)) elements = wd(self.driver, self.outTime).until(lambda x : x.find_element(by, locator)) except TimeoutException as t: print(t) except NoSuchWindowException as e: print(e) except Exception as e: raise e else: # print('[Info:Had found the elements "{}" by "{}"!]'.format(locator, by)) return elements def isElementExsit(self, by, locator): """ assert element if exist :param by: eg: id, name, xpath, css..... :param locator: eg: id, name, xpath for str :return: if element return True else return false """ if by.lower() in self.byDic: try: wd(self.driver, self.outTime).\ until(EC.visibility_of_element_located((self.byDic[by], locator))) except TimeoutException: print('Error: element "{}" time out!'.format(locator)) return False except NoSuchWindowException: print('Error: element "{}" not exsit!'.format(locator)) return False return True else: print('the "{}" error!'.format(by)) def isClick(self, by, locator): """判断是否可点击,返回元素对象""" if by.lower() in self.byDic: try: element = wd(self.driver, self.outTime).\ until(EC.element_to_be_clickable((self.byDic[by], locator))) except Exception: return False return element else: print('the "{}" error!'.format(by)) def isAlertAndSwitchToIt(self): """ assert alert if exsit :return: alert obj """ try: re = wd(self.driver, self.outTime).until(EC.alert_is_present()) except NoAlertPresentException: return False except Exception: return False return re def switchToFrame(self, by, locator): """判断frame是否存在,存在就跳到frame""" print('info:switching to iframe "{}"'.format(locator)) if by.lower() in self.byDic: try: wd(self.driver, self.outTime).\ until(EC.frame_to_be_available_and_switch_to_it((self.byDic[by], locator))) except TimeoutException as t: print('error: found "{}" timeout!'.format(locator), t) except NoSuchFrameException as e: print('error: no such "{}"'.format(locator), e) except Exception as e: raise e else: print('the "{}" error!'.format(by)) def switchToDefaultFrame(self): """返回默认的frame""" print('info:switch back to default iframe') try: self.driver.switch_to.default_content() except Exception as e: print(e) def getAlertText(self): """获取alert的提示信息""" if self.isAlertAndSwitchToIt(): alert = self.isAlertAndSwitchToIt() return alert.text else: return None def getElementText(self, by, locator, name=None): """获取某一个元素的text信息""" try: element = self.findElement(by, locator) if name: return element.get_attribute(name) else: return element.text except: print('get "{}" text failed return None'.format(locator)) return None def loadUrl(self, url): """加载url""" print('info: string upload url "{}"'.format(url)) self.driver.get(url) def getSource(self): """获取页面源码""" return self.driver.page_source def sendKeys(self, by, locator, value=''): """写数据""" print('info:input "{}"'.format(value)) try: element = self.findElement(by, locator) element.send_keys(value) except AttributeError as e: print(e) def clear(self, by, locator): """清理数据""" print('info:clearing value') try: element = self.findElement(by, locator) element.clear() except AttributeError as e: print(e) def click(self, by, locator): """点击某个元素""" print('info:click "{}"'.format(locator)) element = self.isClick(by, locator) if element: element.click() else: print('the "{}" unclickable!') def sleep(self, num=0): """强制等待""" print('info:sleep "{}" minutes'.format(num)) time.sleep(num) def ctrlV(self, value): """ctrl + V 粘贴""" print('info:pasting "{}"'.format(value)) ClipBoard.setText(value) self.sleep(3) KeyBoard.twoKeys('ctrl', 'v') def enterKey(self): """enter 回车键""" print('info:keydown enter') KeyBoard.oneKey('enter') def waitElementtobelocated(self, by, locator): """显示等待某个元素出现,且可见""" print('info:waiting "{}" to be located'.format(locator)) try: wd(self.driver, self.outTime).until(EC.visibility_of_element_located((self.byDic[by], locator))) except TimeoutException as t: print('error: found "{}" timeout!'.format(locator), t) except NoSuchWindowException as e: print('error: no such "{}"'.format(locator), e) except Exception as e: raise e def assertValueInSource(self, value): """断言某个关键字是否存在页面源码中""" print('info:assert "{}" in page source'.format(value)) source = self.getSource() assert value in source, '关键字"{}"不存在源码中!'.format(value) def assertStringContainsValue(self, String, value): """断言某段字符串包含另一个字符串""" print('info:assert "{}" contains "{}"'.format(String, value)) assert value in String, '"{}"不包含"{}"!'.format(String, value) @staticmethod def getSheet(sheetName): """获取某个sheet页的对象""" sheet = BasePage.excel.getSheetByName(sheetName) return sheet
class MyPage(BasePage): # 配置文件读取元素 do_conf = ParseConFile() # 左上角登录icon iamC = do_conf.get_locators_or_account('MyPageElements', 'iamC') # 首页 manage = do_conf.get_locators_or_account('MyPageElements', 'manage') # 推荐 message = do_conf.get_locators_or_account('MyPageElements', 'message') # 援助 info = do_conf.get_locators_or_account('MyPageElements', 'info') # 社区 iamS = do_conf.get_locators_or_account('MyPageElements', 'iamS') # 推荐 iamC_Center = do_conf.get_locators_or_account('MyPageElements', 'iamC_Center') # 援助-need iamC_Order = do_conf.get_locators_or_account('MyPageElements', 'iamC_Order') # 社区 iamC_Evaluation = do_conf.get_locators_or_account('MyPageElements', 'iamC_Evaluation') # 援助-help iamC_Refund = do_conf.get_locators_or_account('MyPageElements', 'iamC_Refund') # 援助-help iamC_ReportT = do_conf.get_locators_or_account('MyPageElements', 'iamC_ReportT') # 援助-help iamC_FReport = do_conf.get_locators_or_account('MyPageElements', 'iamC_FReport') def my_page_select_menu(self, menu=''): if menu == "iamC": self.click_iamC() elif menu == 'manage': self.click_manage() elif menu == 'message': self.click_message() elif menu == 'info': self.click_info() elif menu == 'iamS': self.click_iamS() # elif menu == 'iamC_Center': # self.click_iamC_Center() # elif menu == 'iamC_Order': # self.click_iamC_Order() # elif menu == 'iamC_Evaluation': # self.click_iamC_Evaluation() # elif menu == 'iamC_Refund': # self.click_iamC_Refund() # elif menu == 'iamC_ReportT': # self.click_iamC_ReportT() # elif menu == 'iamC_FReport': # self.click_iamC_FReport() # else: # pass def click_iamC(self): print("iamC的位置地址是:", *MyPage.iamC) return self.click(*MyPage.iamC) def click_manage(self): print("manage的位置地址是", *MyPage.manage) return self.click(*MyPage.manage) def click_message(self): print("message的位置地址是", *MyPage.message) return self.click(*MyPage.message) def click_info(self): print("info的位置地址是", *MyPage.info) return self.click(*MyPage.info) def click_iamS(self): print("iamS的位置地址是", *MyPage.iamS) return self.click(*MyPage.iamS) def click_iamC_Center(self): print("iamC_Center的位置地址是:", *MyPage.iamC_Center) return self.click(*MyPage.iamC_Center) def click_iamC_Order(self): print("recommend的位置地址是", *MyPage.iamC_Order) return self.click(*MyPage.iamC_Order) def click_iamC_Evaluation(self): print("message的位置地址是", *MyPage.iamC_Evaluation) return self.click(*MyPage.iamC_Evaluation) def click_iamC_Refund(self): print("message的位置地址是", *MyPage.iamC_Refund) return self.click(*MyPage.iamC_Refund) def click_iamC_ReportT(self): print("message的位置地址是", *MyPage.iamC_ReportT) return self.click(*MyPage.iamC_ReportT) def click_iamC_FReport(self): print("message的位置地址是", *MyPage.iamC_FReport) return self.click(*MyPage.iamC_FReport)
class RecommendPage(BasePage): # 配置文件读取元素 do_conf = ParseConFile() # 选择密码登录的按钮 select1 = do_conf.get_locators_or_account('RecommendPageElements', 'select1') # 登录框外的iframe select2 = do_conf.get_locators_or_account('RecommendPageElements', 'select2') # 用户名输入框 select3 = do_conf.get_locators_or_account('RecommendPageElements', 'select3') # 密码输入框 select4 = do_conf.get_locators_or_account('RecommendPageElements', 'select4') # 登录按钮 nextBtn = do_conf.get_locators_or_account('RecommendPageElements', 'next_btn') select5_opt2 = do_conf.get_locators_or_account('RecommendPageElements', 'select5_opt2') select6_opt2 = do_conf.get_locators_or_account('RecommendPageElements', 'select6_opt2') select7_opt2 = do_conf.get_locators_or_account('RecommendPageElements', 'select7_opt2') select8_opt2 = do_conf.get_locators_or_account('RecommendPageElements', 'select8_opt2') select9_opt2 = do_conf.get_locators_or_account('RecommendPageElements', 'select9_opt2') # 登录按钮 loginBtn = do_conf.get_locators_or_account('RecommendPageElements', 'login_Btn') detail_btn = do_conf.get_locators_or_account('RecommendPageElements', 'detail_btn') tel_btn = do_conf.get_locators_or_account('RecommendPageElements', 'tel_btn') email_btn = do_conf.get_locators_or_account('RecommendPageElements', 'email_btn') def click_select1_btn(self): return self.click(*RecommendPage.select1) def click_select2_btn(self): return self.click(*RecommendPage.select2) def click_select3_btn(self): return self.click(*RecommendPage.select3) def click_select4_btn(self): return self.click(*RecommendPage.select4) def click_select1_opt(self, value, type): return self.select_option(*RecommendPage.select1[1:], value, type) def click_select2_opt(self, value, type): return self.select_option(*RecommendPage.select2[1:], value, type) def click_select3_opt(self, value, type): return self.select_option(*RecommendPage.select3[1:], value, type) def click_select4_opt(self, value, type): return self.select_option(*RecommendPage.select4[1:], value, type)
class PlanSettingPage(BasePage): do_conf = ParseConFile() addplan_btn = do_conf.get_locators_or_account('PlanSettingPageElements', 'addplan_btn') save_btn = do_conf.get_locators_or_account('PlanSettingPageElements', 'save_btn') planname = do_conf.get_locators_or_account('PlanSettingPageElements', 'planname') def query_flow(self, planname): """查询审方方案流程""" self.search_plan(planname) display = self.get_display_data() database = self.get_database_data(planname) return listdict_compare(display, database) def add_flow(self, planname): self.add_plan(planname) self.result(planname) # 断言方案添加成功 self.search_plan(planname) def modify_flow(self): pass def delete_flow(self): pass def search_plan(self, planname): # 输入方案名称 if planname is not None: self.send_keys('css selector', 'input.audit-setting-search-input', planname) # 点击搜索 self.click('css selector', '.ip-btn>span:nth-of-type(2)') def add_plan(self, planname): """新增审方方案流程""" self.click_addplan_btn() self.input_planname(planname) self.click_save_btn() def get_display_data(self): """获取页面的查询出的审方方案数据""" size = (self.find_element('css selector', 'span.v-middle')).text[2:-1] # 获取总页数 data_list = [] data_dict = {} for i in range(int(size)): rows = len(self.find_elements('css selector', 'tbody>tr')) - 1 for j in range(rows): j += 1 ss = self.find_elements( 'css selector', 'tbody>tr:nth-of-type({}) td'.format(j + 1)) data_dict['name'] = ss[0].text data_dict['category'] = ss[1].text data_dict['user_name'] = ss[2].text # data_dict['created_time'] = ss[3].text # data_dict['modified_time'] = ss[4].text data_list.append(data_dict) if j < int(size): self.click('css selector', 'span.-next-page') # 点击下一页 return data_list def get_database_data(self, planname): db = ConnectDB() cur = db.connect().cursor(pymysql.cursors.DictCursor) plan_name = 'and name like "%%%s%%"' # SELECT name, category,user_name,created_time,modified_time sql = 'SELECT name, category,user_name FROM `sf_audit_plan` where 1 = 1 ' if planname is not None: sql = (sql + plan_name) % planname cur.execute(sql) print(sql) database_data = cur.fetchall() return database_data def plannames(self): """获取创建后的审方方案名称""" names = self.find_elements("css selector", "tbody>tr>td:nth-of-type(1)") return [ele.text for ele in names] def result(self, planname): plannames = self.plannames() if planname in plannames: return True else: return False def delete_plans(self): plans = self.find_elements("css selector", "tbody>tr button:nth-of-type(3)>span") for plan in plans: self.click('css selector', plan) def input_planname(self, planname): return self.send_keys(*PlanSettingPage.planname, planname) def click_addplan_btn(self): return self.click(*PlanSettingPage.addplan_btn) def click_save_btn(self): return self.click(*PlanSettingPage.save_btn)
class HomePage(BasePage): # 配置文件读取元素 do_conf = ParseConFile() # title title = do_conf.get_locators_or_account('HomePageElements', 'title') # 首页 homePage = do_conf.get_locators_or_account('HomePageElements', 'homePage') # 监控 monitor = do_conf.get_locators_or_account('HomePageElements', 'monitor') # 报警 alarm = do_conf.get_locators_or_account('HomePageElements', 'alarm') # 车辆 vehicle = do_conf.get_locators_or_account('HomePageElements', 'vehicle') # 统计 statistics= do_conf.get_locators_or_account('HomePageElements', 'statistics') # 设置 settings = do_conf.get_locators_or_account('HomePageElements', 'settings') def select_menu(self, menu='settings'): self.click_title_menu() if menu == "monitor": self.click_monitor_menu() elif menu == 'homePage': self.click_home_page_menu() elif menu == 'alarm': self.click_alarm_menu() elif menu == 'truck': self.click_vehicle_menu() elif menu == 'statistics': self.click_statistics_menu() elif menu == 'settings': self.click_settings_menu() else: raise ValueError( '''菜单选择错误! homePage->首页 monitor->监控 alarm->报警 truck->车辆 statistics->统计 setting->设置''' ) def click_title_menu(self): return self.move_to_element(*HomePage.title) def click_home_page_menu(self): return self.click(*HomePage.homePage) def click_monitor_menu(self): return self.click(*HomePage.monitor) def click_alarm_menu(self): return self.click(*HomePage.alarm) def click_vehicle_menu(self): return self.click(*HomePage.vehicle) def click_statistics_menu(self): return self.click(*HomePage.statistics) def click_settings_menu(self): return self.click(*HomePage.settings)
class SendMailPage(BasePage): # 配置文件读取元素 do_conf = ParseConFile() # 写信按钮 writeMail = do_conf.get_locators_or_account('SendMailPageElements', 'writeMail') # 收件人输入框 addressee = do_conf.get_locators_or_account('SendMailPageElements', 'addressee') # 邮件主题 subject = do_conf.get_locators_or_account('SendMailPageElements', 'subject') # 上传附件 uploadAttachment = do_conf.get_locators_or_account('SendMailPageElements', 'uploadAttachment') # 正文外的iframe iframe = do_conf.get_locators_or_account('SendMailPageElements', 'iframe') # 正文 text = do_conf.get_locators_or_account('SendMailPageElements', 'text') # 发送按钮 sendBtn = do_conf.get_locators_or_account('SendMailPageElements', 'sendBtn') # 发送成功的提示信息 send_success = do_conf.get_locators_or_account('SendMailPageElements', 'send_success') # 收件人为空的提示信息 error_info_address_is_none = do_conf.get_locators_or_account( 'SendMailPageElements', 'error_info_address_is_none') # 收件人格式或者主题为空的提示信息 error_info_popup_window = do_conf.get_locators_or_account( 'SendMailPageElements', 'error_info_popup_window') def send_mail(self, address, subject, text, pfa): self.click_write_mail_btn() self.input_address(address) self.input_subject(subject) if pfa: self.upload_file(pfa) self.switch_frame() self.input_main_text(text) self.switch_default_frame() self.click_send_btn() def click_write_mail_btn(self): return self.click(*SendMailPage.writeMail) def input_address(self, address): return self.send_keys(*SendMailPage.addressee, address) def input_subject(self, subject): return self.send_keys(*SendMailPage.subject, subject) def upload_file(self, pfa): return self.send_keys(*SendMailPage.uploadAttachment, pfa) def switch_frame(self): return self.switch_to_frame(*SendMailPage.iframe) def input_main_text(self, text): return self.send_keys(*SendMailPage.text, text) def switch_default_frame(self): return self.switch_to_default_frame() def click_send_btn(self): return self.click(*SendMailPage.sendBtn) def wait_success_info_element_located(self): return self.wait_element_to_be_located(*SendMailPage.send_success) def get_error_address_is_none(self): element = self.driver.find_element( *SendMailPage.error_info_address_is_none) return element.text def get_error_popup_window(self): return self.get_element_text(*SendMailPage.error_info_popup_window)
class MissionListPage(BasePage): do_conf = ParseConFile() multi_box = do_conf.get_locators_or_account('MissionListPageElements', 'multi_box') first_box = do_conf.get_locators_or_account('MissionListPageElements', 'first_box') multi_btn = do_conf.get_locators_or_account('MissionListPageElements', 'multi_btn') multi_succMsg = do_conf.get_locators_or_account('MissionListPageElements', 'multi_succMsg') start_sf = do_conf.get_locators_or_account('MissionListPageElements', 'start_sf') end_sf = do_conf.get_locators_or_account('MissionListPageElements', 'end_sf') view = do_conf.get_locators_or_account('MissionListPageElements', 'view') aduit_advice = do_conf.get_locators_or_account('MissionListPageElements', 'audit_advice') reject_btn1 = do_conf.get_locators_or_account('MissionListPageElements', 'reject_btn1') reject_btn2 = do_conf.get_locators_or_account('MissionListPageElements', 'reject_btn2') pass_btn = do_conf.get_locators_or_account('MissionListPageElements', 'pass_btn') back_list = do_conf.get_locators_or_account('MissionListPageElements', 'back_list') def multi_one(self): """批量通过单个任务""" self.click_first_box() self.click_multi_btn() flag = self.is_element_exist(*self.multi_succMsg) assert flag # def multi_all(self): """批量通过待审列表全部任务""" def audit_reject1(self): """审核打回""" self.click_view() self.input_audit_advice() time.sleep(1) self.click_reject_btn1() self.click_back_list() def audit_reject2(self): """审核打回(可双签)""" self.click_view() self.input_audit_advice() time.sleep(1) self.click_reject_btn2() self.click_back_list() def audit_pass(self): """审核通过""" self.click_view() time.sleep(1) self.click_pass_btn() self.click_back_list() # def click_multi_box(self): # self.click(self.multi_box) def click_first_box(self): self.click(*self.first_box) def click_multi_btn(self): self.click(*self.multi_btn) def click_start_sf(self): self.click(*self.start_sf) def click_end_sf(self): self.click(*self.end_sf) def click_view(self): self.click(*self.view) def input_audit_advice(self, audit_advice="审核意见"): self.send_keys(*self.aduit_advice, audit_advice) def click_reject_btn1(self): self.click(*self.reject_btn1) def click_reject_btn2(self): self.click(*self.reject_btn2) def click_pass_btn(self): self.click(*self.pass_btn) def click_back_list(self): self.click(*self.back_list)
class HomePage(BasePage): # 配置文件读取元素 do_conf = ParseConFile() # 左上角登录icon loginIcon = do_conf.get_locators_or_account('HomePageElements', 'loginIcon') # 菜单栏 # 首页 top = do_conf.get_locators_or_account('HomePageElements', 'top') # 推荐 recommend = do_conf.get_locators_or_account('HomePageElements', 'recommend') # 援助 collaboration = do_conf.get_locators_or_account('HomePageElements', 'collaboration') # 社区 community = do_conf.get_locators_or_account('HomePageElements', 'community') # 个人主页 myPage = do_conf.get_locators_or_account('HomePageElements', 'myPage') # 图片点击 # 推荐 pic_recommend = do_conf.get_locators_or_account('HomePageElements', 'pic_recommend') # 援助-need pic_collaboration_needs = do_conf.get_locators_or_account( 'HomePageElements', 'pic_collaboration_needs') # 社区 pic_community = do_conf.get_locators_or_account('HomePageElements', 'pic_community') # 援助-help pic_collaboration_help = do_conf.get_locators_or_account( 'HomePageElements', 'pic_collaboration_help') def select_menu(self, menu=''): if menu == "top": self.click_top_menu() elif menu == 'recommend': self.click_recommend_menu() elif menu == 'collaboration': self.click_collaboration_menu() elif menu == 'community': self.click_community_menu() elif menu == 'myPage': self.click_myPage_menu() elif menu == 'pic_recommend': self.click_pic_recommend() elif menu == 'pic_collaboration_needs': self.click_pic_collaboration_needs() elif menu == 'pic_community': self.click_pic_community() elif menu == 'pic_collaboration_help': self.click_pic_collaboration_help() else: raise ValueError('''菜单选择错误! top->首页 recommend->推荐 collaboration->援助 community->社区 myPage->个人主页''') def click_top_menu(self): print("top的位置地址是:", *HomePage.top) return self.click(*HomePage.top) def click_recommend_menu(self): print("recommend的位置地址是", *HomePage.recommend) return self.click(*HomePage.recommend) def click_collaboration_menu(self): print("collaboration的位置地址是", *HomePage.collaboration) return self.click(*HomePage.collaboration) def click_community_menu(self): print("community的位置地址是", *HomePage.community) return self.click(*HomePage.community) def click_myPage_menu(self): print("myPage的位置地址是", *HomePage.myPage) return self.click(*HomePage.myPage) def click_pic_recommend(self): print("pic_recommend的位置地址是:", *HomePage.pic_recommend) return self.click(*HomePage.pic_recommend) def click_pic_collaboration_needs(self): print("recommend的位置地址是", *HomePage.pic_collaboration_needs) return self.click(*HomePage.pic_collaboration_needs) def click_pic_community(self): print("collaboration的位置地址是", *HomePage.pic_community) return self.click(*HomePage.pic_community) def click_pic_collaboration_help(self): print("community的位置地址是", *HomePage.pic_collaboration_help) return self.click(*HomePage.pic_collaboration_help)
class BasePage(object): """结合显示等待封装一些selenium内置方法""" cf = ParseConFile() excel = ParseExcel() def __init__(self, driver, timeout=30): self.byDic = { 'id': By.ID, 'name': By.NAME, 'class_name': By.CLASS_NAME, 'xpath': By.XPATH, 'link_text': By.LINK_TEXT } self.driver = driver self.outTime = timeout def find_element(self, by, locator): """ find alone element :param by: eg: id, name, xpath, css..... :param locator: id, name, xpath for str :return: element object """ try: print('[Info:Starting find the element "{}" by "{}"!]'.format(locator, by)) element = WD(self.driver, self.outTime).until(lambda x: x.find_element(by, locator)) except TimeoutException as t: print('error: found "{}" timeout!'.format(locator), t) else: return element def find_elements(self, by, locator): """ find group elements :param by: eg: id, name, xpath, css..... :param locator: eg: id, name, xpath for str :return: elements object """ try: print('[Info:start find the elements "{}" by "{}"!]'.format(locator, by)) elements = WD(self.driver, self.outTime).until(lambda x: x.find_elements(by, locator)) except TimeoutException as t: print('error: found "{}" timeout!'.format(locator), t) else: return elements def is_element_exist(self, by, locator): """ assert element if exist :param by: eg: id, name, xpath, css..... :param locator: eg: id, name, xpath for str :return: if element return True else return false """ if by.lower() in self.byDic: try: WD(self.driver, self.outTime). \ until(ec.visibility_of_element_located((self.byDic[by], locator))) except TimeoutException: print('Error: element "{}" not exist'.format(locator)) return False return True else: print('the "{}" error!'.format(by)) def is_click(self, by, locator): if by.lower() in self.byDic: try: element = WD(self.driver, self.outTime). \ until(ec.element_to_be_clickable((self.byDic[by], locator))) except TimeoutException: print("元素不可以点击") else: return element else: print('the "{}" error!'.format(by)) def is_alert(self): """ assert alert if exsit :return: alert obj """ try: re = WD(self.driver, self.outTime).until(ec.alert_is_present()) except (TimeoutException, NoAlertPresentException): print("error:no found alert") else: return re def switch_to_frame(self, by, locator): """判断frame是否存在,存在就跳到frame""" print('info:switching to iframe "{}"'.format(locator)) if by.lower() in self.byDic: try: WD(self.driver, self.outTime). \ until(ec.frame_to_be_available_and_switch_to_it((self.byDic[by], locator))) except TimeoutException as t: print('error: found "{}" timeout!切换frame失败'.format(locator), t) else: print('the "{}" error!'.format(by)) def switch_to_default_frame(self): """返回默认的frame""" print('info:switch back to default iframe') try: self.driver.switch_to.default_content() except Exception as e: print(e) def get_alert_text(self): """获取alert的提示信息""" alert = self.is_alert() if alert: return alert.text else: return None def get_element_text(self, by, locator, name=None): """获取某一个元素的text信息""" try: element = self.find_element(by, locator) if name: return element.get_attribute(name) else: return element.text except AttributeError: print('get "{}" text failed return None'.format(locator)) def load_url(self, url): """加载url""" print('info: string upload url "{}"'.format(url)) self.driver.get(url) def get_source(self): """获取页面源码""" return self.driver.page_source def send_keys(self, by, locator, value=''): """写数据""" print('info:input "{}"'.format(value)) try: element = self.find_element(by, locator) element.send_keys(value) except AttributeError as e: print(e) def clear(self, by, locator): """清理数据""" print('info:clearing value') try: element = self.find_element(by, locator) element.clear() except AttributeError as e: print(e) def click(self, by, locator): """点击某个元素""" print('info:click "{}"'.format(locator)) element = self.is_click(by, locator) if element: element.click() else: print('the "{}" unclickable!') @staticmethod def sleep(num=0): """强制等待""" print('info:sleep "{}" minutes'.format(num)) time.sleep(num) def ctrl_v(self, value): """ctrl + V 粘贴""" print('info:pasting "{}"'.format(value)) ClipBoard.set_text(value) self.sleep(3) KeyBoard.two_keys('ctrl', 'v') @staticmethod def enter_key(): """enter 回车键""" print('info:keydown enter') KeyBoard.one_key('enter') def wait_element_to_be_located(self, by, locator): """显示等待某个元素出现,且可见""" print('info:waiting "{}" to be located'.format(locator)) try: return WD(self.driver, self.outTime).until(ec.presence_of_element_located((self.byDic[by], locator))) except TimeoutException as t: print('error: found "{}" timeout!'.format(locator), t) def get_page_source(self): return self.get_source() """ 功能:遍历option下拉框,比对结果 value_text写法:从第一个依次写,每个选项用, 分割,最后一个后面需要, 举例"製造業, 卸売業, 小売業, IT, " 调用:option_value_check(self, "//*[@id='business_types']", "option", "製造業, 卸売業, 小売業, IT, ") """ def option_value_check(self, xpath, tag_name, value_text): select = self.find_element_by_xpath(xpath) # 注意使用find_elements options_list = select.find_elements_by_tag_name(tag_name) option_text = "" for option in options_list: # 获取下拉框的value和text # print("Value is:%s Text is:%s" % (option.get_attribute("value"), option.text)) s1 = Select(self.driver.find_element_by_xpath(xpath)) s1.select_by_visible_text(option.text) option_text = option_text + option.text + ', ' time.sleep(1) if option_text == value_text: option_text = option_text + "选项内容校验正确" else: option_text = option_text + "选项内容校验错误" return option_text def select_option(self, locator, value, type="index"): #self.wait_utilVisible(locator) time.sleep(2) se = self.driver.find_element_by_xpath(locator) #se = self.get_element_text("xpath", locator, name=None) select = Select(se) #logging.info("选择的type为{0}".format(type)) if type == "index": select.select_by_index(value) #Select(se).select_by_index(self,value) #logging.info("选择的index是{0}".format(value)) elif type == "value": Select(se).select_by_value(value) #logging.info("选择的值是{0}".format(value)) else: Select(se).select_by_visible_text(value) #ogging.info("根据文本内容传的值是{0}".format(value)) def save_screenshot(self, img_doc): ''' 页面截屏保存截图 :param img_doc: 截图说明 :return: ''' file_name = ROOT_DIR + "\\{}_{}.png".format(datetime.strftime(datetime.now(), "%Y%m%d%H%M%S"), img_doc) self.driver.save_screenshot(file_name) with open(file_name, mode='rb') as f: file = f.read() allure.attach(file, img_doc, allure.attachment_type.PNG)
class BasePage(object): """结合显示等待封装一些selenium内置方法""" cf = ParseConFile() excel = ParseExcel() def __init__(self, driver, timeout=30): self.byDic = { 'id': By.ID, 'name': By.NAME, 'class_name': By.CLASS_NAME, 'xpath': By.XPATH, 'link_text': By.LINK_TEXT } self.driver = driver self.outTime = timeout def find_element(self, by, locator): """ 查找单个元素 """ try: print('[Info:Starting find the element "{}" by "{}"!]'.format( locator, by)) element = WD( self.driver, self.outTime).until(lambda x: x.find_element(by, locator)) except TimeoutException as t: print('error: found "{}" timeout!'.format(locator), t) else: return element def find_elements(self, by, locator): """ 查找一组元素 """ try: print('[Info:start find the elements "{}" by "{}"!]'.format( locator, by)) elements = WD( self.driver, self.outTime).until(lambda x: x.find_elements(by, locator)) except TimeoutException as t: print('error: found "{}" timeout!'.format(locator), t) else: return elements def is_element_exist(self, by, locator): """ 判断元素可见 """ if by.lower() in self.byDic: try: WD(self.driver, self.outTime). \ until(EC.visibility_of_element_located((self.byDic[by], locator))) except TimeoutException: print('Error: element "{}" not exist'.format(locator)) return False return True else: print('the "{}" error!'.format(by)) def is_click(self, by, locator): """元素可点击""" if by.lower() in self.byDic: try: element = WD(self.driver, self.outTime). \ until(EC.element_to_be_clickable((self.byDic[by], locator))) except TimeoutException: print("元素不可以点击") else: return element else: print('the "{}" error!'.format(by)) def is_alert(self): """ 判断alert弹框存在 """ try: re = WD(self.driver, self.outTime).until(EC.alert_is_present()) except (TimeoutException, NoAlertPresentException): print("error:no found alert") else: return re def switch_to_frame(self, by, locator): """判断frame是否存在,存在就跳到frame""" print('info:switching to iframe "{}"'.format(locator)) if by.lower() in self.byDic: try: WD(self.driver, self.outTime). \ until(EC.frame_to_be_available_and_switch_to_it((self.byDic[by], locator))) except TimeoutException as t: print('error: found "{}" timeout!切换frame失败'.format(locator), t) else: print('the "{}" error!'.format(by)) def switch_to_default_frame(self): """返回默认的frame""" print('info:switch back to default iframe') try: self.driver.switch_to.default_content() except Exception as e: print(e) def get_alert_text(self): """获取alert的提示信息""" alert = self.is_alert() if alert: return alert.text else: return None def get_element_text(self, by, locator=None): """获取某一个元素的text信息""" try: element = WD(self.driver, 30).until( EC.visibility_of_element_located((self.byDic[by], locator))) # element = WD(self.driver,30).until( # EC.presence_of_element_located((self.byDic[by], locator)) # ) print("element.text:{}".format(element.text)) return element.text except AttributeError: print('get "{}" text failed return None'.format(locator)) def load_url(self, url): """加载url""" print('info: string upload url "{}"'.format(url)) self.driver.get(url) def get_source(self): """获取页面源码""" return self.driver.page_source def get_currenturl(self): """获取当前页的url""" return self.driver.current_url def send_keys(self, value, by, locator=''): """写数据""" print('info:input "{}"'.format(value)) try: element = WD(self.driver, 30).until( EC.presence_of_element_located((by, locator))) element.send_keys(value) time.sleep(1) except AttributeError as e: print(e) def clear(self, by, locator): """清理数据""" print('info:clearing value') try: element = self.find_element(by, locator) element.clear() except AttributeError as e: print(e) def click(self, by, locator): """点击某个元素""" print('info:click "{}"'.format(locator)) element = self.is_click(by, locator) if element != None: element.click() else: print('the "{}" unclickable!') def move_to_element(self, by, locator): """移动至某个元素""" print('info:moveto "{}"'.format(locator)) element = self.find_element(by, locator) if element: webdriver.ActionChains( self.driver).move_to_element(element).click().perform() time.sleep(1) else: print('the "{}" unclickable!') @staticmethod def sleep(num=0): """强制等待""" print('info:sleep "{}" minutes'.format(num)) time.sleep(num) # def ctrl_v(self, value): # """ctrl + V 粘贴""" # print('info:pasting "{}"'.format(value)) # ClipBoard.set_text(value) # self.sleep(3) # KeyBoard.two_keys('ctrl', 'v') # @staticmethod # def enter_key(): # """enter 回车键""" # print('info:keydown enter') # KeyBoard.one_key('enter') def wait_element_to_be_located(self, by, locator): """显示等待某个元素出现""" print('info:waiting "{}" to be located'.format(locator)) try: return WD(self.driver, self.outTime).until( EC.presence_of_element_located((self.byDic[by], locator))) except TimeoutException as t: print('error: found "{}" timeout!'.format(locator), t) def get_page_source(self): return self.get_source()
---------------------------------- @Time: 2019/11/04 @Author: Tian @Files: conftest.py @IDE: Pycharm ---------------------------------- """ import pytest from Page.PageObject.LoginPage import LoginPage from Page.PageObject.HomePage import HomePage from Page.PageObject.ContactPage import ContactPage from Page.PageObject.SendMailPage import SendMailPage from util.parseConFile import ParseConFile do_conf = ParseConFile() # 从配置文件中获取正确的用户名和密码 userName = do_conf.get_locators_or_account('126LoginAccount', 'username') passWord = do_conf.get_locators_or_account('126LoginAccount', 'password') # @pytest.fixture(scope='function') # def login(driver): # """除登录用例,每一个用例的前置条件""" # print('------------staring login------------') # login_action = LoginPage(driver, 30) # login_action.login(userName, passWord) # yield # print('------------end login------------') # driver.delete_all_cookies()
class LoginPage(BasePage): # 配置文件读取元素 do_conf = ParseConFile() # 用户名输入框 username = do_conf.get_locators_or_account('LoginPageElements', 'username') # 密码输入框 password = do_conf.get_locators_or_account('LoginPageElements', 'password') #验证码 authcode = do_conf.get_locators_or_account('LoginPageElements','authcode') #切换验证码 change_codeBtn = do_conf.get_locators_or_account('LoginPageElements','changecode') # 登录按钮 loginBtn = do_conf.get_locators_or_account('LoginPageElements', 'loginBtn') # 登录失败的提示信息 error_head = do_conf.get_locators_or_account('LoginPageElements', 'errorHead') # 登录成功后的用户显示元素 account = do_conf.get_locators_or_account('HomePageElements', 'account') def login(self, username, password): """登录流程""" self.open_url() self.input_username(username) self.input_password(password) self.click_changecode_btn #数据库中取最新的验证码 ret=local_db.select_sql('id','challenge','response','captcha_captchastore','id') self.input_authcode(ret['response']) self.click_login_btn() time.sleep(1) def open_url(self): return self.load_url(URL) def get_url(self): current_url=self.get_currenturl() print(current_url) return current_url def clear_username(self): return self.clear(*LoginPage.username) def input_username(self, username): self.clear_username() return self.send_keys(username,*LoginPage.username) def clear_password(self): return self.clear(*LoginPage.password) def input_password(self, password): self.clear_password() return self.send_keys(password,*LoginPage.password) def input_authcode(self,code): return self.send_keys(code,*LoginPage.authcode) def click_login_btn(self): return self.click(*LoginPage.loginBtn) def click_changecode_btn(self): return self.click(*LoginPage.change_codeBtn) def switch_default_frame(self): return self.switch_to_default_frame() def get_error_text(self): return self.get_element_text(*LoginPage.error_head) def get_login_success_account(self): return self.get_element_text(*LoginPage.account)
class CollaborationPage_2(BasePage): # 配置文件读取元素 do_conf = ParseConFile() select_opt1 = do_conf.get_locators_or_account( 'collaborationPageElements_1', 'select_opt1') select_opt2 = do_conf.get_locators_or_account( 'collaborationPageElements_1', 'select_opt2') select_opt3 = do_conf.get_locators_or_account( 'collaborationPageElements_1', 'select_opt3') # 登录按钮 select_Btn = do_conf.get_locators_or_account('collaborationPageElements_1', 'select_Btn') # 选择密码登录的按钮 select1 = do_conf.get_locators_or_account('collaborationPageElements_2', 'select1') select2 = do_conf.get_locators_or_account('collaborationPageElements_2', 'select2') select3 = do_conf.get_locators_or_account('collaborationPageElements_2', 'select3') select4 = do_conf.get_locators_or_account('collaborationPageElements_2', 'select4') select5 = do_conf.get_locators_or_account('collaborationPageElements_2', 'select5') select6 = do_conf.get_locators_or_account('collaborationPageElements_2', 'select6') # 登录按钮 login_Btn = do_conf.get_locators_or_account('collaborationPageElements_2', 'login_Btn') detail_btn = do_conf.get_locators_or_account('collaborationPageElements_2', 'detail_btn') tel_btn = do_conf.get_locators_or_account('collaborationPageElements_2', 'tel_btn') email_btn = do_conf.get_locators_or_account('collaborationPageElements_2', 'email_btn') def click_select1_btn(self): return self.click(*CollaborationPage_2.select1) def click_select2_btn(self): return self.click(*CollaborationPage_2.select2) def click_select3_btn(self): return self.click(*CollaborationPage_2.select3) def click_select4_btn(self): return self.click(*CollaborationPage_2.select4) def click_select5_btn(self): return self.click(*CollaborationPage_2.select5) def click_select6_btn(self): return self.click(*CollaborationPage_2.select6) def click_select1_opt(self, value, type): return self.select_option(*CollaborationPage_2.select1[1:], value, type) def click_select2_opt(self, value, type): return self.select_option(*CollaborationPage_2.select2[1:], value, type) def click_select3_opt(self, value, type): return self.select_option(*CollaborationPage_2.select3[1:], value, type) def click_select4_opt(self, value, type): return self.select_option(*CollaborationPage_2.select4[1:], value, type) def click_select5_opt(self, value, type): return self.select_option(*CollaborationPage_2.select5[1:], value, type) def click_select6_opt(self, value, type): return self.select_option(*CollaborationPage_2.select6[1:], value, type)
class ProfilePage(BasePage): # 配置文件读取元素 do_conf = ParseConFile() # 新键worker按钮 new_worker_btn = do_conf.get_locators_or_account('ProfilePageElements', 'new_worker') # 修改worker按钮 alter_worker_btn = do_conf.get_locators_or_account('ProfilePageElements', 'alter_worker') #删除worker按钮 delete_worker_btn = do_conf.get_locators_or_account( 'ProfilePageElements', 'delete_worker') # 姓名输入框 name = do_conf.get_locators_or_account('ProfilePageElements', 'name') # 电话输入框 phone = do_conf.get_locators_or_account('ProfilePageElements', 'phone') # 部门输入框 department = do_conf.get_locators_or_account('ProfilePageElements', 'department') # 确定按钮 commit = do_conf.get_locators_or_account('ProfilePageElements', 'commit') # 删除确定按钮 commit_del = do_conf.get_locators_or_account('ProfilePageElements', 'delete_btn') #信息行 item = do_conf.get_locators_or_account('ProfilePageElements', 'item') # 添加的提示信息 add_tip = do_conf.get_locators_or_account('ProfilePageElements', 'addmsg') def add_worker(self, name, phone, department): """添加业务员""" self.click_new_btn() self.input_name(name) self.input_phone(phone) self.input_department(department) self.click_commit_btn() def alter_worker(self, name, phone, department): """修改业务员""" self.click_msssage_row() self.click_alter_btn() self.clear_name(name) self.input_name(name) self.clear_phone(phone) self.input_phone(phone) self.clear_department(department) self.input_department(department) self.click_commit_btn() def delete_worker(self): """删除业务员""" self.click_msssage_row() self.click_delete_btn() self.click_commit_del_btn() def click_new_btn(self): return self.click(*ProfilePage.new_worker_btn) def click_alter_btn(self): return self.click(*ProfilePage.alter_worker_btn) def click_delete_btn(self): return self.click(*ProfilePage.delete_worker_btn) def click_msssage_row(self): return self.move_to_element(*ProfilePage.item) def input_name(self, name): return self.send_keys(name, *ProfilePage.name) def input_phone(self, phone): return self.send_keys(phone, *ProfilePage.phone) def input_department(self, department): return self.send_keys(department, *ProfilePage.department) def clear_name(self, name): return self.clear(*ProfilePage.name) def clear_phone(self, phone): return self.clear(*ProfilePage.phone) def clear_department(self, department): return self.clear(*ProfilePage.department) def click_commit_btn(self): return self.click(*ProfilePage.commit) def click_commit_del_btn(self): return self.click(*ProfilePage.commit_del) def get_add_text(self): return self.get_element_text(*ProfilePage.add_tip)