|
| 1 | +import type {StringRecord} from '@appium/types'; |
| 2 | +import type {AndroidUiautomator2Driver} from '../driver'; |
| 3 | +import type {ActionResult} from './types'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Schedules a recurring action to be performed at specified intervals. |
| 7 | + * @param name - Unique name for the scheduled action. |
| 8 | + * @param steps - Array of action steps to be executed. |
| 9 | + * @param maxPass - Maximum number of successful executions before stopping. |
| 10 | + * @param maxFail - Maximum number of failed executions before stopping. |
| 11 | + * @param times - Total number of times to execute the action. |
| 12 | + * @param intervalMs - Interval in milliseconds between action executions. |
| 13 | + * @param maxHistoryItems - Maximum number of history items to keep. |
| 14 | + * @returns The result of scheduling the action. |
| 15 | + */ |
| 16 | +export async function mobileScheduleAction( |
| 17 | + this: AndroidUiautomator2Driver, |
| 18 | + name: string, |
| 19 | + steps: StringRecord[], |
| 20 | + maxPass?: number, |
| 21 | + maxFail?: number, |
| 22 | + times?: number, |
| 23 | + intervalMs?: number, |
| 24 | + maxHistoryItems?: number, |
| 25 | +): Promise<any> { |
| 26 | + return await this.uiautomator2.jwproxy.command('/appium/schedule_action', 'POST', { |
| 27 | + name, |
| 28 | + steps, |
| 29 | + maxFail, |
| 30 | + maxPass, |
| 31 | + times, |
| 32 | + intervalMs, |
| 33 | + maxHistoryItems, |
| 34 | + }); |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Gets the execution history for a scheduled action. |
| 39 | + * @param name - Name of the scheduled action. |
| 40 | + * @returns The action execution history containing repeats and step results. |
| 41 | + */ |
| 42 | +export async function mobileGetActionHistory( |
| 43 | + this: AndroidUiautomator2Driver, |
| 44 | + name: string, |
| 45 | +): Promise<ActionResult> { |
| 46 | + return (await this.uiautomator2.jwproxy.command('/appium/action_history', 'POST', {name})) as ActionResult; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Unschedules a previously scheduled action. |
| 51 | + * @param name - Name of the scheduled action to unschedule. |
| 52 | + * @returns The result of unscheduling the action. |
| 53 | + */ |
| 54 | +export async function mobileUnscheduleAction( |
| 55 | + this: AndroidUiautomator2Driver, |
| 56 | + name: string, |
| 57 | +): Promise<any> { |
| 58 | + return await this.uiautomator2.jwproxy.command('/appium/unschedule_action', 'POST', {name}); |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Performs a sequence of actions. |
| 63 | + * @param actions - Array of action objects to perform. Pointer actions are automatically converted to touch type. |
| 64 | + */ |
| 65 | +export async function performActions( |
| 66 | + this: AndroidUiautomator2Driver, |
| 67 | + actions: StringRecord[], |
| 68 | +): Promise<void> { |
| 69 | + // This is mandatory, since Selenium API uses MOUSE as the default pointer type |
| 70 | + const preprocessedActions = actions.map((action) => |
| 71 | + Object.assign( |
| 72 | + {}, |
| 73 | + action, |
| 74 | + action.type === 'pointer' |
| 75 | + ? { |
| 76 | + parameters: { |
| 77 | + pointerType: 'touch', |
| 78 | + }, |
| 79 | + } |
| 80 | + : {} |
| 81 | + ) |
| 82 | + ); |
| 83 | + this.log.debug(`Preprocessed actions: ${JSON.stringify(preprocessedActions, null, ' ')}`); |
| 84 | + await this.uiautomator2.jwproxy.command('/actions', 'POST', { |
| 85 | + actions: preprocessedActions, |
| 86 | + }); |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Releases all currently pressed keys and buttons. |
| 91 | + */ |
| 92 | +export async function releaseActions(this: AndroidUiautomator2Driver): Promise<void> { |
| 93 | + this.log.info('On this platform, releaseActions is a no-op'); |
| 94 | +} |
| 95 | + |
0 commit comments