在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:

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:

.press(100,100) // Start at 100,100
.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:

TouchAction().tap(el).perform()

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:

action0 = TouchAction().tap(el)
action1 = TouchAction().tap(el)
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.

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

Sample to scroll using direction and element.

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

Automating Sliders

iOS

  • Java
// java
// slider values can be string representations of numbers between 0 and 1
// e.g., "0.1" is 10%, "1.0" is 100%
WebElement slider = driver.findElement(By.xpath("//window[1]/slider[1]"));
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. 理解pkg-config工具

    你在 Unix 或 Linux 下开发过软件吗?写完一个程序,编译运行完全正常,在你本机上工作得好好的,你放到源代码管理系统中.然后,告诉你的同事说,你可以取下来用了.这时,你长长的出了一口气,几天的 ...

  2. win7下.NET 2.0未在web服务器上注册的问题(转)

    转自:http://blog.sina.com.cn/s/blog_6d15b547010192hx.html 电脑装了win7操作系统,装上vs2008后运行dotnetnuke项目后出现" ...

  3. .NET Framework(一)

    .NET Framework:即Microsoft .NET Framework,它是用于Windows的新托管代码编程模型.它强大功能与新技术结合起来,用于构建具有视觉上引人注目的用户体验的应用程序 ...

  4. Java导出Excel和CSV(简单Demo)

    Java导出Excel和CSV的简单实现,分别使用POI和JavaCSV. JavaBean public class ReportInfo { int id; String date; int nu ...

  5. php 之 查询 投票练习(0508)

    练习题目: 解题: 方法一: 1. 投票主页面: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  6. PHP第一课笔记

    打算以后学习PHP,花3个月时间学会它,自己为自己加油.每天坚持学习,第一天感觉良好,没开始写,所以不敢觉难,在难也学,加油,me!! PHP笔记记录(2014.7.27) ★web开发的介绍 1.动 ...

  7. YII session存储 调用login方法

    当要进行用户的session存储的时候,可以调用里面的login方法进行存储

  8. C语言结构体(struct)使用方法

    基本定义:结构体,通俗讲就像是打包封装,把一些变量有共同特征(比如同属于某一类事物的属性)的变量封装在内部,通过一定方法访问修改内部变量. 结构体定义: 第一种:只有结构体定义 struct stuf ...

  9. 那些年被我坑过的Python——道阻且长(第五章实用模块讲解)

    random模块 我的随机验证吗程序: 首先保证了字母和数字出现的概率是50% VS 50%,其次是可以订制输出多少位 def Captcha(size): Captcha_list = [] for ...

  10. 在Linux下写一个线程池以及线程池的一些用法和注意点

    -->线程池介绍(大部分来自网络)  在这个部分,详细的介绍一下线程池的作用以及它的技术背景以及他提供的一些服务等.大部分内容来自我日常生活中在网络中学习到的一些概念性的东西. -->代码 ...