33 * Licensed under the MIT License. See License.txt in the project root for license information.
44 *--------------------------------------------------------------------------------------------*/
55
6- import { Driver , Element } from './driver' ;
6+ import { IDriver , IElement } from './vscode/driver' ;
7+
8+ export class CodeDriver {
9+
10+ constructor (
11+ private driver : IDriver ,
12+ private verbose : boolean
13+ ) { }
14+
15+ private _activeWindowId : number | undefined = undefined ;
16+
17+ async dispatchKeybinding ( keybinding : string ) : Promise < void > {
18+ if ( this . verbose ) {
19+ console . log ( '- dispatchKeybinding:' , keybinding ) ;
20+ }
21+
22+ const windowId = await this . getWindowId ( ) ;
23+ await this . driver . dispatchKeybinding ( windowId , keybinding ) ;
24+ }
25+
26+ async click ( selector : string , xoffset ?: number | undefined , yoffset ?: number | undefined ) : Promise < any > {
27+ if ( this . verbose ) {
28+ console . log ( '- click:' , selector ) ;
29+ }
30+
31+ const windowId = await this . getWindowId ( ) ;
32+ await this . driver . click ( windowId , selector , xoffset , yoffset ) ;
33+ }
34+
35+ async doubleClick ( selector : string ) : Promise < any > {
36+ if ( this . verbose ) {
37+ console . log ( '- doubleClick:' , selector ) ;
38+ }
39+
40+ const windowId = await this . getWindowId ( ) ;
41+ await this . driver . doubleClick ( windowId , selector ) ;
42+ }
43+
44+ async move ( selector : string ) : Promise < any > {
45+ if ( this . verbose ) {
46+ console . log ( '- move:' , selector ) ;
47+ }
48+
49+ const windowId = await this . getWindowId ( ) ;
50+ await this . driver . move ( windowId , selector ) ;
51+ }
52+
53+ async setValue ( selector : string , text : string ) : Promise < void > {
54+ if ( this . verbose ) {
55+ console . log ( '- setValue:' , selector , text ) ;
56+ }
57+
58+ const windowId = await this . getWindowId ( ) ;
59+ await this . driver . setValue ( windowId , selector , text ) ;
60+ }
61+
62+ async getTitle ( ) : Promise < string > {
63+ if ( this . verbose ) {
64+ console . log ( '- getTitle:' ) ;
65+ }
66+
67+ const windowId = await this . getWindowId ( ) ;
68+ return await this . driver . getTitle ( windowId ) ;
69+ }
70+
71+ async isActiveElement ( selector : string ) : Promise < boolean > {
72+ if ( this . verbose ) {
73+ console . log ( '- isActiveElement:' , selector ) ;
74+ }
75+
76+ const windowId = await this . getWindowId ( ) ;
77+ return await this . driver . isActiveElement ( windowId , selector ) ;
78+ }
79+
80+ async getElements ( selector : string , recursive = false ) : Promise < IElement [ ] > {
81+ if ( this . verbose ) {
82+ console . log ( '- getElements:' , selector ) ;
83+ }
84+
85+ const windowId = await this . getWindowId ( ) ;
86+ return await this . driver . getElements ( windowId , selector , recursive ) ;
87+ }
88+
89+ async typeInEditor ( selector : string , text : string ) : Promise < void > {
90+ if ( this . verbose ) {
91+ console . log ( '- typeInEditor:' , selector , text ) ;
92+ }
93+
94+ const windowId = await this . getWindowId ( ) ;
95+ return await this . driver . typeInEditor ( windowId , selector , text ) ;
96+ }
97+
98+ async getTerminalBuffer ( selector : string ) : Promise < string [ ] > {
99+ if ( this . verbose ) {
100+ console . log ( '- getTerminalBuffer:' , selector ) ;
101+ }
102+
103+ const windowId = await this . getWindowId ( ) ;
104+ return await this . driver . getTerminalBuffer ( windowId , selector ) ;
105+ }
106+
107+ private async getWindowId ( ) : Promise < number > {
108+ if ( typeof this . _activeWindowId !== 'number' ) {
109+ const windows = await this . driver . getWindowIds ( ) ;
110+ this . _activeWindowId = windows [ 0 ] ;
111+ }
112+
113+ return this . _activeWindowId ;
114+ }
115+ }
116+
117+ export function findElement ( element : IElement , fn : ( element : IElement ) => boolean ) : IElement | null {
118+ const queue = [ element ] ;
119+
120+ while ( queue . length > 0 ) {
121+ const element = queue . shift ( ) ! ;
122+
123+ if ( fn ( element ) ) {
124+ return element ;
125+ }
126+
127+ queue . push ( ...element . children ) ;
128+ }
129+
130+ return null ;
131+ }
132+
133+ export function findElements ( element : IElement , fn : ( element : IElement ) => boolean ) : IElement [ ] {
134+ const result : IElement [ ] = [ ] ;
135+ const queue = [ element ] ;
136+
137+ while ( queue . length > 0 ) {
138+ const element = queue . shift ( ) ! ;
139+
140+ if ( fn ( element ) ) {
141+ result . push ( element ) ;
142+ }
143+
144+ queue . push ( ...element . children ) ;
145+ }
146+
147+ return result ;
148+ }
7149
8150export class API {
9151
@@ -13,7 +155,7 @@ export class API {
13155 private readonly retryDuration = 100 ; // in milliseconds
14156
15157 constructor (
16- private driver : Driver ,
158+ private driver : CodeDriver ,
17159 waitTime : number
18160 ) {
19161 this . retryCount = ( waitTime * 1000 ) / this . retryDuration ;
@@ -58,11 +200,11 @@ export class API {
58200 return elements . length ;
59201 }
60202
61- waitForElements ( selector : string , recursive : boolean , accept : ( result : Element [ ] ) => boolean = result => result . length > 0 ) : Promise < Element [ ] > {
203+ waitForElements ( selector : string , recursive : boolean , accept : ( result : IElement [ ] ) => boolean = result => result . length > 0 ) : Promise < IElement [ ] > {
62204 return this . waitFor ( ( ) => this . driver . getElements ( selector , recursive ) , accept , `elements with selector ${ selector } ` ) as Promise < any > ;
63205 }
64206
65- waitForElement ( selector : string , accept : ( result : Element | undefined ) => boolean = result => ! ! result ) : Promise < void > {
207+ waitForElement ( selector : string , accept : ( result : IElement | undefined ) => boolean = result => ! ! result ) : Promise < void > {
66208 return this . waitFor ( ( ) => this . driver . getElements ( selector ) . then ( els => els [ 0 ] ) , accept , `element with selector ${ selector } ` ) as Promise < any > ;
67209 }
68210
0 commit comments