在移动端应用中,基于简便的原因,用户通常会倾向于使用滑动操作来达到与应用程序中的控件进行交互的,这使得滑动成为自动化测试中频繁使用的关键动作。在 Appium 中提供了多种方式来实现模拟用户的滑动屏幕动作。
移动端应用中的滑动场景,大致有如下几种类型:
从一个点滑动到另一个点,可选择持续时间,具有滑动的惯性。需要的参数如下:
Python 实现
def test_swipe_views(self):
# 从一个点滑动到另一个点,可选择持续时间
'''
start_x: 开始坐标 x
start_y: 开始坐标 y
end_x: 结束坐标 x
end_y: 结束坐标 y
duration: 持续时间
'''
# 点击进入 Views 界面
self.driver.find_element(AppiumBy.ACCESSIBILITY_ID, 'Views').click()
# 获取窗口的宽和高
height = self.driver.get_window_size().get('height')
weight = self.driver.get_window_size().get('width')
# 执行滑动操作
self.driver.swipe(start_x=weight * 0.5,
start_y=height * 0.8,
end_x=weight * 0.5,
end_y=height * 0.2, duration=2000)
ele = self.driver.find_element(AppiumBy.ACCESSIBILITY_ID, 'Picker')
# 断言滑动成功 找到元素
assert ele.text == 'Picker'
从一个元素滑动到另一个元素,具有滑动的惯性。需要的参数如下:
Python 实现
def test_scroll_ele(self):
'''
从一个元素滑动到另一个元素
:return:
'''
# 点击进入 Views 界面
self.driver.find_element(AppiumBy.ACCESSIBILITY_ID, 'Views').click()
# 滑动起始元素
image_btn = self.driver.find_element(AppiumBy.ACCESSIBILITY_ID, 'ImageButton')
# 滑动结束元素
button = self.driver.find_element(AppiumBy.ACCESSIBILITY_ID, 'Buttons')
# 执行滑动操作
self.driver.scroll(image_btn, button, duration=2000)
list_ele = self.driver.find_element(AppiumBy.ACCESSIBILITY_ID, 'Picker')
assert list_ele.text == 'Picker'
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。