Fetch the repository succeeded.
"""
__author__ = '霍格沃兹测试开发学社'
__desc__ = '更多测试开发技术探讨,请访问:https://ceshiren.com/t/topic/15860'
"""
import os
from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.webdriver import WebDriver
from selenium.common import NoSuchElementException
from app_auto_test.app_po.utils.errpr_handles import black_wrapper
from app_auto_test.app_po.utils.log_utils import logger
from app_auto_test.app_po.utils.utils import Utils
class BasePage:
IMPLICITLY_WAIT = 10
def __init__(self, driver: WebDriver = None):
self.driver = driver
@black_wrapper
def find(self, by, value):
'''
查找元素,返回元素
:param by: 定位方法
:param value: 元素定位表达式
:return: 定位到的元素对象
'''
return self.driver.find_element(by, value)
def find_and_click(self, by, value):
'''
查找元素并点击
:param by: 定位方法
:param value: 元素定位表达式
'''
self.find(by, value).click()
def find_and_sendkeys(self, text, by, value):
'''
查找元素并输入
:param text: 输入的文本
:param by: 定位方法
:param value: 元素定位表达式
'''
self.find(by, value).send_keys(text)
def set_implicitly_wait(self, time=1):
'''
设置隐式等待
:param time: 隐式等待的时间
'''
self.driver.implicitly_wait(time)
def swipe_find(self, text, max_num=5):
"""滑动查找一个文本 text
如果没有找元素,完成滑动操作
如果找到了,则返回元素
"""
self.driver.implicitly_wait(1)
for num in range(max_num):
try:
# find_element() 每次调用这个方法的时候,都会激活隐式等待,也就是在隐式等待的时长之内,动态的找元素
element = self.find(AppiumBy.XPATH, f"//*[@text='{text}']")
# 找到了元素之后,再设置回全局的隐式等待时长 10秒
self.driver.implicitly_wait(self.IMPLICITLY_WAIT)
return element
except NoSuchElementException as e:
print("未找到元素")
# 滑动 从下向上
size = self.driver.get_window_size()
# 'width', 'height'
width = size.get("width")
height = size.get("height")
startx = width / 2
starty = height * 0.8
endx = startx
endy = height * 0.2
duration = 2000
self.driver.swipe(startx, starty, endx, endy, duration)
if num == max_num - 1:
# 没有找到的情况。在抛出异常之前,把这个隐式等待改回全局的等待时长 10 秒
self.driver.implicitly_wait(self.IMPLICITLY_WAIT)
# 执行到最大次数,仍然没有找到这个文本,则抛出异常
raise NoSuchElementException(f"找了{num} 次,未找到{text}")
def get_toast_tips(self):
'''
获取 toast 文本
:return: 获取到的文本
'''
return self.driver.find_element(AppiumBy.XPATH, "//*[@class='android.widget.Toast']").text
def go_back(self, num=5):
'''
执行返回操作
:param num: 返回的次数
'''
for i in range(num):
self.driver.back()
def screenshot(self, root_path):
'''
截图
:param path: 截图保存路径
'''
logger.info(f"没有找到元素,截图")
# 以当前时间命名的截图
image_name = Utils.get_current_time() + ".png"
# 拼接当前要输出图片的路径
image_dir_path = os.sep.join([root_path, '..', f'images/'])
if not os.path.isdir(image_dir_path):
os.mkdir(image_dir_path)
image_path = image_dir_path + image_name
logger.info(f"截图路径为:{image_path}")
self.driver.save_screenshot(image_path)
return image_path
def save_page_source(self, root_path):
'''
保存页面源码
:return: 返回源码文件路径
'''
logger.info(f"没有找到元素,保存页面源码")
pagesource_name = Utils.get_current_time() + "_page_source.xml"
pagesource_dir_path = os.sep.join([root_path, '..', f'page_source/'])
if not os.path.isdir(pagesource_dir_path):
os.mkdir(pagesource_dir_path)
pagesource_path = pagesource_dir_path + pagesource_name
logger.info(f"page source 路径为:{pagesource_path}")
with open(pagesource_path, "w", encoding="u8") as f:
f.write(self.driver.page_source)
return pagesource_path
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。