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