1+ /**
2+ * Copyright (c) Microsoft Corporation. All rights reserved.
3+ * Licensed under the MIT License.
4+ */
5+
6+ import { WebDriver , WebElementCondition , By } from "selenium-webdriver" ;
7+ import { IAppiumDriver } from './appiumdriver' ;
8+
9+ export interface IPageObject {
10+ clickAndGotoPage < T extends IPageObject > ( type : ( new ( ...args : any [ ] ) => T ) , by : By , timeout ?: number ) : Promise < T > ;
11+ gotoPage < T extends IPageObject > ( type : ( new ( ...args : any [ ] ) => T ) , timeout ?: number ) : Promise < T > ;
12+ isReadyConditions ( ) : WebElementCondition [ ] ;
13+ waitUntilReady ( timeout ?: number ) : Promise < void > ;
14+ } ;
15+
16+ function getInstance < T > ( type : ( new ( ...args : any [ ] ) => T ) , ...args : any [ ] ) : T {
17+ return new type ( ...args ) ;
18+ } ;
19+
20+ function gotoPage < T extends IPageObject > ( type : ( new ( ...args : any [ ] ) => T ) , driver : IAppiumDriver , timeout ?: number ) : Promise < T > {
21+ const page = getInstance ( type , driver ) ;
22+ return new Promise < T > ( ( resolve ) => {
23+ page . waitUntilReady ( timeout ) . then ( ( ) => resolve ( page ) )
24+ } ) ;
25+ }
26+
27+ function clickAndGotoPage < T extends IPageObject > ( type : ( new ( ...args : any [ ] ) => T ) , driver : IAppiumDriver , by : By , timeout ?: number ) : Promise < T > {
28+ return new Promise < T > ( ( resolve ) => {
29+ driver . get ( by , timeout )
30+ . then ( el => { return el . click ( ) ; } )
31+ . then ( ( ) => { return gotoPage ( type , driver , timeout ) ; } )
32+ . then ( page => resolve ( page ) ) ;
33+ } ) ;
34+ }
35+
36+ export class PageObject implements IPageObject {
37+ private static defaultTimeout : number = 20000 ; //ms
38+ private timeout ?: number ;
39+ private getTimeout ( timeout ?: number ) : number {
40+ if ( timeout )
41+ return timeout ;
42+ if ( this . timeout )
43+ return this . timeout ;
44+ return PageObject . defaultTimeout ;
45+ }
46+
47+ static setDefaultTimeout ( ms : number ) {
48+ this . defaultTimeout = ms ;
49+ }
50+
51+ protected appiumDriver : IAppiumDriver ;
52+
53+ constructor ( dirver : IAppiumDriver , timeout ?: number ) {
54+ this . timeout = timeout ;
55+ this . appiumDriver = dirver ;
56+ }
57+
58+ isReadyConditions ( ) : WebElementCondition [ ] {
59+ throw new Error ( "Not implemented" ) ;
60+ }
61+
62+ waitUntilReady ( timeout ?: number ) : Promise < void > {
63+ const webDriver = this . appiumDriver . seleniumDriver ( ) ;
64+ if ( webDriver ) {
65+ return PageObject . waitForConditions ( webDriver , this . isReadyConditions ( ) , this . getTimeout ( timeout ) ) ;
66+ } else {
67+ return Promise . reject ( "no driver found" ) ;
68+ }
69+ }
70+
71+ clickAndGotoPage < T extends IPageObject > ( type : ( new ( ...args : any [ ] ) => T ) , by : By , timeout ?: number ) : Promise < T > {
72+ return clickAndGotoPage ( type , this . appiumDriver , by , this . getTimeout ( timeout ) ) ;
73+ }
74+
75+ gotoPage < T extends IPageObject > ( type : ( new ( ...args : any [ ] ) => T ) , timeout ?: number ) : Promise < T > {
76+ return gotoPage ( type , this . appiumDriver , this . getTimeout ( timeout ) ) ;
77+ }
78+
79+ private static async waitForConditions ( driver : WebDriver , conditions : WebElementCondition [ ] , timeout ?: number ) {
80+ for ( var i = 0 ; i < conditions . length ; i ++ ) {
81+ await driver . wait ( conditions [ i ] , timeout ) ;
82+ }
83+ }
84+ }
0 commit comments