Appium Touch Actions in CodeceptJS — Double Tap, Press, Long Press, Drag and Drop etc.
Most of the Mobile apps these days work with a simple touch operation — technically the ‘tap’, which the CodeceptJS and Appium handle quite nicely with the ‘tap’ API.
But at times, the UX designer and the product manager decides more operations on Mobile elements should be supported — there come the requirements for double-tapping, press, long press, and drag and drop. That’s where CodeceptJS APIs gets trickier. And, without further build-up — Yes! The good news is that it’s possible.
Let’s jump to the code then/ TLDR:
const value = await I.grabElementBoundingRect('<source-selector>');
const sourceX = parseInt(value['x'])+parseInt(value['width'])/2;
const sourceY = parseInt(value['y'])+parseInt(value['height'])/2;//Double Tap
I.touchPerform([{
action: 'tap',
options: {
x: sourceX,
y: sourceY,
count: 2
}
}]);//Press
I.touchPerform([{
action: 'press',
options: {
x: sourceX,
y: sourceY,
}
}, {action: 'release'}])//Long Press
I.touchPerform([{
action: 'longPress',
options: {
x: sourceX,
y: sourceY,
}
}, {action: 'release'}]);//Drag and Drop
const value2 = await I.grabElementBoundingRect('<target-selector>');
const targetX = parseInt(value2['x'])+parseInt(value2['width'])/2;
const targetY = parseInt(value2['y'])+parseInt(value2['height'])/2;
I.touchPerform([{
action: 'press',
options: {
x: sourceX,
y: sourceY,
}
},
{ action: 'moveTo',
options: {
x: targetX,
y: targetY,
} },
{action: 'release'}]);
Explanation:
The first thing we got to do is find a safe point/ (x,y) coordinate on the screen for the element we want to do the operation. The mid-point of the Mobile Element is usually a good spot which is what is being used in this example. Do the same for the target element as well.
Each action gets executed in the sequence so for e.g. drag and drop-Appium press the source mid-point and moves to destination mid-point. That’s it! We are done!
Happy tapping & bunch of other things on your test automation code!