简介
我们在做移动端自动化测试的时候,有些按钮在当前屏幕不显示,需要滑动几次屏幕后才会出现或者有些操作需要滑动屏幕,这时候,我们需要使用代码来模拟手指的滑动,本次我们先讲解Appium 里面滑动swipe方法使用
手机上坐标说明
手机从左上角开始为0,横着的是x轴,竖着的是y轴
swipe介绍
说明
从一个坐标位置滑动到另一个坐标位置,只能是两个点之间的滑动。
方法详情
def swipe(self: T, start_x: int, start_y: int, end_x: int, end_y: int, duration: int = 0) -> T:
"""Swipe from one point to another point, for an optional duration.
Args:
start_x: x-coordinate at which to start
start_y: y-coordinate at which to start
end_x: x-coordinate at which to stop
end_y: y-coordinate at which to stop
duration: time to take the swipe, in ms.
Usage:
driver.swipe(100, 100, 100, 400)
Returns:
Union['WebDriver', 'ActionHelpers']: Self instance
"""
actions = ActionChains(self)
actions.w3c_actions = ActionBuilder(self, mouse=PointerInput(interaction.POINTER_TOUCH, "touch"))
actions.w3c_actions.pointer_action.move_to_location(start_x, start_y)
actions.w3c_actions.pointer_action.pointer_down()
actions.w3c_actions.pointer_action.pause(duration / 1000)
actions.w3c_actions.pointer_action.move_to_location(end_x, end_y)
actions.w3c_actions.pointer_action.release()
actions.perform()
return self
核心参数说明:
:Args:
- start_x - 开始滑动的x坐标
- start_y - 开始滑动的y坐标
- end_x - 结束点x坐标
- end_y - 结束点y坐标
- duration - 持续时间,单位毫秒,默认为 0 ms
实战
-
从坐标点(400, 700)滑动到坐标点(400,200),duration=0
driver.swipe(start_x=400, start_y=700, end_x=400, end_y=200)
-
从坐标点(400, 700)滑动到坐标点(400,200),duration=500
driver.swipe(start_x=400, start_y=700, end_x=400, end_y=200,duration=500)
-
从坐标点(400, 700)滑动到坐标点(400,200),duration=2000
driver.swipe(start_x=400, start_y=700, end_x=400, end_y=200,duration=2000)
-
从坐标点(400, 700)滑动到坐标点(400,600),duration=500
driver.swipe(start_x=400, start_y=700, end_x=400, end_y=200,duration=500)
-
从坐标点(400, 700)滑动到坐标点(400,400),duration=500
driver.swipe(start_x=400, start_y=700, end_x=400, end_y=200,duration=500)
-
从坐标点(400, 700)滑动到坐标点(400,200),duration=500
driver.swipe(start_x=400, start_y=700, end_x=400, end_y=200,duration=500)
结论
- 距离相同时,持续时间越长,惯性越小
- 持续时间相同时,手指滑动的距离越大,实际滑动的距离也就越大
以上为内容纯属个人理解,如有不足,欢迎各位大神指正!
如果觉得文章不错,欢迎关注微信公众号,微信公众号定期推送相关测试技术文章