Skip to content

Commit 7789b1d

Browse files
feat: Add mobile: wrappers for clipboard APIs (#800)
1 parent 07fe6bb commit 7789b1d

File tree

6 files changed

+106
-18
lines changed

6 files changed

+106
-18
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,26 @@ Fetches the list of supported performance data types that could be used as `data
13401340

13411341
List of strings, where each item is data type name.
13421342

1343+
### mobile: getClipboard
1344+
1345+
Retrieves the plaintext content of the device's clipboard. Available since driver version 3.7
1346+
1347+
#### Returned Result
1348+
1349+
Base64-encoded content of the clipboard or an empty string if the clipboard is empty.
1350+
1351+
### mobile: setClipboard
1352+
1353+
Allows to set the plain text content of the device's clipboard. Available since driver version 3.7
1354+
1355+
#### Arguments
1356+
1357+
Name | Type | Required | Description | Example
1358+
--- | --- | --- | --- | ---
1359+
content | string | yes | Base64-encoded clipboard payload. | YXBwaXVt
1360+
contentType | string | no | The only supported and the default value is `plaintext` | plaintext
1361+
lable | string | no | Optinal label to identify the current clipboard payload. | yolo
1362+
13431363
### mobile: getPerformanceData
13441364

13451365
Retrieves performance data about the given Android subsystem. The data is parsed from the output of the dumpsys utility. Available since driver version 2.24
@@ -1598,6 +1618,8 @@ Useful links:
15981618
UiAutomator2 driver supports Appium endpoints for clipboard management:
15991619
- Set clipboard content (`POST /appium/device/set_clipboard'`)
16001620
- Get clipboard content (`POST /appium/device/get_clipboard`)
1621+
- [mobile: getClipboard](#mobile-getclipboard)
1622+
- [mobile: setClipboard](#mobile-setclipboard)
16011623

16021624
Useful links:
16031625
- https://github.com/appium/python-client/blob/master/appium/webdriver/extensions/clipboard.py

lib/commands/clipboard.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @this {AndroidUiautomator2Driver}
3+
* @returns {Promise<string>} Base64-encoded content of the clipboard
4+
* or an empty string if the clipboard is empty.
5+
*/
6+
export async function getClipboard() {
7+
return String(
8+
(await this.adb.getApiLevel()) < 29
9+
? await this.uiautomator2.jwproxy.command(
10+
'/appium/device/get_clipboard',
11+
'POST',
12+
{}
13+
)
14+
: await this.settingsApp.getClipboard()
15+
);
16+
}
17+
18+
/**
19+
* @this {AndroidUiautomator2Driver}
20+
* @returns {Promise<string>} Base64-encoded content of the clipboard
21+
* or an empty string if the clipboard is empty.
22+
*/
23+
export async function mobileGetClipboard() {
24+
return await this.getClipboard();
25+
}
26+
27+
/**
28+
* @this {AndroidUiautomator2Driver}
29+
* @param {string} content Base64-encoded clipboard payload
30+
* @param {'plaintext'} [contentType='plaintext'] Only a single
31+
* content type is supported, which is 'plaintext'
32+
* @param {string} [label] Optinal label to identify the current
33+
* clipboard payload
34+
* @returns {Promise<void>}
35+
*/
36+
export async function setClipboard(content, contentType, label) {
37+
await this.uiautomator2.jwproxy.command(
38+
'/appium/device/set_clipboard',
39+
'POST',
40+
{content, contentType, label}
41+
);
42+
}
43+
44+
/**
45+
* @this {AndroidUiautomator2Driver}
46+
* @param {import('./types').SetClipboardOpts} opts
47+
* @returns {Promise<void>}
48+
*/
49+
export async function mobileSetClipboard(opts) {
50+
await this.setClipboard(opts.content, opts.contentType, opts.label);
51+
}
52+
53+
/**
54+
* @typedef {import('../driver').AndroidUiautomator2Driver} AndroidUiautomator2Driver
55+
*/

lib/commands/execute.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ export function mobileCommandsMapping() {
4949
scheduleAction: 'mobileScheduleAction',
5050
getActionHistory: 'mobileGetActionHistory',
5151
unscheduleAction: 'mobileUnscheduleAction',
52+
53+
setClipboard: 'mobileSetClipboard',
54+
getClipboard: 'mobileGetClipboard',
5255
};
5356
}
5457

lib/commands/misc.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,6 @@ export async function setOrientation(orientation) {
4040
);
4141
}
4242

43-
/**
44-
* @this {AndroidUiautomator2Driver}
45-
* @returns {Promise<string>}
46-
*/
47-
export async function getClipboard() {
48-
return String(
49-
(await this.adb.getApiLevel()) < 29
50-
? await this.uiautomator2.jwproxy.command(
51-
'/appium/device/get_clipboard',
52-
'POST',
53-
{}
54-
)
55-
: await this.settingsApp.getClipboard()
56-
);
57-
}
58-
5943
/**
6044
* @this {AndroidUiautomator2Driver}
6145
* @returns {Promise<void>}

lib/commands/types.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,3 +471,18 @@ export interface ActionResult {
471471
export interface ActionArgs {
472472
name: string;
473473
}
474+
475+
export interface SetClipboardOpts {
476+
/**
477+
* Base64-encoded clipboard payload
478+
*/
479+
content: string;
480+
/**
481+
* Only a single content type is supported, which is 'plaintext'
482+
*/
483+
contentType?: 'plaintext';
484+
/**
485+
* Optinal label to identify the current clipboard payload
486+
*/
487+
label?: string;
488+
}

lib/driver.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ import {
6161
import {
6262
mobileGetBatteryInfo,
6363
} from './commands/battery';
64+
import {
65+
getClipboard,
66+
mobileGetClipboard,
67+
setClipboard,
68+
mobileSetClipboard,
69+
} from './commands/clipboard';
6470
import {
6571
active,
6672
getAttribute,
@@ -111,7 +117,6 @@ import {
111117
getPageSource,
112118
getOrientation,
113119
setOrientation,
114-
getClipboard,
115120
openNotifications,
116121
suspendChromedriverProxy,
117122
mobileGetDeviceInfo,
@@ -1054,11 +1059,15 @@ class AndroidUiautomator2Driver
10541059
getPageSource = getPageSource;
10551060
getOrientation = getOrientation;
10561061
setOrientation = setOrientation;
1057-
getClipboard = getClipboard;
10581062
openNotifications = openNotifications;
10591063
suspendChromedriverProxy = suspendChromedriverProxy as any;
10601064
mobileGetDeviceInfo = mobileGetDeviceInfo;
10611065

1066+
getClipboard = getClipboard;
1067+
mobileGetClipboard = mobileGetClipboard;
1068+
setClipboard = setClipboard;
1069+
mobileSetClipboard = mobileSetClipboard;
1070+
10621071
setUrl = setUrl;
10631072
mobileDeepLink = mobileDeepLink;
10641073
back = back;

0 commit comments

Comments
 (0)