当前位置:网站首页>干货|移动端App自动化之触屏操作自动化
干货|移动端App自动化之触屏操作自动化
2022-06-09 16:03:00 【华为云】
工作中我们经常需要对应用的页面进行手势操作,比如滑动、长按、拖动等,AppiumDriver 为我们提供一个模拟手势操作的辅助类 TouchAction,可以通过它对手机屏幕进行手势操作。
具体用法参见链接:chromedriver下载地址与webview自动化关键代码 2
导入TouchAction
from appium.webdriver.common.touch_action import TouchAction常用的手势操作
TouchAction 提供的常用的手势操作有如下操作:
- Press 按下
- release 释放
- move_to 移动
- tap 点击
- longPress 长按
- wait 等待
- cancel 取消
- perform 执行
press
TouchAction 提供的 press( ) 方法可以实现对元素或者坐标的按下操作。通常会结合 release( ) 方法实现对某个元素的点击(包括按下和抬起两个动作)。
在某个控件上执行 press 操作,用法如下:
press(WebElement el)在坐标为(x,y)的点执行 press 操作,用法如下:
press(int x, int y)release
释放操作,可以结合其它的事件使用。代表该系列动作的一个结束标志。在某个控件上执行释放操作,用法如下:
release(WebElement el)也可以在上一个操作结束之后执行 release,不添加任何参数,用法如下:
release()move_to
以控件为目标,从一个点移动到该目标上,用法如下:
move_to(WebElement el)以(x,y)点为目标,从一个点移动到该目标,用法如下:
move_to(WebElement el, int x, int y)tap
在某个控件的中心点上点击一下,用法如下:
tap(WebElement el)以(x,y)坐标点为目标点击,用法如下:
tap(int x, int y)以控件 el 的左上角为基准,沿着 x 轴向右移动 x 单位,沿着 y 轴向下移动 y 单位。在该点上点击,用法如下:
tap(WebElement el, int x, int y)longpress
长按某一控件,用法如下:
long_press(WebElement el)以(x,y)点为目标实现长按,用法如下:
long_press(int x, int y)在控件的左上角的 x 坐标偏移 x 单位,y 左边偏移 y 单位的坐标上长按。用法如下:
long_press(WebElement el, int x, int y)wait
等待,单位为毫秒。可以在操作事件的过程中,短暂的停留几秒再继续操作。用法如下:
wait(long timeout)cancel
可以取消执行事件链中的事件,用法如下:
cancel()perform
执行事件链中的事件,一般最后会调用这个方法,顺序执行事件链中的动作。用法如下:
perform()打开测试应用,从元素 “Views” 文本滑动到 “Accessibility” 元素,创建一个测试文件 test_touchaction.py,代码如下:
测试 app 官方下载地址:https://github.com/appium/appium/tree/master/sample-code/apps
#!/usr/bin/env python# -*- coding: utf-8 -*-from appium import webdriverfrom appium.webdriver.common.touch_action import TouchActionclass TestTouchAction(): def setup(self): caps = {} caps['platformName'] = 'Android' caps['platformVersion'] = '6.0' caps['deviceName'] = 'emulator-5554' caps['appPackage'] = 'io.appium.android.apis' caps['appActivity'] = 'io.appium.android.apis.ApiDemos' self.driver = webdriver.Remote(\ "http://127.0.0.1:4723/wd/hub", caps) self.driver.implicitly_wait(5) def teardown(self): self.driver.quit() def test_touchaction_unlock(self): # 点击 Views el1 = self.driver.find_element_by_accessibility_id( "Views") # 点击 Accessibility el2 = self.driver.find_element_by_accessibility_id( "Accessibility") # TouchAction 滑动操作 action = TouchAction(self.driver) action.press(el1).wait(100).move_to\ (el2).wait(100).release().perform()上面代码可以看出,首先定位两个元素 el1 与 el2 ,分别作为滑动过程中起点元素和终点元素,将我们滑动的起点元素与终点元素找到之后,创建一个 TouchAction 对象,调用里面的 press( ) 方法实现起点元素的点击,使用 wait( ) 方法在事件之间添加等待,使用 move_to( ) 方法完成手势的移动操作,然后调用 release( ) 方法来完成手势的抬起,最后调用 perform( ) 方法对添加到 TouchAction 中的事件链顺序执行。
边栏推荐
- 代码。。。。
- 618's money saving technology strategy is coming - experience the scene and get a 10 yuan cat super card!
- 关闭StackExchange等平台的privacy收集窗口
- R language plot visualization: use the plot visualization model to create a grid by using the classification contour line (contour line) of the entire data space and meshgrid, where the distance betwe
- Laravel8框架七牛云上传
- LINQ left join example
- 检测ip地址库内是否存在指定的ip
- Moonbeam基金会与Arrington Capital共同启动1亿美金的生态增长基金以促进Moonbeam生态成长
- C语言去掉字符串末尾的空白字符
- Ueeditor image cross domain upload solution
猜你喜欢

nth-child选择器

Customizing the in app keyboard in fluent

从 0 到 1,探究百亿流量验证下的 MVVM 框架设计

Phalapi framework improvement scheme: manage multiple API applications on a set of phalapi system

Without regret, appium automation environment is built perfectly

QueryList异步抓取网页数据

Senior openstacker - Bloomberg, vexxhost upgraded to gold member of openinfra Foundation
![[SEETF]Super Secure Requests Forwarder](/img/b4/f3211c28a2474973e231a69d9a4ca8.png)
[SEETF]Super Secure Requests Forwarder

看完就会,从抓包到接口测试的全过程解析

还在从零开始搭建项目?这款升级版快速开发脚手架值得一试~
随机推荐
Redis implements paging
Execution strategy of application software efficiency test
Are cash financial products breakeven?
Win10找不到飞行模式开关怎么办?
元宇宙可以给正在陷入到瓶颈期的互联网打开全新的思考窗口
[ctfshow singles cup]web writeup & learn the basic usage of SED awk
[ctfshow 单身杯]web writeup&&学习一下sed awk基本用法
Introduction to database storage directory structure
R language plot visualization: plot visualization horizontal boxplot
现金类的理财产品是保本的吗?
C语言去掉字符串末尾的空白字符
R language plot visualization: use the plot visualization model to create a grid by using the classification contour line (contour line) of the entire data space and meshgrid, where the distance betwe
站长工具浏览器SEO插件-站长必备工具 SEO网站排名 快速查看网站数据
资深OpenStacker - 彭博、Vexxhost升级为OpenInfra基金会黄金成员
海通证券是安全的吗
Querylist asynchronously fetches web page data
Solution de téléchargement d'images ueditor entre domaines
一个时代的终结!吴恩达经典《机器学习》课程本月关闭注册...然而
Why can't Google search page infinite?
virtualBox 虚拟机网卡设置