Skip to content

Commit 2c3dddb

Browse files
DancingNerdDani Paul
andauthored
Adds a mobile drag and drop snippet example (#83)
* Adds a mobile drag and drop snippet example * sneaking another fix into this pr --------- Co-authored-by: Dani Paul <[email protected]>
1 parent b72b2a2 commit 2c3dddb

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* This script demonstrates how to perform a drag and drop action on a mobile app element using Appium.
3+
* @param {*} dragElementText The exact text of the element to drag.
4+
* @param {*} dropElementText The exact text of the element to drop the element onto.
5+
*/
6+
function mablJavaScriptStep(
7+
mablInputs,
8+
callback,
9+
dragElementText = undefined,
10+
dropElementText = undefined
11+
) {
12+
async function callAppium() {
13+
const driver = await mabl.mobile.getDriver();
14+
// Create the xpath for both elements
15+
const elm1 = await driver.findElement(
16+
"xpath",
17+
getXpathForElementMatching(dragElementText)
18+
);
19+
const elm2 = await driver.findElement(
20+
"xpath",
21+
getXpathForElementMatching(dropElementText)
22+
);
23+
// Get the rect of both elements
24+
const elm1Rect = await getElementCenterXY(elm1);
25+
const elm2Rect = await getElementCenterXY(elm2);
26+
// Define the actions to perform
27+
const actions = {
28+
type: "pointer",
29+
id: "finger1",
30+
parameters: { pointerType: "touch" },
31+
actions: [
32+
// Move the pointer to the center of the first element
33+
{ type: "pointerMove", duration: 0, x: elm1Rect.x, y: elm1Rect.y },
34+
// Press down on the first element
35+
{ type: "pointerDown", button: 0 },
36+
// Wait for 1 second as a long press to lift element
37+
{ type: "pause", duration: 1000 },
38+
// Move the pointer to the center of the second element over 500ms
39+
{ type: "pointerMove", duration: 2000, x: elm2Rect.x, y: elm2Rect.y },
40+
// Release the press
41+
{ type: "pointerUp", button: 0 },
42+
],
43+
};
44+
// Perform the actions
45+
await driver.performActions([actions]);
46+
}
47+
// Call the function and return the result
48+
callAppium()
49+
.then((result) => callback(result))
50+
.catch((error) => callback(error));
51+
}
52+
53+
/**
54+
* Returns an xpath for an element matching the given text.
55+
* @param {string} text The text to match.
56+
* @returns {string} The xpath.
57+
*/
58+
function getXpathForElementMatching(text) {
59+
return `//*[@text="${text}" or @content-desc="${text}" or @label="${text}"]`;
60+
}
61+
62+
/**
63+
* Returns the center coordinates of an element.
64+
* @param target element to get the center coordinates of.
65+
* @returns {x, y} The center coordinates.
66+
*/
67+
async function getElementCenterXY(target) {
68+
const rect = await target.getRect();
69+
return {
70+
x: rect.x + rect.width / 2,
71+
y: rect.y + rect.height / 2,
72+
};
73+
}

mabl snippets/native mobile testing/tapOnElementByText.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function mablJavaScriptStep(mablInputs, callback) {
55
// Create the xpath to find the element by text
66
const xpath = `//*[@text="${elementText}" or @content-desc="${elementText}" or @label="${elementText}"]`;
77
// Find the element by xpath
8-
const target = await driver.findElement("xpath", elementXpath);
8+
const target = await driver.findElement("xpath", xpath);
99
// Tap on the element
1010
await target.click();
1111
// Return a success message

0 commit comments

Comments
 (0)