Automating the Mobile Automation Gestures | Appium Mobile Automation
In mobile automation below listed Appium actions most of the time we
use only these actions for complete automation. So these actions are
very important. we should be aware of these actions
Appium supports the following gestures:
- Tap on an element.
- Tap on x, y coordinates.
- Press an element for a particular duration.
- Press x, y coordinates for a particular duration.
- Drag(Swipe) one element to another element.
- Multitouch for an element.
Appium Supports these gestures using the TouchAction Class.
TouchAction touch = new TouchAction(driver):
Supported methods for TouchAction are:
Method Name
|
Purpose
|
press(PointOption pressOptions) |
Press action on the screen. |
longPress(LongPressOptions longPressOptions) |
Press and hold the at the center of an element until the
context menu event has fired. |
tap(PointOption tapOptions) |
Tap on a position. |
moveTo(PointOption moveToOptions) |
Moves current touch to a new position. |
cancel() |
Cancel this action, if it was partially completed by the
performsTouchActions. |
perform() |
Perform this chain of actions on the performsTouchActions. |
Before exploring each mentioned action we need to understand the
significance of perform() as it plays a vital role. The Appium client
simply records all the instructions and actions on the client side and
stores the intermediate values in a local data structure. The
perform() method is used to send all actions to the appium server - as
soon as perform() is called, the intermediate actions and instructions
are converted to JSON and sent to the appium server, and then the
actual action is being performed. So for any gesture code the last
method called would be perform()
Note: This is a common omission during development, forgetting to call
perform() and wondering why your test isn’t working!
1) Tap on element in Appium:
Method:
tap(TapOptions tapOptions)
Usage:
It is the simplest action, as the name suggests it will simply
click/tap
on a particular location. It is a combination of press() and
release()
Example:
TouchAction touchAction = new TouchAction(driver);
touchAction.tap(tapOptions()
.withElement(element(androidElement)))
.perform()
NOTE:
Here you can also put the wait along with the tap action, for
example:
new TouchAction(driver)
.tap(tapOptions().withElement(element(androidElement)))
.waitAction(waitOptions(Duration.ofMillis(millis)))
.perform();
2) Tap on x, y coordinates in Appium:
Now the question is how can you get the x,y coordinate?
- It depends...
- Because you can get the Pointer location in Android but you can not get it in iOS devices.
Getting the pointer location in Android:
-
Move to Settings > Developer options
-
Enable the Pointer location.
-
Now move to any application for which you need the coordinates of a
particular location. Tap on the location and you will get the
coordinates for that spot at the top of the screen.
Getting the pointer location in iOS:
iOS does not support the pointer location and there aren’t even any
third party apps or tools which come to the rescue. Therefore you need
to calculate it using screen resolution and a little bit of
prediction. In case you don’t get success at first, you can use trial
and error to get the needed location.
Lets look into click tap by x, y co-ordinate method:
Method:
tap(PointOption pointOptions)
Usage:
It is used to tap on a particular x,y coordinate point.
Example:
TouchAction touchAction = new TouchAction(driver);
touchAction.tap(PointOption.point(1280, 1013))
.perform()
NOTE:
Similar like Tap on element you can put the wait along with the tap
action, for example:
new TouchAction(driver)
.tap(point(x, y))
.waitAction(waitOptions(Duration.ofMillis(millis)))
.perform();
3) Press an element for a particular duration :
Method:
press(PointOption pressOptions)
Usage:
It is used to apply the press action. After the press action you also
need to release so that the state would be in press mode. You do so by
calling the release() function after calling press().
Example:
TouchAction touchAction = new TouchAction(driver);
touchAction.press(element(element))
.waitAction(waitOptions(ofSeconds(seconds)))
.release()
.perform();
4) Press x, y coordinates for a particular duration in Appim :
Method:
press(PointOption pressOptions)
Usage:
Similar to Press(ing) an element for a particular duration, here you
just need to pass x, y coordinates instead of an element and don’t
forget to call the release() function after calling press().
Example:
TouchAction touchAction = new TouchAction(driver);
touchAction.press(point(x,y))
.waitAction(waitOptions(ofSeconds(seconds)))
.release()
.perform();
5) Drag(swipe) one element to an another element in Appim:
Dragging one element to another element is one kind of swiping
action.
But here location in coordinates would not matter as we have both of
the elements :
1. Element which needs to be dragged,
2. Element upon which another element will be dragged).
TouchAction swipe = new TouchAction(driver)
.press(ElementOption.element(element1))
.waitAction(waitOptions(ofSeconds(2)))
.moveTo(ElementOption.element(element2))
.release()
.perform();
6) MultiTouch in Appim:
As the name suggests it means multiple touches happening at the same
time.
For example on iOS if you want to move to the Main screen, you need
to use 5 fingers and do a swipe.
Multi Touch is handled by the MultiTouchAction class. It has a add((TouchActions touchActions) method so in which we need to pass a
TouchActions object
So let say you want to press on 5 different points at a time then
first you need to create 5 TouchActions, but here the important thing
is we are not having a perform method at the end. We just need to call
the release method for the TouchAction object, and then pass those
values into the add method of the MultiTouchAction class.
You can perform Multi Touch for:
a. Multiple touches at a time.
TouchAction touchActionOne = new TouchAction();
touchActionOne.press(PointOption.point(100, 100));
touchActionOne.release();
TouchAction touchActionTwo = new TouchAction();
touchActionTwo.press(PointOption.point(200, 200));
touchActionTwo.release();
MultiTouchAction action = new MultiTouchAction();
action.add(touchActionOne);
action.add(touchActionTwo);
action.perform();
OR if you want to perform multi touch on particular elements then use
the below code snippet.
TouchAction touchAction1 = new TouchAction(driver)
.tap(ElementOption.element(e1))
.release();
TouchAction touchAction2 = new TouchAction(driver)
.tap(ElementOption.element(e2))
.release();
MultiTouchAction action = new MultiTouchAction();
action.add(touchAction1);
action.add(touchAction2);
action.perform();
b. Swiping using multiple fingers.
TouchAction touchActionOne = new TouchAction();
touchActionOne.press(PointOption.point(100, 100));
touchActionOne.moveTo(PointOption.point(500, 100));
touchActionOne.release();
TouchAction touchActionTwo = new TouchAction();
touchActionTwo.press(PointOption.point(100, 200));
touchActionTwo.moveTo(PointOption.point(500, 100));
touchActionTwo.release();
MultiTouchAction action = new MultiTouchAction();
action.add(touchActionOne);
action.add(touchActionTwo);
action.perform();
NOTE:
As mentioned earlier only MultiTouchAction should call the
perform() method at the end. For TouchActions, the perform() method
should not be called otherwise instructions will be sent to the
Appium server and the click will happen before the Multi Touch
action.
No comments:
Post a Comment
If any suggestions or issue, please provide