@@ -2,13 +2,13 @@ import fetch from 'node-fetch';
22import log from './logger' ;
33import { waitUntil , TimeoutError } from 'async-wait-until' ;
44
5- const defaultConfig = {
6- timeout : '10000' ,
7- intervalBetweenAttempts : '500' ,
8- } ;
5+ let map = new Map ( ) ;
96
10- export async function find ( driver , args , cliArgs ) {
11- _getTimeout ( cliArgs ) ;
7+ export async function find ( driver , args ) {
8+ const elementWaitProps = JSON . parse ( JSON . stringify ( driver . opts . plugin [ 'element-wait' ] ) ) ;
9+ const session = driver . sessionId ;
10+ _setTimeout ( elementWaitProps , session ) ;
11+ let timeoutProp = _getTimeout ( session ) ;
1212 const locatorArgs = JSON . parse ( JSON . stringify ( args ) ) ;
1313 const strategy = locatorArgs [ 0 ] ;
1414 const selector = locatorArgs [ 1 ] ;
@@ -22,10 +22,7 @@ export async function find(driver, args, cliArgs) {
2222 }
2323 } ;
2424 try {
25- const element = await waitUntil ( predicate , {
26- timeout : defaultConfig . timeout ,
27- intervalBetweenAttempts : defaultConfig . intervalBetweenAttempts ,
28- } ) ;
25+ const element = await waitUntil ( predicate , timeoutProp ) ;
2926 if ( element . value . ELEMENT ) {
3027 log . info ( `Element with ${ strategy } strategy for ${ selector } selector found.` ) ;
3128 let elementViewState = await elementIsDisplayed ( driver , element . value . ELEMENT ) ;
@@ -38,18 +35,19 @@ export async function find(driver, args, cliArgs) {
3835 } catch ( e ) {
3936 if ( e instanceof TimeoutError ) {
4037 throw new Error (
41- `Time out after waiting for element ${ selector } for ${ defaultConfig . timeout } `
38+ `Time out after waiting for element ${ selector } for ${ timeoutProp . timeout } ms `
4239 ) ;
4340 } else {
4441 console . error ( e ) ;
4542 }
4643 }
4744}
4845
49- export async function elementEnabled ( driver ) {
46+ export async function elementEnabled ( driver , el ) {
47+ let timeoutProp = _getTimeout ( driver . sessionId ) ;
5048 const predicate = async ( ) => {
51- const element = await driver . elementEnabled ( ) ;
52- if ( element . value === 'true' ) {
49+ const element = await driver . elementEnabled ( el ) ;
50+ if ( element ) {
5351 return element ;
5452 } else {
5553 log . info ( 'Waiting to find element to be enabled' ) ;
@@ -58,14 +56,11 @@ export async function elementEnabled(driver) {
5856 } ;
5957
6058 try {
61- await waitUntil ( predicate , {
62- timeout : defaultConfig . timeout ,
63- intervalBetweenAttempts : defaultConfig . intervalBetweenAttempts ,
64- } ) ;
59+ await waitUntil ( predicate , timeoutProp ) ;
6560 } catch ( e ) {
6661 if ( e instanceof TimeoutError ) {
6762 throw new Error (
68- `Time out after waiting for element to be enabled for ${ defaultConfig . timeout } `
63+ `Time out after waiting for element to be enabled for ${ timeoutProp . timeout } `
6964 ) ;
7065 } else {
7166 console . error ( e ) ;
@@ -119,16 +114,34 @@ function sessionInfo(driver, strategy, selector) {
119114 }
120115}
121116
122- function _getTimeout ( cliArgs ) {
123- if ( cliArgs . timeout != undefined && cliArgs . timeout !== defaultConfig . timeout ) {
124- defaultConfig . timeout = cliArgs . timeout ;
125- log . info ( `Timeout is set to ${ defaultConfig . timeout } ` ) ;
126- }
127- if (
128- cliArgs . intervalBetweenAttempts != undefined &&
129- cliArgs . intervalBetweenAttempts !== defaultConfig . intervalBetweenAttempts
130- ) {
131- defaultConfig . intervalBetweenAttempts = cliArgs . intervalBetweenAttempts ;
132- log . info ( `Internal between attempts is set to ${ defaultConfig . intervalBetweenAttempts } ` ) ;
117+ function _setTimeout ( elementWaitProps , session ) {
118+ if ( ! map . has ( session ) ) {
119+ log . info ( `Timeout properties not set for session ${ session } , trying to set one` ) ;
120+ let defaultTimeoutProp = {
121+ timeout : 10000 ,
122+ intervalBetweenAttempts : 500 ,
123+ } ;
124+ if (
125+ elementWaitProps . timeout != undefined &&
126+ elementWaitProps . timeout !== defaultTimeoutProp . timeout
127+ ) {
128+ defaultTimeoutProp . timeout = elementWaitProps . timeout ;
129+ log . info ( `Timeout is set to ${ defaultTimeoutProp . timeout } ` ) ;
130+ }
131+ if (
132+ elementWaitProps . intervalBetweenAttempts != undefined &&
133+ elementWaitProps . intervalBetweenAttempts !== defaultTimeoutProp . intervalBetweenAttempts
134+ ) {
135+ defaultTimeoutProp . intervalBetweenAttempts = elementWaitProps . intervalBetweenAttempts ;
136+ log . info ( `Internal between attempts is set to ${ defaultTimeoutProp . intervalBetweenAttempts } ` ) ;
137+ }
138+ map . set ( session , defaultTimeoutProp ) ;
133139 }
140+ log . info (
141+ `Timeout properties set for session ${ session } is ${ JSON . stringify ( _getTimeout ( session ) ) } ms`
142+ ) ;
143+ }
144+
145+ function _getTimeout ( session ) {
146+ return map . get ( session ) ;
134147}
0 commit comments