1.用装饰器,制作black黑名单,在黑名单里处理异常:源码在test_frame里的base_page和black_handle里
用闭包:
1. black_handle.py def black_wrapper(fun): def run(*args, **kwargs): basepage = args[0] try: return fun(*args, **kwargs) # 捕获元素没找到异常 except Exception as e: # 遍历黑名单中的元素,进行处理 for black in basepage.black_list: # 用finds是为什么? eles = basepage.finds(*black) # 黑名单被找到 if len(eles) > 0: # 对黑名单元素进行点击,可以*扩展 eles[0].click() return fun(*args, **kwargs) raise e return run
2. base_page.py class BasePage: def __init__(self, driver: WebDriver = None): self.driver = driver # 参考:黑名单类 self.black_list = [(By.XPATH, "//*[@resource-id='com.xueqiu.android:id/iv_close']")] # 设计模式:代理模式,装饰器模式 # 装饰器 @black_wrapper def find(self, by, locator): return self.driver.find_element(by, locator)