44 */
55
66'use strict' ;
7+ import { WebDriver , Builder , until , Capabilities , WebElementPromise } from 'selenium-webdriver'
8+ import { By2 } from './by2' ;
9+ import { By } from 'selenium-webdriver'
710
8- import { WebDriver , Builder , until } from 'selenium-webdriver'
9- import { By } from './by' ;
11+ interface IAppiumWaitUntilFound {
12+ get ( by : By , timeout ?: number , message ?: string ) : WebElementPromise ;
13+ getByAccessibilityId ( id : string , timeout ?: number , message ?: string ) : WebElementPromise ;
14+ getByName ( name : string , timeout ?: number , message ?: string ) : WebElementPromise ;
15+ getById ( id : string , timeout ?: number , message ?: string ) : WebElementPromise ;
16+ getByclassName ( className : string , timeout ?: number , message ?: string ) : WebElementPromise ;
17+ }
1018
11- export class AppiumDriver {
12- private static windowsCapabilities ( appName : string ) {
13- return {
14- browserName : '' ,
15- platformName : 'windows' ,
16- deviceName : 'WindowsPC' ,
17- app : appName
18- } ;
19+ export interface IAppiumDriver extends IAppiumWaitUntilFound
20+ {
21+ start ( ) : Promise < void > ;
22+ stop ( ) : Promise < void > ;
23+ restart ( ) : Promise < void > ;
24+ isActive ( ) : Promise < boolean > ;
25+ seleniumDriver ( ) : WebDriver ;
26+ lastError ( ) : any ;
27+ sleep ( ms : number ) : Promise < void > ;
28+ }
29+
30+ class AppiumDriver implements IAppiumDriver {
31+ get ( by : By , timeout ?: number , message ?: string | undefined ) : WebElementPromise {
32+ if ( this . webDriver )
33+ return this . webDriver . wait ( until . elementLocated ( by ) , timeout , message ) ;
34+ throw new Error ( "no valid connection" ) ;
1935 }
2036
21- driver_ : WebDriver ;
37+ getByAccessibilityId ( id : string , timeout ?: number | undefined , message ?: string | undefined ) : WebElementPromise {
38+ return this . get ( By2 . accessibilityId ( id ) , timeout , message ) ;
39+ }
2240
23- static createWinAppDriver ( appName : string , url = "http://localhost:4723/wd/hub" ) : Promise < AppiumDriver > {
24- return new Promise < AppiumDriver > ( resolve => {
25- new Builder ( )
26- . usingServer ( url )
27- . withCapabilities ( AppiumDriver . windowsCapabilities ( appName ) )
28- . build ( ) .
29- then ( ( driver ) => { resolve ( new AppiumDriver ( driver ) ) ; } ) ;
30- } ) ;
41+ getByName ( name : string , timeout ?: number | undefined , message ?: string | undefined ) : WebElementPromise {
42+ return this . get ( By2 . name ( name ) , timeout , message ) ;
3143 }
3244
33- webDriver ( ) : WebDriver {
34- return this . driver_ ;
45+ getById ( id : string , timeout ?: number | undefined , message ?: string | undefined ) : WebElementPromise {
46+ return this . get ( By2 . id ( id ) , timeout , message ) ;
3547 }
3648
37- constructor ( driver : WebDriver ) {
38- this . driver_ = driver ;
49+ getByclassName ( className : string , timeout ?: number | undefined , message ?: string | undefined ) : WebElementPromise {
50+ return this . get ( By2 . className ( className ) , timeout , message ) ;
3951 }
4052
41- getByAccessibilityId ( id : string , timeout = 0 , message = undefined ) {
42- return this . driver_ . wait ( until . elementLocated ( By . accessibilityId ( id ) ) , timeout , message ) ;
53+ sleep ( ms : number ) : Promise < void > {
54+ if ( this . webDriver )
55+ return this . webDriver . sleep ( ms ) ;
56+ throw new Error ( "no valid connection" ) ;
4357 }
4458
45- getByName ( name : string , timeout = 0 , message = undefined ) {
46- return this . driver_ . wait ( until . elementLocated ( By . name ( name ) ) , timeout , message ) ;
59+ seleniumDriver ( ) : WebDriver {
60+ if ( this . webDriver )
61+ return this . webDriver ;
62+ throw new Error ( "no valid connection" ) ;
4763 }
4864
49- getById ( id : string , timeout = 0 , message = undefined ) {
50- return this . driver_ . wait ( until . elementLocated ( By . id ( id ) ) , timeout , message ) ;
65+ lastError ( ) {
66+ return this . error_ ;
5167 }
5268
53- getByclassName ( className : string , timeout = 0 , message = undefined ) {
54- return this . driver_ . wait ( until . elementLocated ( By . className ( className ) ) , timeout , message ) ;
69+ start ( ) : Promise < void > {
70+ return new Promise < void > ( resolve => {
71+ new Builder ( )
72+ . usingServer ( this . url_ )
73+ . withCapabilities ( this . capabilities_ )
74+ . build ( )
75+ . then ( driver => { this . webDriver = driver ; resolve ( ) ; } )
76+ . catch ( e => { this . error_ = e ; throw e ; } ) ;
77+ } ) ;
5578 }
5679
57- sleep ( ms : number ) {
58- return this . driver_ . sleep ( ms ) ;
80+ stop ( ) : Promise < void > {
81+ return new Promise < void > ( resolve => {
82+ this . webDriver && this . webDriver
83+ . quit ( )
84+ . then ( ( ) => resolve ( ) )
85+ . catch ( e => { this . error_ = e ; throw e } ) ;
86+ resolve ( ) ;
87+ } ) ;
5988 }
60- quit ( ) {
61- return this . driver_ . quit ( ) ;
89+
90+ restart ( ) : Promise < void > {
91+ return this . _restart ( ) ;
6292 }
93+
94+ private async _restart ( ) {
95+ await this . stop ( ) . catch ( ) ; //ignore stop error
96+ await this . start ( ) ;
97+ }
98+
99+ private capabilities_ : Capabilities | { } ;
100+ private error_ ?: any ;
101+ private url_ : string
102+ private webDriver ?: WebDriver ;
103+
104+ constructor ( capabilities : Capabilities | { } , url : string = "http://localhost:4723/wd/hub" ) {
105+ this . capabilities_ = capabilities ;
106+ this . url_ = url ;
107+ }
108+
109+ isActive ( ) : Promise < boolean > {
110+ return new Promise < boolean > ( resolve => {
111+ this . webDriver && this . webDriver
112+ . getSession ( )
113+ . then ( ( ) => resolve ( true ) , reason => resolve ( false ) )
114+ resolve ( false ) ;
115+ } ) ;
116+ }
117+ }
118+
119+ export function createAppiumWebDriver ( capabilities : Capabilities | { } , url ?: string ) : IAppiumDriver {
120+ return new AppiumDriver ( capabilities , url ) ;
63121}
0 commit comments