前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Appium滑动操作

Appium滑动操作

作者头像
清风穆云
发布2021-08-09 10:52:37
9840
发布2021-08-09 10:52:37
举报
文章被收录于专栏:QA一隅QA一隅

应用背景

在app应用日常使用过程中,会经常用到在屏幕滑动操作。如刷朋友圈上下滑操作、浏览图片左右滑动操作等。在自动化脚本该如何实现这些操作呢?

在Appium中模拟用户滑动操作需要使用swipe方法,该方法定义如下:

代码语言:javascript
复制
    def swipe(self, start_x, start_y, end_x, end_y, duration=None):
        """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 - (optional) time to take the swipe, in ms.

        :Usage:
            driver.swipe(100, 100, 100, 400)

滑动解析

滑动主要分为:

  1. 水平滑动
  2. 垂直滑动
  3. 任意方向滑动

滑动轨迹图如下:

实践应用

测试场景
  • 安装启动考研帮,手动向水平左滑动首页引导页面。
  • 点击“立即体验”进入登录页面。
代码实现

swipe.py

代码语言:javascript
复制
from time import sleep
from find_element.capability import driver

#获取屏幕尺寸
def get_size():
    x=driver.get_window_size()['width']
    y=driver.get_window_size()['height']
    return x,y

#显示屏幕尺寸(width,height)
l=get_size()
print(l)

#向左滑动
def swipeLeft():
    l=get_size()
    x1=int(l[0]*0.9)
    y1=int(l[1]*0.5)
    x2=int(l[0]*0.1)
    driver.swipe(x1,y1,x2,y1,1000)

if __name__ == '__main__':

    for i in range(2):
        swipeLeft()
        sleep(0.5)

    driver.find_element_by_id('com.tal.kaoyan:id/activity_splash_guidfinish').click()

其他滑动

把垂直上下滑动以及向右滑动的也封装并实践。

  • def swipeUp()
  • def swipeDown()
  • def swipeRight()
代码实现
代码语言:javascript
复制
def swipeUp():
    l = get_size()
    x1 = int(l[0] * 0.5)
    y1 = int(l[1] * 0.95)
    y2 = int(l[1] * 0.35)
    driver.swipe(x1, y1, x1, y2, 1000)

def swipeDown():
    l=get_size()
    x1 = int(l[0] * 0.5)
    y1 = int(l[1] * 0.35)
    y2 = int(l[1] * 0.85)
    driver.swipe(x1, y1, x1, y2, 1000)

def swipeRight():
    l=get_size()
    y1 = int(l[1] * 0.5)
    x1 = int(l[0] * 0.25)
    x2 = int(l[0] * 0.95)
    driver.swipe(x1, y1, x2, y1, 1000)
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-07-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 QA一隅 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 应用背景
  • 滑动解析
  • 实践应用
    • 测试场景
      • 代码实现
      • 其他滑动
        • 代码实现
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档