在android测试过程中,会遇到要点击一下屏幕的需求。

在appium旧版本使用下面代码点击android屏幕,没有报错。
Map tap = new HashMap(); 
tap.put("tapCount", new Double(2));
tap.put("touchCount", new Double(1)); 
tap.put("duration", new Double(0.5)); 
tap.put("x", new Double(300)); 
tap.put("y", new Double(300)); 
driver.executeScript("mobile: tap", tap); 
driver.executeAsyncScript(script, args);

但是在升级appium 新版本后,使用这段代码,会报错误:
org.openqa.selenium.WebDriverException: Method has not yet been implemented (WARNING: The server did not provide any stacktrace information)

所以这个方法不能再使用了,如果用driver.tap点击屏幕,
driver.tap(1, 100, 100, 200); 
会有比较高几率的报错,报错误内容:An unknown server-side error occurred
这样报错不利于自动化长期运行

查找资料,appium使用如下这个方法点击屏幕,没有报错而且来自appium官方资料:
TouchAction action = new TouchAction(driver);
action.tap(100, 100).perform();

解决了点击屏幕报错的问题

https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/touch-actions.md

Automating mobile gestures

While the Selenium WebDriver spec has support for certain kinds of mobile interaction, its parameters are not always easily mappable to the functionality that the underlying device automation (like UIAutomation in the case of iOS) provides. To that end, Appium implements the new TouchAction / MultiAction API defined in the newest version of the spec (https://dvcs.w3.org/hg/webdriver/raw-file/tip/webdriver-spec.html#multiactions-1). Note that this is different from the earlier version of the TouchAction API in the original JSON Wire Protocol.

These APIs allow you to build up arbitrary gestures with multiple actuators. Please see the Appium client docs for your language in order to find examples of using this API.

An Overview of the TouchAction / MultiAction API

TouchAction

TouchAction objects contain a chain of events.

In all the appium client libraries, touch objects are created and are given a chain of events.

The available events from the spec are:

  • press
  • release
  • moveTo
  • tap
  • wait
  • longPress
  • cancel
  • perform

Here's an example of creating an action in pseudocode:

  1. TouchAction().press(el0).moveTo(el1).release()

The above simulates a user pressing down on an element, sliding their finger to another position, and removing their finger from the screen.

Appium performs the events in sequence. You can add a wait event to control the timing of the gesture.

moveTo coordinates are relative to the current position. For example, dragging from 100,100 to 200,200 can be achieved by:

  1. .press(100,100) // Start at 100,100
  2. .moveTo(100,100) // Increase X & Y by 100 each, ending up at 200,200

The appium client libraries have different ways of implementing this, for example: you can pass in coordinates or an element to a moveTo event. Passing both coordinates and an element will treat the coordinates as relative to the element's position, rather than relative to the current position.

Calling the perform event sends the entire sequence of events to appium, and the touch gesture is run on your device.

Appium clients also allow one to directly execute a TouchAction through the driver object, rather than calling the performevent on the TouchAction object.

In pseudocode, both of the following are equivalent:

  1. TouchAction().tap(el).perform()
  2. driver.perform(TouchAction().tap(el))

MultiTouch

MultiTouch objects are collections of TouchActions.

MultiTouch gestures only have two methods, add, and perform.

add is used to add another TouchAction to this MultiTouch.

When perform is called, all the TouchActions which were added to the MultiTouch are sent to appium and performed as if they happened at the same time. Appium first performs the first event of all TouchActions together, then the second, etc.

Pseudocode example of tapping with two fingers:

  1. action0 = TouchAction().tap(el)
  2. action1 = TouchAction().tap(el)
  3. MultiAction().add(action0).add(action1).perform()

Bugs and Workarounds

An unfortunate bug exists in the iOS 7.0 - 8.x Simulators where ScrollViews, CollectionViews, and TableViews don't recognize gestures initiated by UIAutomation (which Appium uses under the hood for iOS). To work around this, we have provided access to a different function, scroll, which in many cases allows you to do what you wanted to do with one of these views, namely, scroll it!

Scrolling

To allow access to this special feature, we override the execute or executeScript methods in the driver, and prefix the command with mobile:. See examples below:

To scroll, pass direction in which you intend to scroll as parameter.

  1. // javascript
  2. driver.execute('mobile: scroll', {direction: 'down'})
  1. // java
  2. JavascriptExecutor js = (JavascriptExecutor) driver;
  3. HashMap<String, String> scrollObject = new HashMap<String, String>();
  4. scrollObject.put("direction", "down");
  5. js.executeScript("mobile: scroll", scrollObject);
  1. # ruby
  2. execute_script 'mobile: scroll', direction: 'down'
  1. # python
  2. driver.execute_script("mobile: scroll", {"direction": "down"})
  1. // c#
  2. Dictionary<string, string> scrollObject = new Dictionary<string, string>();
  3. scrollObject.Add("direction", "down");
  4. ((IJavaScriptExecutor)driver).ExecuteScript("mobile: scroll", scrollObject));
  1. $params = array(array('direction' => 'down'));
  2. $driver->executeScript("mobile: scroll", $params);

