Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add more mobile extensions #606

Merged
merged 2 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,99 @@ Values are dictionaries with the following properties:
- `width`: Bar width (might be zero if the bar is not present in the system info output)
- `height`: Bar height (might be zero if the bar is not present in the system info output)

### mobile: fingerprint

Emulate [fingerprint](https://learn.microsoft.com/en-us/xamarin/android/platform/fingerprint-authentication/enrolling-fingerprint) on Android Emulator. Only works on API 23+. Available since driver version 2.22

#### Arguments

Name | Type | Required | Description | Example
--- | --- | --- | --- | ---
fingerprintId | number | yes | The value is the id for the finger that was "scanned". It is a unique integer that you assign for each virtual fingerprint. When the app is running you can run this same command each time the emulator prompts you for a fingerprint, you can run the adb command and pass it the fingerprintId to simulate the fingerprint scan. | 1

### mobile: sendSms

Emulate sending an SMS to the given phone number. Only works on emulators. Available since driver version 2.22

#### Arguments

Name | Type | Required | Description | Example
--- | --- | --- | --- | ---
phoneNumber | string | yes | The phone number to send SMS to | 0123456789
message | string | yes | The SMS message payload | Hello

### mobile: gsmCall

Emulate a GSM call to the given phone number. Only works on emulators. Available since driver version 2.22

#### Arguments

Name | Type | Required | Description | Example
--- | --- | --- | --- | ---
phoneNumber | string | yes | The phone number to call to | 0123456789
action | call or accept or cancel or hold | yes | One of possible actions to take | accept

### mobile: gsmSignal

Emulate GSM signal strength change event. Only works on emulators. Available since driver version 2.22

#### Arguments

Name | Type | Required | Description | Example
--- | --- | --- | --- | ---
strength | 0 or 1 or 2 or 3 or 4 | yes | One of possible signal strength values, where 4 is the best signal. | 3

### mobile: gsmVoice

Emulate a GSM call to the given phone number. Only works on emulators. Available since driver version 2.22

#### Arguments

Name | Type | Required | Description | Example
--- | --- | --- | --- | ---
state | on or off | yes | Voice state | off

### mobile: powerAC

Emulate AC power state change. Only works on emulators. Available since driver version 2.22

#### Arguments

Name | Type | Required | Description | Example
--- | --- | --- | --- | ---
state | on or off | yes | AC Power state | off

### mobile: powerCapacity

Emulate power capacity change. Only works on emulators. Available since driver version 2.22

#### Arguments

Name | Type | Required | Description | Example
--- | --- | --- | --- | ---
percent | 0 to 100 | yes | Percentage value in range [0, 100] | 50

### mobile: networkSpeed

Emulate different network connection speed modes. Only works on emulators. Available since driver version 2.22

#### Arguments

Name | Type | Required | Description | Example
--- | --- | --- | --- | ---
speed | gsm or scsd or gprs or edge or umts or hsdpa or lte or evdo or full | yes | Mobile network speed mode name | edge

### mobile: replaceElementValue

Sends a text to the given element by replacing its previous content. Available since driver version 2.22

#### Arguments

Name | Type | Required | Description | Example
--- | --- | --- | --- | ---
elementId | string | yes | Hexadecimal identifier of the destination text input | 123456-3456-3435-3453453
text | string | yes | The text to enter. It could also contain Unicode characters. If the text ends with `\\n` (the backslash must be escaped, so the char is NOT translated into `0x0A`) then the Enter key press is going to be emulated after it is entered (the `\\n` substring itself will be cut off from the typed text). | yolo


## Applications Management

Expand Down
18 changes: 18 additions & 0 deletions lib/commands/element.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import _ from 'lodash';
import { util } from 'appium/support';
import { PROTOCOLS, W3C_ELEMENT_KEY } from 'appium/driver';
import { requireArgs } from '../utils';

let commands = {}, helpers = {}, extensions = {};

Expand Down Expand Up @@ -121,6 +122,23 @@ commands.getElementRect = async function (elementId) {
return await this.uiautomator2.jwproxy.command(`/element/${elementId}/rect`, 'GET');
};

/**
* @typedef {Object} ReplaceValueOptions
* @property {string} elementId The id of the element whose content will be replaced
* @property {string} text The actual text to set
*/

/**
* Sends text to the given element by replacing its previous content
*
* @param {ReplaceValueOptions} opts
* @throws {Error} If there was a faulre while setting the text
*/
commands.mobileReplaceElementValue = async function mobileReplaceElementValue (opts = {}) {
const {elementId, text} = requireArgs(['elementId', 'text'], opts);
return await this.uiautomator2.jwproxy.command(`/element/${elementId}/value`, 'POST', {text, replace: true});
};

Object.assign(extensions, commands, helpers);
export { commands, helpers };
export default extensions;
12 changes: 11 additions & 1 deletion lib/commands/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ extensions.executeMobile = async function (mobileCommand, opts = {}) {
listSms: 'mobileListSms',

type: 'mobileType',
sensorSet: 'sensorSet',
replaceElementValue: 'mobileReplaceElementValue',

pushFile: 'mobilePushFile',
pullFile: 'mobilePullFile',
Expand Down Expand Up @@ -149,6 +149,16 @@ extensions.executeMobile = async function (mobileCommand, opts = {}) {

getDisplayDensity: 'getDisplayDensity',
getSystemBars: 'getSystemBars',

fingerprint: 'mobilefingerprint',
sendSms: 'mobileSendSms',
gsmCall: 'mobileGsmCall',
gsmSignal: 'mobileGsmSignal',
gsmVoice: 'mobileGsmVoice',
powerAc: 'mobilePowerAc',
powerCapacity: 'mobilePowerCapacity',
networkSpeed: 'mobileNetworkSpeed',
sensorSet: 'sensorSet',
};

if (!_.has(mobileCommandsMapping, mobileCommand)) {
Expand Down
19 changes: 19 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import _ from 'lodash';
import { errors } from 'appium/driver';

/**
* Assert the presence of particular keys in the given object
*
* @template {Object} T
* @param {string|string[]} argNames one or more key names
* @param {T} opts the object to check
* @returns {T} the same given object
*/
export function requireArgs (argNames, opts = {}) {
for (const argName of (_.isArray(argNames) ? argNames : [argNames])) {
if (!_.has(opts, argName)) {
throw new errors.InvalidArgumentError(`'${argName}' argument must be provided`);
}
}
return opts;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"dependencies": {
"@babel/runtime": "^7.0.0",
"appium-adb": "^9.11.0",
"appium-android-driver": "^5.10.4",
"appium-android-driver": "^5.11.0",
"appium-chromedriver": "^5.3.1",
"appium-uiautomator2-server": "^5.7.2",
"asyncbox": "^2.3.1",
Expand Down