PO设计模式在Appium中的应用——框架内容的补充

春衣少年当酒歌,起舞四顾以笑和。这篇文章主要讲述PO设计模式在Appium中的应用——框架内容的补充相关的知识,希望能为你提供帮助。
1.对basepage模块的改造
1)封装查找元素方法

def find(self, by, locator=None): # *by意思是传递多个位置参数,如果传递的是一个元组的话,就用前面的,locator注意默认等于None return self.driver.find_elements(*by) if isinstance(by, tuple) else self.driver.find_element(by, locator)

2)对find方法继续封装,添加如果在查找元素的时候有弹窗弹出的时候的处理逻辑
class BasePage():
#设置一个黑名单 _black_list = [MobileBy.ID, ‘image_cancel‘]
#设置一个计数器 _error_count = 0
#最大错误次数 _error_max = 10 _params = {}def __init__(self, driver: WebDriver = None): self._driver = driverdef find(self, by, locator=None): # *by意思是传递多个位置参数,如果传递的是一个元组的话,就用前面的,locator注意默认等于None try: self._error_count = 0 return self.driver.find_elements(*by) if isinstance(by, tuple) else self.driver.find_element(by, locator) except Exception as e: self._error_count += 1 if self._error_count > = self._error_max: raise e
# 循环遍历,但是有个问题,如果弹窗没有的话就会陷入一直查找的死循环,因此需要设置一个错误次数, 这里要是没有的话len=0,就不会进入if语句中,
就没有办法从for循环中出来 for black in self._black_list: elements = self.driver.find_elements(*black)
#对弹框进行处理,如果有弹框,find-elements的len大于0,然后就点击这个弹窗,关闭它,然后再返回去查找是否有弹框; if len(elements) > 0: elements[0].click() return self.find(by, locator) raise e

3)basepage中设置数据驱动逻辑
from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.webdriver import WebDriver
import yaml

class BasePage(): _black_list = [MobileBy.ID, ‘image_cancel‘] _error_count = 0 _error_max = 10 _params = {}def __init__(self, driver: WebDriver = None): self._driver = driver def steps(self, path):
#读取yaml文件, ??为什么设置utf编码呢,是因为防止路径中有中文,with一般会和close一起使用 with open(path, encoding="utf-8") as f: # 注意是list[]而不是list(),因为这是一个列表嵌套字典的文件list()是转换成列表的函数 steps: list[dict] = yaml.safe_load(f) print(steps)
###for step in steps:中的step是yaml文件中的一组数据 for step in steps: if "by" in step.keys(): element = self.find(step["by"], step["locator"]) if "action" in step.keys(): if "click" == step["action"]: element.click() if "send" == step["action"]: content: str = step["value"] for param in self._params: # 如果字典中的值命中了{value}就把param中的值替换了,这就是为什么_params={}设置为字典的原因 content = content.replace("{%s}" % param, self._params[param]) self.send(content, step["by"], step["locator"]) 

yaml文件中的一组数据如下:一定一定要注意-(空格)by:(空格)id,两处空格不要忘,否则读取出来的文件格式将会成为str格式而不是dict
PO设计模式在Appium中的应用——框架内容的补充

文章图片

 
 
 
 
【PO设计模式在Appium中的应用——框架内容的补充】 
 
 
 
 
 
 


    推荐阅读