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 mobile extension for pressKey action #597

Merged
merged 1 commit into from
Apr 20, 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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,20 @@ Checks if the system on-screen keyboard is visible.

`true` if the keyboard is visible

### mobile: pressKey

Emulates single key press on the key with the given code. Available since driver version 2.17.0

#### Arguments

Name | Type | Required | Description | Example
--- | --- | --- | --- | ---
keycode | number | yes | A valid Android key code. See [KeyEvent documentation](https://developer.android.com/reference/android/view/KeyEvent) for the list of available key codes | 0x00000099 (which is KEYCODE_NUMPAD_9)
metastate | number | no | An integer in which each bit set to 1 represents a pressed meta key. See
[KeyEvent documentation](https://developer.android.com/reference/android/view/KeyEvent) for more details. | 0x00000010 (which is META_ALT_LEFT_ON)
flags | number | no | Flags for the particular key event. See [KeyEvent documentation](https://developer.android.com/reference/android/view/KeyEvent) for more details. | 0x00000001 (which is FLAG_WOKE_HERE)
isLongPress | number | no | Whether to emulate long key press. `false` by default. | true


## Applications Management

Expand Down
34 changes: 34 additions & 0 deletions lib/commands/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,40 @@ commands.setOrientation = async function (orientation) {
return await this.uiautomator2.jwproxy.command(`/orientation`, 'POST', {orientation});
};

/**
* @typedef {Object} PressKeyOptions
* @property {number} keycode A valid Android key code. See https://developer.android.com/reference/android/view/KeyEvent
* for the list of available key codes
* @property {number?} metastate An integer in which each bit set to 1 represents a pressed meta key. See
* https://developer.android.com/reference/android/view/KeyEvent for more details.
* @property {string?} flags Flags for the particular key event. See
* https://developer.android.com/reference/android/view/KeyEvent for more details.
* @property {boolean} isLongPress [false] Whether to emulate long key press
*/

/**
* Emulates single key press of the key with the given code.
*
* @param {PressKeyOptions} opts
*/
commands.mobilePressKey = async function mobilePressKey(opts = {}) {
const {
keycode,
metastate,
flags,
isLongPress = false,
} = opts;

return await this.uiautomator2.jwproxy.command(
`/appium/device/${isLongPress ? 'long_' : ''}press_keycode`,
'POST', {
keycode,
metastate,
flags
}
);
};

Object.assign(extensions, commands, helpers);
export { commands, helpers };
export default extensions;
2 changes: 2 additions & 0 deletions lib/commands/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ extensions.executeMobile = async function (mobileCommand, opts = {}) {

hideKeyboard: 'mobileHideKeyboard',
isKeyboardShown: 'isKeyboardShown',

pressKey: 'mobilePressKey',
};

if (!_.has(mobileCommandsMapping, mobileCommand)) {
Expand Down