@@ -4,88 +4,106 @@ import fetch from 'node-fetch';
44let retryCount = 0 ;
55export default class WaitCommandPlugin extends BasePlugin {
66 async findElement ( next , driver , ...args ) {
7+ this . driver = driver ;
78 let originalRes ;
89 const locatorArgs = JSON . parse ( JSON . stringify ( args ) ) ;
910 this . strategy = locatorArgs [ 0 ] ;
1011 this . selector = locatorArgs [ 1 ] ;
11- await this . _find ( driver , locatorArgs ) ;
12- await this . _elementDisplayed ( driver ) ;
12+ await this . _find ( locatorArgs ) ;
13+ await this . _elementDisplayed ( ) ;
1314 originalRes = await next ( ) ;
1415 retryCount = 0 ;
1516 return originalRes ;
1617 }
1718
18- async _find ( driver ) {
19- const baseUrl = this . _constructSessionUrl ( driver ) ;
20- const response = await fetch (
21- `${ baseUrl } wd/hub/session/${ driver . sessionId } /element` ,
22- {
23- body : JSON . stringify ( {
24- strategy : this . strategy ,
25- selector : this . selector ,
26- context : '' ,
27- multiple : false ,
28- } ) ,
29- method : 'POST' ,
30- headers : { 'Content-Type' : 'application/json' } ,
31- }
32- ) ;
33- const json = await response . json ( ) ;
34- if ( json . value . error ) {
35- if ( retryCount !== 25 ) {
19+ async _find ( ) {
20+ const baseUrl = this . _constructSessionUrl ( ) ;
21+ const element = await this . elementState ( baseUrl ) ;
22+ if ( element . value . error ) {
23+ console . log ( '----' , this . _getTimeout ( ) ) ;
24+ if ( retryCount !== this . _getTimeout ( ) ) {
3625 this . logger . info (
37- `Retrying to find element with ${ this . strategy } strategy for ${ this . selector } selector`
26+ `Waiting for find element with ${ this . strategy } strategy for ${ this . selector } selector`
3827 ) ;
3928 retryCount ++ ;
40- await this . _find ( driver ) ;
29+ await this . _find ( ) ;
4130 }
4231 }
4332
44- if ( json . sessionId && json . value . ELEMENT ) {
45- this . element = json . value . ELEMENT ;
33+ if ( element . sessionId && element . value . ELEMENT ) {
34+ this . element = element . value . ELEMENT ;
4635 this . logger . info (
4736 `Element with ${ this . strategy } strategy for ${ this . selector } selector found.`
4837 ) ;
4938 retryCount = 0 ;
5039 }
5140 }
5241
53- async _elementDisplayed ( driver ) {
54- this . logger . info ( `Checking if ${ this . selector } element is displayed` ) ;
55- const baseUrl = this . _constructSessionUrl ( driver ) ;
42+ async elementState ( baseUrl ) {
5643 const response = await fetch (
57- `${ baseUrl } wd/hub/session/${ driver . sessionId } /element/ ${ this . element } /attribute/displayed ` ,
44+ `${ baseUrl } wd/hub/session/${ this . driver . sessionId } /element` ,
5845 {
59- method : 'GET' ,
46+ body : JSON . stringify ( {
47+ strategy : this . strategy ,
48+ selector : this . selector ,
49+ context : '' ,
50+ multiple : false ,
51+ } ) ,
52+ method : 'POST' ,
6053 headers : { 'Content-Type' : 'application/json' } ,
6154 }
6255 ) ;
63- const json = await response . json ( ) ;
64- if ( json . value . error ) {
65- if ( retryCount !== 25 ) {
56+ return await response . json ( ) ;
57+ }
58+
59+ async _elementDisplayed ( ) {
60+ this . logger . info ( `Checking if ${ this . selector } element is displayed` ) ;
61+ const baseUrl = this . _constructSessionUrl ( ) ;
62+ const response = await this . elementDisplayed ( baseUrl ) ;
63+ if ( response . value . error ) {
64+ if ( retryCount !== this . _getTimeout ( ) ) {
6665 this . logger . info (
6766 `Retrying to check whether ${ this . selector } element is displayed or not`
6867 ) ;
6968 retryCount ++ ;
70- await this . _elementDisplayed ( driver ) ;
69+ await this . _elementDisplayed ( ) ;
7170 }
7271 }
73- if ( json . sessionId && json . value === 'true' ) {
72+ if ( response . sessionId && response . value === 'true' ) {
7473 this . logger . info ( `${ this . selector } element is displayed.` ) ;
7574 retryCount = 0 ;
7675 }
7776 }
7877
79- _getAutomationName ( driver ) {
80- return driver . caps . automationName ;
78+ async elementDisplayed ( baseUrl ) {
79+ const response = await fetch (
80+ `${ baseUrl } wd/hub/session/${ this . driver . sessionId } /element/${ this . element } /attribute/displayed` ,
81+ {
82+ method : 'GET' ,
83+ headers : { 'Content-Type' : 'application/json' } ,
84+ }
85+ ) ;
86+ return await response . json ( ) ;
87+ }
88+
89+ _getAutomationName ( ) {
90+ return this . driver . caps . automationName ;
8191 }
8292
83- _constructSessionUrl ( driver ) {
84- const automationName = this . _getAutomationName ( driver ) ;
93+ _constructSessionUrl ( ) {
94+ const automationName = this . _getAutomationName ( ) ;
8595 if ( automationName === 'XCuiTest' ) {
86- return `${ driver . wda . wdaBaseUrl } :${ driver . wda . wdaLocalPort } /` ;
96+ return `${ this . driver . wda . wdaBaseUrl } :${ this . driver . wda . wdaLocalPort } /` ;
97+ } else {
98+ return `http://${ this . driver . uiautomator2 . host } :${ this . driver . uiautomator2 . systemPort } /` ;
99+ }
100+ }
101+
102+ _getTimeout ( ) {
103+ if ( this . driver . caps [ 'element-wait' ] ) {
104+ return ( this . timeout = this . driver . caps [ 'element-wait' ] ) ;
87105 } else {
88- return `http:// ${ driver . uiautomator2 . host } : ${ driver . uiautomator2 . systemPort } /` ;
106+ return ( this . timeout = 30 ) ;
89107 }
90108 }
91109}
0 commit comments