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://w3c.github.io/webdriver/webdriver-spec.html#multiactions-1). Note that this is different from the earlier version of the TouchAction API in the original JSON Wire Protocol.//尽管Selenium Webdriver提供了一些对移动端交互的支持,但是不好用。所以,Appium提供了TouchAction/MultiAction接口,来解决这个问题。

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.

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//TouchAction是由一系列事件组成的。

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//moveTo的坐标参数是个相对值。

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 `perform` event 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://MultiTouch有add和perform两个方法。

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!//在iOS7.0~8.x系统的模拟器中,ScrollViews、CollectionViews、TableViews无效。为解决这个问题,需要使用scroll。

**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 # python
2 driver.execute_script("mobile: scroll", {"direction": "down"})

Sample to scroll using direction and element.

1 # python
2 driver.execute_script("mobile: scroll", {"direction": "down", element: element.getAttribute("id")})
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);

Sample to scroll using direction and 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);

Automating Sliders

iOS

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.

时间: 2024-10-13 17:41:21

Automating mobile gestures的相关文章

Automating mobile web apps

Automating mobile web apps If you're interested in automating your web app in Mobile Safari on iOS or Chrome on Android, Appium can help you. Basically, you write a normal WebDriver test, and use Appium as the Selenium server with a special set of de

全面拥抱移动测试,Mobile JSON Wire Protocol Specification文档翻译

Selenium3已经宣布不支持移动化测试.对于老牌测试工具selenium来说这是以退为进,因为移动自动化测试工具的标准还在selenium团队手上. 本文轻度翻译了这个标准,看得懂的人不用翻译也能看懂,看不懂的人翻的天花乱坠也是一头雾水. 注意,这个规格是给工具的开发者定义的条条框框,对于使用者来说,只要知道哪些是必须工具必须支持的,且支持的细节是什么就可以了,其他可以不去深究. 这个标题就不翻译了 Mobile JSON Wire Protocol Specification 源地址 DR

Automating CSS Regression Testing

The following is a guest post by Garris Shipon . We've touched on the four types of CSS testing here before. Regression testing is the hardest. It's the type where you're trying to test if a change you made to CSS resulted in any unexpected visual pr

Different ways to trigger touchcancel in mobile browsers

The touchcancel event is often neglected when building touch–interfaces using JavaScript. Historically, browsers vendors have never really published documentation detailing the circumstances as to when this event gets fired, and hence it has always b

jQuery Mobile中表单的使用体会

jQuery Mobile是手机端(移动端)页面制作用的框架,包括CSS和JavaScript,此处简单总结一下表单的书写,主要涉及CSS部分.框架提供了表单的一些样式,但在实际使用的时候,我们可能会用自己的自定义样式,这种情况下,框架提供的样式可能就不能满足我们的要求.今天项目中写登录页面的静态网页,碰到了几个问题,在这里和大家交流一下. 1 利用data-role="none" 在使用表单的时候,如果想使用自定义样式,就可以表单元素上给data-role属性赋值none,意思就是不

jQuery Mobile

1. pageinit & pageshow JQM的官方手册重点提醒了使用$(document).bind(‘pageinit’)代替$(document).ready(). 但当你需要对某一个页面(page)编写其独享的Javascript脚本时, 选择器应该选择的是该page层, 而不是document, 并使用live()添加事件处理器.这在ajaxEnable=true的情况下尤为重要. View Demo JS : $(document).bind('pageinit', funct

Mobile Push Notification

In one embodiment, a method includes sending to a mobile client computing device a first notification through a real-time push service, the first notification including content and being associated with a stateful object; the method also includes, in

史上最简单的个人移动APP开发入门--jQuery Mobile版跨平台APP开发

书是人类进步的阶梯. ——高尔基 习大大要求新新人类要有中国梦,鼓励大学生们一毕业就创业.那最好的创业途径是什么呢?就是APP.<构建跨平台APP-jQuery Mobile移动应用实战>就是一本写给没钱没身份没资历的创业小白看的APP书,看完这本书你可以拥有自己的一个APP,不用花钱就能移植到其他移动平台,支持iOS,Android,Windows Phone!!!!!!!!找个最便宜的来练手吧!  小白APP交流Q群:  348632872 清华大学出版社推出的<构建跨平台APP:j

【数据结构】Not so Mobile (6-9)

[UVA839]Not so Mobile 算法入门经典第6章6-9(P157) 题目大意:输入一个树状天平,根据力矩相等原则判断是否平衡. 试题分析:貌似没有什么难点…… #include<iostream> #include<cstring> #include<cstdio> #include<vector> #include<queue> #include<stack> #include<algorithm> usi