@@ -19,7 +19,7 @@ import { repeat } from '@midscene/shared/utils';
19
19
import {
20
20
type AndroidDeviceInputOpt ,
21
21
type AndroidDevicePage ,
22
- commonWebActions ,
22
+ commonWebActionsForWebPage ,
23
23
} from '@midscene/web' ;
24
24
import { ADB } from 'appium-adb' ;
25
25
@@ -36,51 +36,6 @@ export type AndroidDeviceOpt = {
36
36
imeStrategy ?: 'always-yadb' | 'yadb-for-non-ascii' ;
37
37
} & AndroidDeviceInputOpt ;
38
38
39
- const asyncNoop = async ( ) => { } ;
40
- const androidActions : DeviceAction [ ] = [
41
- {
42
- name : 'AndroidBackButton' ,
43
- description : 'Trigger the system "back" operation on Android devices' ,
44
- location : false ,
45
- call : asyncNoop ,
46
- } ,
47
- {
48
- name : 'AndroidHomeButton' ,
49
- description : 'Trigger the system "home" operation on Android devices' ,
50
- location : false ,
51
- call : asyncNoop ,
52
- } ,
53
- {
54
- name : 'AndroidRecentAppsButton' ,
55
- description :
56
- 'Trigger the system "recent apps" operation on Android devices' ,
57
- location : false ,
58
- call : asyncNoop ,
59
- } ,
60
- {
61
- name : 'AndroidLongPress' ,
62
- description :
63
- 'Trigger a long press on the screen at specified coordinates on Android devices' ,
64
- paramSchema : '{ duration?: number }' ,
65
- paramDescription : 'The duration of the long press' ,
66
- location : 'optional' ,
67
- whatToLocate : 'The element to be long pressed' ,
68
- call : asyncNoop ,
69
- } ,
70
- {
71
- name : 'AndroidPull' ,
72
- description :
73
- 'Trigger pull down to refresh or pull up actions on Android devices' ,
74
- paramSchema :
75
- '{ direction: "up" | "down", distance?: number, duration?: number }' ,
76
- paramDescription :
77
- 'The direction to pull, the distance to pull, and the duration of the pull.' ,
78
- location : 'optional' ,
79
- whatToLocate : 'The element to be pulled' ,
80
- call : asyncNoop ,
81
- } ,
82
- ] ;
83
-
84
39
export class AndroidDevice implements AndroidDevicePage {
85
40
private deviceId : string ;
86
41
private yadbPushed = false ;
@@ -93,7 +48,102 @@ export class AndroidDevice implements AndroidDevicePage {
93
48
options ?: AndroidDeviceOpt ;
94
49
95
50
actionSpace ( ) : DeviceAction [ ] {
96
- return commonWebActions . concat ( androidActions ) ;
51
+ const commonActions = commonWebActionsForWebPage ( this ) ;
52
+ commonActions . forEach ( ( action ) => {
53
+ if ( action . name === 'Input' ) {
54
+ action . call = async ( context , param ) => {
55
+ const { element } = context ;
56
+ if ( element ) {
57
+ await this . clearInput ( element as unknown as ElementInfo ) ;
58
+
59
+ if ( ! param || ! param . value ) {
60
+ return ;
61
+ }
62
+ }
63
+
64
+ await this . keyboard . type ( param . value , {
65
+ autoDismissKeyboard : this . options ?. autoDismissKeyboard ,
66
+ } ) ;
67
+ } ;
68
+ }
69
+ } ) ;
70
+
71
+ const allActions : DeviceAction [ ] = [
72
+ ...commonWebActionsForWebPage ( this ) ,
73
+ {
74
+ name : 'AndroidBackButton' ,
75
+ description : 'Trigger the system "back" operation on Android devices' ,
76
+ location : false ,
77
+ call : async ( context , param ) => {
78
+ await this . back ( ) ;
79
+ } ,
80
+ } ,
81
+ {
82
+ name : 'AndroidHomeButton' ,
83
+ description : 'Trigger the system "home" operation on Android devices' ,
84
+ location : false ,
85
+ call : async ( context , param ) => {
86
+ await this . home ( ) ;
87
+ } ,
88
+ } ,
89
+ {
90
+ name : 'AndroidRecentAppsButton' ,
91
+ description :
92
+ 'Trigger the system "recent apps" operation on Android devices' ,
93
+ location : false ,
94
+ call : async ( context , param ) => {
95
+ await this . recentApps ( ) ;
96
+ } ,
97
+ } ,
98
+ {
99
+ name : 'AndroidLongPress' ,
100
+ description :
101
+ 'Trigger a long press on the screen at specified coordinates on Android devices' ,
102
+ paramSchema : '{ duration?: number }' ,
103
+ paramDescription : 'The duration of the long press in milliseconds' ,
104
+ location : 'required' ,
105
+ whatToLocate : 'The element to be long pressed' ,
106
+ call : async ( context , param ) => {
107
+ const { element } = context ;
108
+ if ( ! element ) {
109
+ throw new Error (
110
+ 'AndroidLongPress requires an element to be located' ,
111
+ ) ;
112
+ }
113
+ const [ x , y ] = element . center ;
114
+ await this . longPress ( x , y , param . duration ) ;
115
+ } ,
116
+ } as DeviceAction < { duration ?: number } > ,
117
+ {
118
+ name : 'AndroidPull' ,
119
+ description :
120
+ 'Trigger pull down to refresh or pull up actions on Android devices' ,
121
+ paramSchema :
122
+ '{ direction: "up" | "down", distance?: number, duration?: number }' ,
123
+ paramDescription :
124
+ 'The direction to pull, the distance to pull (in pixels), and the duration of the pull (in milliseconds).' ,
125
+ location : 'optional' ,
126
+ whatToLocate : 'The element to be pulled' ,
127
+ call : async ( context , param ) => {
128
+ const { element } = context ;
129
+ const startPoint = element
130
+ ? { left : element . center [ 0 ] , top : element . center [ 1 ] }
131
+ : undefined ;
132
+ if ( param . direction === 'down' ) {
133
+ await this . pullDown ( startPoint , param . distance , param . duration ) ;
134
+ } else if ( param . direction === 'up' ) {
135
+ await this . pullUp ( startPoint , param . distance , param . duration ) ;
136
+ } else {
137
+ throw new Error ( `Unknown pull direction: ${ param . direction } ` ) ;
138
+ }
139
+ } ,
140
+ } as DeviceAction < {
141
+ direction : 'up' | 'down' ;
142
+ distance ?: number ;
143
+ duration ?: number ;
144
+ } > ,
145
+ ] ;
146
+ return allActions ;
97
147
}
98
148
99
149
constructor ( deviceId : string , options ?: AndroidDeviceOpt ) {
0 commit comments