11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT License.
33
4- import { SpectronWindow } from 'spectron' ;
4+ import { SpectronClient , SpectronWindow } from 'spectron' ;
55import * as WebDriverIO from 'webdriverio' ;
66
7- // spectron 10.0.1 includes @types/webdriverio, whose absence
8- // we worked around when initially consuming spectron.
9- // @types /webdriver lacks promises, so this file adds
10- // promise-based signatures that our e2e code can rely on.
11- // @types /webdriver has been superceded by improved types
12- // in webdriverio 5 directly, but Spectron has not consumed them
7+ // This file worked around incorrect or missing spectron/webdriverio
8+ // typings in the past. webdriverio types are improved in spectron 12.0.0,
9+ // so parts of this file can be removed by updating individual end-to-end
10+ // tests/controllers to consume SpectronClient directly. SpectronAsyncWindow
11+ // works around github issue spectron@343
12+
13+ export function getSpectronAsyncClient ( client : SpectronClient , browserWindow : SpectronWindow ) {
14+ const typedAsyncClient : SpectronAsyncClient = {
15+ browserWindow : ( browserWindow as unknown ) as SpectronAsyncWindow ,
16+ $ : ( selector : string ) => client . $ ( selector ) ,
17+ $$ : ( selector : string ) => client . $$ ( selector ) ,
18+ click : async ( selector : string ) => {
19+ const element = await client . $ ( selector ) ;
20+ return await element . click ( ) ;
21+ } ,
22+ execute : ( script : string | ( ( ...args : any [ ] ) => void ) , ...args : any [ ] ) =>
23+ client . execute ( script , ...args ) ,
24+ executeAsync : ( script : string | ( ( ...args : any [ ] ) => void ) , ...args : any [ ] ) =>
25+ client . executeAsync ( script , ...args ) ,
26+ getAttribute : async ( selector : string , attributeName : string ) => {
27+ const element = await client . $ ( selector ) ;
28+ return await element . getAttribute ( attributeName ) ;
29+ } ,
30+ getText : async ( selector : string ) => {
31+ const element = await client . $ ( selector ) ;
32+ return await element . getText ( ) ;
33+ } ,
34+ isEnabled : async ( selector : string ) => {
35+ const element = await client . $ ( selector ) ;
36+ return await element . isEnabled ( ) ;
37+ } ,
38+ keys : ( keys : string ) => client . keys ( keys ) ,
39+ pause : ( milliseconds : number ) => client . pause ( milliseconds ) ,
40+ waitForEnabled : async ( selector : string , milliseconds ?: number , reverse ?: boolean ) => {
41+ const element = await client . $ ( selector ) ;
42+ return await element . waitForEnabled ( {
43+ timeout : milliseconds ,
44+ reverse,
45+ } ) ;
46+ } ,
47+ waitForExist : async (
48+ selector : string ,
49+ milliseconds ?: number ,
50+ reverse ?: boolean ,
51+ timeoutMsg ?: string ,
52+ ) => {
53+ const element = await client . $ ( selector ) ;
54+ return await element . waitForExist ( {
55+ timeout : milliseconds ,
56+ reverse,
57+ timeoutMsg,
58+ } ) ;
59+ } ,
60+ waitUntil : ( condition : ( ) => Promise < Boolean > , options ?: WebDriverIO . WaitUntilOptions ) =>
61+ client . waitUntil ( condition , options ) ,
62+ } ;
63+ return typedAsyncClient ;
64+ }
1365
1466export interface SpectronAsyncWindow {
1567 restore ( ) : Promise < void > ;
@@ -20,26 +72,25 @@ export interface SpectronAsyncWindow {
2072export interface SpectronAsyncClient {
2173 // https://github.com/electron-userland/spectron/blob/cd733c4bc6b28eb5a1041ed79eef5563e75432ae/lib/api.js#L311
2274 browserWindow : SpectronAsyncWindow ;
23-
24- $ ( selector : string ) : Promise < WebDriverIO . RawResult < any > > ;
25- $$ ( selector : string ) : Promise < WebDriverIO . RawResult < any > [ ] > ;
26- click ( selector ?: string ) : Promise < void > ;
75+ $ ( selector : string ) : Promise < WebDriverIO . Element > ;
76+ $$ ( selector : string ) : Promise < WebDriverIO . Element [ ] > ;
77+ click ( selector : string ) : Promise < void > ;
2778 executeAsync ( script : string | ( ( ...args : any [ ] ) => void ) , ...args : any [ ] ) : Promise < any > ;
2879 execute ( script : string | ( ( ...args : any [ ] ) => void ) , ...args : any [ ] ) : Promise < any > ;
29- getAttribute < P > ( selector : string , attributeName : string ) : Promise < P > ;
80+ getAttribute ( selector : string , attributeName : string ) : Promise < string > ;
3081 getText ( selector ?: string ) : Promise < string > ;
3182 isEnabled ( selector ?: string ) : Promise < boolean > ;
3283 keys ( keys : string ) : Promise < void > ;
3384 pause ( milliseconds : number ) : Promise < void > ;
3485 waitForEnabled ( selector : string , milliseconds ?: number , reverse ?: boolean ) : Promise < boolean > ;
35- waitForExist ( selector : string , milliseconds ?: number , reverse ?: boolean ) : Promise < boolean > ;
36- waitUntil (
37- condition : ( ) =>
38- | boolean
39- | Promise < boolean >
40- | ( WebDriverIO . Client < WebDriverIO . RawResult < any > > & WebDriverIO . RawResult < any > ) ,
41- timeout ?: number ,
86+ waitForExist (
87+ selector : string ,
88+ milliseconds ?: number ,
89+ reverse ?: boolean ,
4290 timeoutMsg ?: string ,
43- interval ?: number ,
91+ ) : Promise < boolean > ;
92+ waitUntil (
93+ condition : ( ) => Promise < Boolean > ,
94+ options ?: WebDriverIO . WaitUntilOptions ,
4495 ) : Promise < boolean > ;
4596}
0 commit comments