minium一些常用方法的封装
class BaseDef(minium.MiniTest):
def __init__(self, mini):
super().__init__()
self.mini = mini
def navigate_to_open(self, route):
"""以导航的方式跳转到指定页面,不允许跳转到 tabbar 页面,支持相对路径和绝对路径, 小程序中页面栈最多十层"""
# self.logger.info(f"页面跳转到{route}!!!!!!!!!!!!!!!!!!!!!!!!!!")
self.mini.app.navigate_to(route)
def redirect_to_open(self, route):
"""关闭当前页面,重定向到应用内的某个页面,不允许跳转到 tabbar 页面"""
self.mini.app.redirect_to(route)
def relaunch_to_open(self, route):
"""关闭所有页面,打开到应用内的某个页面"""
self.mini.app.relaunch(route)
def switch_tab_open(self, route):
"""跳转到 tabBar 页面,会关闭其他所有非 tabBar 页面"""
self.mini.app.switch_tab(route)
def current_path(self) -> str:
"""获取当前页面route"""
return self.mini.page.path
# 多个元素定位
def get_elements(self, elements):
"""
:param elements: 传入的元素
:return: 返回多个元素
"""
try:
ele = self.mini.page.wait_for(elements)
if ele:
return self.page.get_elements(elements)
else:
self.mini.logger.error(f"找不到该元素{elements},无法点击!!")
raise ElementNotFoundException(f"未找到元素{elements}")
except AttributeError as e:
self.mini.logger.error(f"找不到该元素{elements},无法点击!!,报错原因{e}")
raise ElementNotFoundException(f"未找到元素{elements}")
# 单个元素定位
def get_element(self, element):
"""
:param element: 传入的元素
:return: 返回单个元素
"""
try:
ele = self.mini.page.wait_for(element)
if ele:
return self.page.get_element(element)
else:
self.mini.logger.error(f"找不到该元素{element},无法点击!!")
raise ElementNotFoundException(f"未找到元素{element}")
except AttributeError as e:
self.mini.logger.error(f"找不到该元素{element},无法点击!!,报错原因{e}")
raise ElementNotFoundException(f"未找到元素{element}")
# 判断某个元素是否存在
def element_is_exist(self, element):
self.mini.logger.info(f"目前在断言元素{element}")
bool = self.mini.page.element_is_exists(element)
try:
assert bool == True
except AssertionError:
self.mini.logger.error(f"断言失败,错误元素{element}")
raise AssertionError(f"断言失败,错误元素{element}")
return bool
# 判断某个元素是否存在, 只判断不断言
def element_is_exist_without_assert(self, element):
self.mini.logger.info(f"目前在断言元素{element}")
flag = self.mini.page.element_is_exists(element,max_timeout=3)
return flag
# 文本框输入
def send_key(self, element, text: str, with_confirm=None):
sleep(1)
try:
for i in range(6):
ele = self.mini.page.wait_for(element, max_timeout=3)
if ele:
self.mini.logger.info(f"目前在输入元素{element}")
ele_text = self.page.get_element(element)
if with_confirm is True:
ele_text.input(text, with_confirm=True)
else:
ele_text.input(text)
return
else:
self.mini.logger.error(f"找不到该元素{element},无法点击!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
raise RuntimeError(f"找不到该元素{element},超过等待时间,用例执行失败")
except AttributeError as e:
self.mini.logger.error(f"找不到该元素{element},无法输入!!!,,报错原因{e}")
raise ElementNotFoundException(f"未找到元素{element}")
def scroll(self, top=500):
"""
滚动到指定位置
:param scroll_top: 位置 px
:param duration: 滚动时长
:return:
"""
self.page.scroll_to(top)
# 操作弹窗
def handle_modal(self, text):
sleep(0.5)
self.mini.native.handle_modal(btn_text=text)
# 获取toast的文案,返回一个字典dict,断言需要用assertDictEqual
def asser_toast_text(self, text: str):
"""
:param text: 传入需要判断的文本
:return: 返回弹窗的文本,数据格式是dict
"""
before = Callback() # 创建接口调用前callback实例
after = Callback() # 创建接口调用前callback实例
callback = Callback() # 创建接口调用前callback实例
self.mini.app.hook_wx_method("showToast", before=before, callback=callback, after=after)
result_before = before.get_callback_result(timeout=15)
self.mini.logger.info(f"收到hook_wx_method回调,回调内容{result_before}")
assert text == result_before["title"]
def tap(self, element):
"""
:param element: 要点击的元素
:return:
"""
sleep(1)
try:
for i in range(6):
ele = self.mini.page.wait_for(element, max_timeout=3)
if ele:
self.mini.logger.info(f"目前在点击元素{element},点击方式Tap")
ele_tap = self.mini.page.get_element(element)
ele_tap.tap()
return
else:
self.mini.logger.error(f"找不到该元素{element},无法点击!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
raise RuntimeError(f"找不到该元素{element},超过等待时间,用例执行失败")
except AttributeError as e:
self.mini.logger.error(f"找不到该元素{element},无法输入!!!,,报错原因{e}")
raise
def picker_select(self, element, value):
"""
:param element: 时间选择器元素
:param value: 需要选择的时间,以多列选择器为例,所以value的值为数组[0,0,0,0,0],根据列数出入对应的下标
:return:
"""
try:
for i in range(6):
ele = self.mini.page.wait_for(element, max_timeout=3)
if ele:
self.mini.logger.info(f"目前在输入元素{element}")
self.mini.page.get_element(element).pick(value)
return
else:
self.mini.logger.error(f"找不到该元素{element},无法点击!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
raise RuntimeError(f"找不到该元素{element},超过等待时间,用例执行失败")
except AttributeError as e:
self.mini.logger.error(f"找不到该元素{element},无法输入!!!,,报错原因{e}")
raise
def show_action_sheets(self, item):
"""
:param item:输入选择的sheet的文字
:return:
"""
self.mini.native.handle_action_sheet(item)
def allow_send_subscribe_message(self, answer=False):
"""
允许发送订阅消息
:param answer: 默认True,可以False
:return:
"""
sleep(2)
self.mini.native.allow_send_subscribe_message(answer)
"""上传大图操作"""
def choose_img_single(self):
"""只能传本地的图片,将图片放在"data\\picture"路径下,先调用该方法,再模拟点击上传图片操作"""
file_Path = Path(f'{os.path.abspath(__file__)}')
curPath = os.path.abspath(os.path.dirname(os.path.dirname(file_Path)))
image_path = os.path.join(curPath, "data\\picture").replace("\\", "/")
try:
with open(os.path.join(image_path, "singleImg.png"), "rb") as fd:
c = fd.read()
image_b64data = base64.b64encode(c).decode("utf8")
ret = self.mini.app.mock_choose_image("singleImg.png", image_b64data)
res = self.mini.app.get_modals()
except IndexError:
self.mini.logger.info("没有监听到toast")
"""上传小图操作"""
def choose_img_multi(self):
"""只能传本地的图片,将图片放在"data\\picture"路径下,先调用该方法,再模拟点击上传图片操作"""
file_Path = Path(f'{os.path.abspath(__file__)}')
curPath = os.path.abspath(os.path.dirname(os.path.dirname(file_Path)))
image_path = os.path.join(curPath, "data\\picture").replace("\\", "/")
try:
items = []
for name in ["multi1.jpg", "multi2.jpg"]:
with open(os.path.join(image_path, name), "rb") as fd:
c = fd.read()
image_b64data = base64.b64encode(c).decode("utf8")
items.append({
"name": name,
"b64data": image_b64data
})
ret = self.mini.app.mock_choose_images(items)
res = self.mini.app.get_modals()
except IndexError:
self.mini.logger.info("没有监听到toast")
def get_last_toast(self):
"""
获取最后一个toast内容
:return:
"""
try:
sleep(3)
toast = self.mini.app.get_modals()
toast_title = toast[-1]
self.mini.logger.info(f"{toast[-1]}最后一个toast!!!!!!!!!!!!!!!!!!!!!!!!!")
except IndexError:
self.mini.logger.info("没有toast!!!!!!!!!!!!!!!!!!")
def set_storage(self):
"""
更新set_storage,但是没啥用,更新后需要重新编译小程序才生效
:return:
"""
arg = {
"key": "token",
"data": "XXXXXX"
}
info = self.mini.app.call_wx_method("setStorage", arg)