Sample to scroll using direction and element.

  1. // javascript
  2. driver.execute('mobile: scroll', {direction: 'down', element: element.value.ELEMENT});
  1. // java
  2. JavascriptExecutor js = (JavascriptExecutor) driver;
  3. HashMap<String, String> scrollObject = new HashMap<String, String>();
  4. scrollObject.put("direction", "down");
  5. scrollObject.put("element", ((RemoteWebElement) element).getId());
  6. js.executeScript("mobile: scroll", scrollObject);
  1. # ruby
  2. execute_script 'mobile: scroll', direction: 'down', element: element.ref
  1. # python
  2. driver.execute_script("mobile: scroll", {"direction": "down", element: element.getAttribute("id")})
  1. // c#
  2. Dictionary<string, string> scrollObject = new Dictionary<string, string>();
  3. scrollObject.Add("direction", "down");
  4. scrollObject.Add("element", <element_id>);
  5. ((IJavaScriptExecutor)driver).ExecuteScript("mobile: scroll", scrollObject));
  1. $params = array(array('direction' => 'down', 'element' => element.GetAttribute("id")));
  2. $driver->executeScript("mobile: scroll", $params);

Automating Sliders

iOS

  • Java
  1. // java
  2. // slider values can be string representations of numbers between 0 and 1
  3. // e.g., "0.1" is 10%, "1.0" is 100%
  4. WebElement slider = driver.findElement(By.xpath("//window[1]/slider[1]"));
  5. slider.sendKeys("0.1");

Android

The best way to interact with the slider on Android is with TouchActions.

appium点击屏幕(手势)的更多相关文章

  1. Appium 点击屏幕

    由于版本变更,appium 点击屏幕方法已经改变, TouchAction action = new TouchAction(driver); Duration duration = Duration ...

  2. Appium 点击Android屏幕

    用driver.tap(1, 10, 10, 800); 点击屏幕,经常提示:An unknown server-side error occurred while processing the co ...

  3. navigationcontroller和navigationbar和navigationitem之间的区别以及不用nib实现点击屏幕关闭虚拟键盘20130911

    1.UIViewController UIView的关系. UIView是视图,UIViewController是视图控制器,两者之间是从属关系,当创建一个UIViewController的时候,一般 ...

  4. Vuforia点击屏幕自动对焦,过滤UGUI的按钮

    //点击屏幕自对对焦 #if UNITY_EDITOR )) #elif UNITY_ANDROID || UNITY_IPHONE && Input.GetTouch().phase ...

  5. android 点击屏幕关闭 软键盘

    //点击屏幕 关闭输入弹出框 @Override public boolean onTouchEvent(MotionEvent event) { InputMethodManager im = (I ...

  6. HTML5实现屏幕手势解锁

    HTML5实现屏幕手势解锁(转载) https://github.com/lvming6816077/H5lockHow to use? <script type="text/java ...

  7. iOS 点击return或者点击屏幕键盘消失

    //定义两个文本框 UITextField *textName; UITextField *textSummary; //点击return 按钮 去掉 -(BOOL)textFieldShouldRe ...

  8. ios-点击屏幕,隐藏键盘

    ios-点击屏幕,隐藏键盘 - (void)getFirstRegist{ //结束键盘编辑 __weak typeof(self)weakSelf = self; UITapGestureRecog ...

  9. unity3d点击屏幕选中物体

    原文  http://blog.csdn.net/mycwq/article/details/19906335 前些天接触unity3d,想实现点击屏幕选中物体的功能.后来研究了下,实现原理就是检测从 ...

随机推荐

  1. 工欲善其事必先利其器之Xcode高效插件和舒适配色

    功能强大的Xcode再配上高效的插件,必会让你的开发事半功倍.直接进入正题. Xcode插件安装方式: 1.github下载插件然后用xcode打开运行一遍,然后重启xcode. 2.安装插件管理Al ...

  2. python中的几种集成分类器

    from sklearn import ensemble 集成分类器(ensemble): 1.bagging(ensemble.bagging.BaggingClassifier) 对随机选取的子样 ...

  3. 《作业控制系列》-“linux命令五分钟系列”之十

    本原创文章属于<Linux大棚>博客. 博客地址为http://roclinux.cn. 文章作者为roc 希望您能通过捐款的方式支持Linux大棚博客的运行和发展.请见“关于捐款” == ...

  4. centos 安装vnc服务

    1.安装tigervnc-server yum install tigervnc-server 2.启动vnc服务 vncserver:1 [错误提示待解决bad display name " ...

  5. Android Framework------之Keyguard 简单分析

    前面对于MediaPlayer的系统研究,刚刚开始,由于其他原因现在要先暂停一下.这次要看的模块是android 4.2 系统中的Keyguard模块.在接触之后才发现,android4.2的keyg ...

  6. Android 即时语音聊天工具 开发

    使用融云SDK 1. 功能需求分析 1.1 核心功能需求: * 即时通讯 * 文字聊天 * 语音聊天 1.2 辅助功能需求: * 注册.登录 * 好友添加功能 * 好友关系管理 2. 融云即时通讯平台 ...

  7. 黑马程序员——利用swap函数研究C的指针

    ------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- 设计3个函数,分别实现已下功能: 交换两个整数 交换两个整形指针 交换任意两个同类型的变量 #i ...

  8. SOA和微服务

    SOA和微服务 SOA和微服务到底是什么关系? 说实话,我确实不明白SOA和微服务到底有什么本质上的区别,两者说到底都是对外提供接口的一种架构设计方式.我倒觉得微服务其实就是随着互联网的发展,复杂的平 ...

  9. Java中间件

    传统的HTML已经满足不了如今web系统的诸多的功能需求,建立一个交互式的Web,便诞生了各种Web开发语言,如ASP,JSP,PHP等,这些语言与传统的语言有着密切的联系,如JSP基于Java语言. ...

  10. GO:格式化代码

    http://www.ituring.com.cn/article/39380 Go 开发团队不想要 Go 语言像许多其它语言那样总是在为代码风格而引发无休止的争论,浪费大量宝贵的开发时间,因此他们制 ...