携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第27天,点击查看活动详
swipe滑动事件
driver.swipe(start_x: int, start_y: int, end_x: int, end_y: int, duration: int = 0)
start_x,start_y 起始点坐标
end_x,end_y 滑动终点坐标
duration:
定义通用滑动方法
app屏幕滑动原理
# start_x: int, start_y: int, end_x: int, end_y: int, duration: int = 0
def swip_page(driver, swip_type: str, count: int = 1, duration: int = 1000):
"""
滑屏操作
:param duration: 滑动操作执行时间
:param driver: app driver
:param swip_type: 滑动类型,必须是down up left right的一种
:param count: 滑动次数,默认是1次
:return:
# direction_position_map字典中每个key对应的value表示(start_x: int, start_y: int, end_x: int, end_y: int)
"""
width = driver.get_window_size()["width"]
height = driver.get_window_size()["height"]
direction_position_map = {
"up": (0.5 * width, 0.1 * height, 0.5 * width, 0.9 * height),
"down": (0.5 * width, 0.9 * height, 0.5 * width, 0.1 * height),
"left": (0.9 * width, 0.5 * height, 0.1 * width, 0.5 * height),
"right": (0.1 * width, 0.5 * height, 0.9 * width, 0.5 * height)
}
try:
for i in range(count):
driver.swipe(*direction_position_map[swip_type], duration)
except Exception as e:
raise e
scroll滑动事件
scroll是通过元素来进行滑动的。通常IOS系统使用的方式,可能部分安卓系统不支持
driver.scroll(origin_el, destination_el, duration: Optional[int] = None)
origin_el:开始元素
destination_el:滑动到的目标原酸
duration:滑动时间ms
边滑动边查找
场景:不是滑动页面,而是页面中的某个元素可以进行滑动,然后通过滑动元素找到需要的元素。
滑动元素,边滑动边查找元素内的某个元素:
def swip_and_find(driver, swip_element, target_element_info: tuple, swipe_type: str, duration: int = 1000):
"""
在一个
:param duration: 滑动操作时间
:param swipe_type: 滑动方向 down up left right
:param driver: appium driver
:param swip_element: 需要进行滑动的元素对象
:param target_element_info: 通过滑动需要找到的元素信息
:return:
"""
# 元素大小
width = swip_element.size["width"]
height = swip_element.size["height"]
# 元素的坐标(元素左上角坐标)
swip_ele_position_x = swip_element.location["x"]
swip_ele_position_y = swip_element.location["y"]
direction_position_map = {
"up": (swip_ele_position_x + 0.5 * width, swip_ele_position_y + 0.1 * height, swip_ele_position_x + 0.5 * width, swip_ele_position_y + 0.9 * height),
"down": (swip_ele_position_x + 0.5 * width, swip_ele_position_y + 0.9 * height, swip_ele_position_x + 0.5 * width, swip_ele_position_y + 0.1 * height),
"left": (swip_ele_position_x + 0.9 * width, swip_ele_position_y + 0.5 * height, swip_ele_position_x + 0.1 * width, swip_ele_position_y + 0.5 * height),
"right": (swip_ele_position_x + 0.1 * width, swip_ele_position_y + 0.5 * height, swip_ele_position_x + 0.9 * width, swip_ele_position_y + 0.5 * height)
}
while True:
# 获取页面资源,用于进行比对当前页面和上一个页面是否为同一个,以此结束循环
page = driver.page_source
try:
tar_ele = get_element(driver, target_element_info)
tar_ele.click()
print("找到了哦~~~~")
return True
except Exception as e:
print(e, "当前页面没有找到")
driver.swipe(*direction_position_map[swipe_type], duration)
if page == driver.page_source:
print("滑完了也没找到")
return False
drag_and_drop拖拽事件
场景:将一个元素拖动到另一个元素中,当元素不能被拖拽移动的时候,就相当于滑屏操作
drag_and_drop(origin_el: WebElement, destination_el: WebElement)