技术分享 触屏操作自动化

导入TouchAction

from appium.webdriver.common.touch_action import TouchAction

  • Java 版本

import io.appium.java_client.TouchAction;

常用的手势操作

press 按下

TouchAction 提供的常用的手势操作有如下操作:

  • press 按下
  • release 释放
  • move_to/moveTo 移动
  • tap 点击
  • long_press/longPress 长按
  • wait 等待
  • cancel 取消
  • perform 执行

TouchAction 提供的 press( ) 方法可以实现对元素或者坐标的按下操作。通常会结合 release( ) 方法实现对某个元素的点击(包括按下和抬起两个动作)。

在某个控件上执行 press 操作,用法如下:

  • Python 版本

按下某个元素,用法如下:

press(WebElement el)

在坐标为(x,y)的点执行 press 操作,用法如下:

press(int x, int y)

  • Java 版本

在坐标为(x,y)的点执行 press 操作,用法如下:

press(int x, int y)

release 释放

释放操作,可以结合其它的事件使用。代表该系列动作的一个结束标志。在某个控件上执行释放操作,用法如下:

  • Python 版本

release(WebElement el)

也可以在上一个操作结束之后执行 release,不添加任何参数,用法如下:

release()

  • Java 版本

release()

移动

以控件为目标,从一个点移动到该目标上,用法如下:

  • Python 版本

move_to(WebElement el)

以(x,y)点为目标,从一个点移动到该目标,用法如下:

move_to(WebElement el, int x, int y)

  • Java 版本

以(x,y)点为目标,从一个点移动到该目标,用法如下:

moveTo(WebElement el, int x, int y)

tap 点击

在某个控件的中心点上点击一下,用法如下:

  • Python 版本

tap(WebElement el)

以控件 el 的左上角为基准,沿着 x 轴向右移动 x 单位,沿着 y 轴向下移动 y 单位。在该电脑上点击,用法如下:

tap(WebElement el, int x, int y)

以(x,y)坐标点为目标点击,用法如下:

tap(int x, int y)

  • Java版本

只提供坐标点击,用法如下:

tap(int x, int y)

长按

长按某一控件,用法如下:

  • Python 版本

long_press(WebElement el)

以(x,y)点为目标实现长按,用法如下:

long_press(int x, int y)

在控件的左上角的 x 坐标偏移 x 单位,y 左边偏移 y 单位的坐标上长按。用法如下:

long_press(WebElement el, int x, int y)

  • Java 版本

只提供坐标点击,用法如下:

longPress(int x, int y)

等待

等待,单位为秒。可以在操作事件的过程中,短暂地停留几秒再继续操作。用法如下:

  • Python 版本

wait(long timeout)

  • Java 版本

wait(long timeout)

cancel 取消

可以取消执行事件链中的事件,用法如下:

  • Python 版本

cancel()

  • Java 版本

cancel()

执行 perform

执行事件链中的事件,一般最后会调用这个方法,顺序执行事件链中的动作。用法如下:

  • Python 版本

perform()

  • Java 版本

perform()

案例

打开测试应用,从元素 “Views” 文本滑动到 “Accessibility” 元素,创建一个测试文件代码如下:

测试 app 官方下载地址:appium/sample-code/apps at master · appium/appium · GitHub

#!/usr/bin/env python# -*- coding: utf-8 -*-# 测试文件 test_touchaction.pyfrom 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()public class TouchActionTest { static AppiumDriver driver; @BeforeAll public static void beforeAll() throws MalformedURLException { DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability(“deviceName”, “emulator-5554”); caps.setCapability(“platformName”, “Android”); caps.setCapability(“appPackage”, “io.appium.android.apis”); caps.setCapability(“appActivity”, “io.appium.android.apis. ApiDemos”); URL appiumServer = new URL(“http://127.0.0.1:4723/wd/hub”); driver = new AndroidDriver(appiumServer, caps); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); } @Test void test() { // 创建 TouchAction 对象 TouchAction action = new TouchAction(driver); // TouchAction 滑动操作 action.press(PointOption.point((int) (width * 0.5), (int) (height * 0.8))).waitAction(WaitOptions. waitOptions(Duration.ofSeconds(2))).moveTo( PointOption.point((int) (width * 0.5), (int) (height * 0.2))).release().perform(); }}

以上两段代码实现了相同的操作,创建了一个 TouchAction 对象,调用里面的 press() 方法实现起点元素的点击,使用 wait() 方法在事件之间添加等待,使用 move_to()/moveTo() 方法是完成手势的移动操作,然后调用 release() 方法来完成手势的抬起,最后调用 perform() 方法对添加到 TouchAction 中的事件链顺序执行。

郑重声明:本文内容及图片均整理自互联网,不代表本站立场,版权归原作者所有,如有侵权请联系管理员(admin#wlmqw.com)删除。
上一篇 2022年6月22日 15:21
下一篇 2022年6月22日 15:22

相关推荐

联系我们

联系邮箱:admin#wlmqw.com
工作时间:周一至周五,10:30-18:30,节假日休息