Appium自动化之滑动操作

在app应用日常使用过程中,会经常用到在屏幕滑动操作。如上下滑动操作、左右滑动操作等。在自动化脚本该如何实现这些操作呢?下面通过一个实例来实现。

实例

测试场景

  • 安装启动鼠贷金融,手动向水平左滑动首页引导页面。
  • 点击“立即体验”进入登录页面。

测试环境

  • appium版本:1.7.2
  • 测试设备:Android 7.0
  • Python:3.5
  • 测试App:鼠贷金融 Android app V4.2.9

实现步骤

  1. 首先创建一个工程目录
  2. 在工程目录下创建一个APP目录用来存放app安装包
  3. 在工程目录下创建test_app.py文件

代码实现

test_app.py

from appium import webdriver
from time import sleep
import os

# 获取项目的根目录路径
pro_path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__))))
print(pro_path)

# 获取app安装包所在路径
app_path = os.path.join(pro_path, "app", "com.jinding.shuqian_4.2.9_liqucn.com.apk")
print(app_path)

# 真机
desired_caps = {
  "platformName": "Android",
  "platformVersion": "7.0",
  "deviceName": "Honor NOTE 8",
  "udid": "4556R1672646446",
  "appPackage": "com.jinding.shuqian",
  "appActivity": "com.jinding.shuqian.WelcomeActivity",
  "app": app_path

}

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
sleep(2)

print('检测是否是第一次启动')
try:
  # 定位我的
  w = driver.find_element_by_id('com.jinding.shuqian:id/rb_center')
except Exception as e:
  print('是第一次启动,需要授权')
  try:
    while True:
      # 定位授权按钮
      accredit = driver.find_element_by_id('com.android.packageinstaller:id/permission_allow_button')
      if accredit:
        accredit.click()  # 点击授权
  except Exception:
    print('已经全部授权')

else:
  w.click()  # 点击我的

#获取屏幕尺寸
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)

#向左滑动2次
for i in range(2):
    swipeLeft()
    sleep(0.5)

# 点击立即体验
driver.find_element_by_id('com.jinding.shuqian:id/imageView_guide_join').click()

补充定位动态元素,坐标


def go_to_optional(self, timeout=5):
    # 获取当前设备元素x y 轴宽高
    location = self.driver.get_window_size()
    width = location['width']
    height = location['height']
    # 获取相对定位的宽高, 通过uiautomatorviewer拿到要定位的元素宽高除以屏幕的最大宽高,
    #然后乘以当前设备获取到的最大宽高所得到的值就是当前设备要定位元素的宽高了。
    xd_x = (209 / 750) * width + (209 / 750) * (width - 750)
    xd_y = (1304 / 1334) * height + (1304 / 1334) * (height - 1334)
    # 拿到相对定位的宽高 减去 原始设备的宽高 得到的偏移量
    offset_x = xd_x - 209
    offset_y = xd_y - 1304
    for i in range(timeout):
        #循环5秒 判断他们之间的偏移量是否在2和-2之间 如果是的话就点击
        if (offset_x < 2 or offset_x > -2) and (offset_y < 2 or offset_y > -2):
            self.show_wait_find_element(self.optional_tab).click()
            break
        else:
            sleep(1)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,219评论 25 708
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 12,913评论 2 59