PageObject设计模型
Page Object模式是使用Selenium的广大同行最为公认的一种设计模式。
在设计测试时,把元素和方法按照页面抽象出来,分离成一定的对象,然后再进行组织。
体现在对界面交互细节的封装,测试在更上层使用页面对象,在底层的属性或者操作的更改不会中断测试。
减少代码重复,提高测试代码的可读性和可维护性。
比如封装app启动的配置信息
desired_caps.py
# -*- coding: utf-8 -*-# from appium import webdriver import yaml import logging import logging.config #log提取、输出 CON_LOG='../log/log.conf' logging.config.fileConfig(CON_LOG) logging=logging.getLogger() #封装成一个方法 def appium_desired():
#调用yaml的配置
file = open('../case/capability.yaml',encoding="utf-8")
data = yaml.load(file,yaml.FullLoader)
desired_caps={ "platformName": data["platformName"], "platformVersion": data["platformVersion"], "deviceName": data["deviceName"], "appPackage": data["appPackage"], "appActivity": data["appActivity"], "unicodeKeyboard":data["unicodeKeyboard"], "resetKeyboard":data["resetKeyboard"], "noReset": data["noReset"] } logging.info("logging app...") driver = webdriver.Remote('http://'+str(data['ip'])+':'+str(data['port'])+'/wd/hub',desired_caps) return driver #调用方法 if __name__=="__main__": appium_desired()
然后写一个公共类,先写封装一个基类,用于driver、基础定位的调用
BaseView.py
# -*- coding: utf-8 -*-# #封装基类 class BaseView(object): def __init__(self,driver): self.driver=driver def find_element(self,*loc): return self.driver.find_element(*loc)
然后写一个启动淘宝的公共类封装
# -*- coding: utf-8 -*-# #封装公共类 from Appiumtaobao.page_object.BaseView import BaseView from Appiumtaobao.page_object.desired_caps import appium_desired from selenium.common.exceptions import NoSuchElementException import logging from selenium.webdriver.common.by import By class common(BaseView): agreebtn=(By.ID,"com.taobao.taobao:id/provision_positive_button") localtionbtn=(By.ID,"com.taobao.taobao:id/uik_mdButtonDefaultPositive") #同意协议按钮 def check_agreebtn(self): logging.info('==========check_agreebtn==========') try: agreebtn=self.driver.find_element(*self.agreebtn) except NoSuchElementException: logging.info("no agreebtn") else: agreebtn.click() #获取位置权限 def check_localtionbtn(self): logging.info("===========check_localtionbtn===========") try: localtionbtn=self.driver.find_element(*self.localtionbtn) except NoSuchElementException: logging.info("no localtionbtn") else: localtionbtn.click() if __name__=="__main__": driver=appium_desired() com=common(driver) com.check_agreebtn() driver.implicitly_wait(3) com.check_localtionbtn()