To execute automated testing using Appium, after the appropriate locator is found, the next step is to perform action on the element.
Though the webdriver specification provides support for certain kind of mobile interaction, its parameters are not always easily mappable to the functionality of the underlying device automation framework.
Appium implements the TouchAction/MultiAction API which helps to build up gestures with multiple actuators. Following are some actions and gestures which can be used on the elements:
The objects of the TouchAction class consist of a series of events. The events are “press”, “release”, “moveTo”, “tap”, “wait”, “longPress”, “cancel” and “perform”. The events take place in a sequence.
A wait event can also be added in between these events to control the timing of the gesture.
The tap event is to tap to an element on the mobile device.
TouchAction touchAction = new TouchAction(driver); AndroidElement expandableListsText = driver .findElementByXPath("//android.widget.TextView[@text='Expandable Lists']"); touchAction.tap(tapOptions().withElement(element(expandableListsText))).perform();
The press and longPress events are self-explanatory. These are used to press on an element or press on an element for a longer time duration respectively. To release the press, release event is used.
TouchAction touchAction = new TouchAction(driver); AndroidElement peopleNamesText = driver .findElementByXPath("//android.widget.TextView[@text='People Names']"); touchAction.longPress(longPressOptions().withElement(element(peopleNamesText)).withDuration(ofSeconds(5))) .release().perform();
The perform event sends the entire sequence of events to Appium and then the gesture is run on the device.
The moveTo event is used to move to an element or to a particular position defined by coordinates.
TouchAction touchAction = new TouchAction(driver); AndroidElement hello1Text = driver.findElementByXPath("//*[@content-desc='hello1']"); AndroidElement hello2Text = driver.findElementByXPath("//*[@content-desc='hello2']"); touchAction.longPress(longPressOptions().withElement(element(hello1Text)).withDuration(ofSeconds(2))) .moveTo(element(hello2Text)).release().perform();
The objects of MultiTouch are collections of TouchActions. The Multitouch gestures only have 2 methods: add and perform.
“add” is used to add another TouchAction to the MultiTouch. Once “perform” is called, all the TouchActions 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 and then the second.
TouchActions actionOne = new TouchAction(); actionOne.press(10, 10); actionOne.moveTo(10, 100); actionOne.release(); TouchActions actionTwo = new TouchAction(); actionTwo.press(20, 20); actionTwo.moveTo(20, 200); actionTwo.release(); MultiTouchAction action = new MultiTouchAction(); action.add(actionOne); action.add(actionTwo); action.perform();