Currently, I am undergoing testing with the Selenium web driver and one of the requirements is to wait for my object to be defined on the page. Important: There are no visible changes in the DOM to indicate when my object is ready, and it's not feasible to alter that. Therefore, please refrain from suggesting such a modification as it is imperative for me to verify using the console.
Typically, after a page finishes loading, there is a waiting period ranging between 0-5 seconds until my object becomes accessible. The approach involves looping window.myObject !== undefined
until this condition validates, at which point I can confidently execute myObject.myFunctionCall()
. If I skip this waiting step and directly call myObject.myFunctionCall()
post page load completion, there's a high probability of encountering an error stating myObject is not defined
.
On manual execution via the browser console, these steps work flawlessly:
let ret = false;
while (ret === false) {
ret = window.myObject !== undefined;
console.log(ret);
}
// At this stage, ret holds true value indicating that myObject is defined and test continuation is feasible
myObject.myFunctionCall()
...
However, when attempting the same process with the selenium driver (this.driver
) by implementing the following code:
let ret = null;
while (ret === null) {
let val = this.driver.executeScript("window.myObject !== undefined;"); //returns promise
console.log(val);
ret = await val; //await promise resolution and assign value to ret
console.log(val);
console.log(ret);
//Unexpectedly, 'ret' remains null regardless
}
This results in a continuous cycle of output leading up to test failure accompanied by
Error: function timed out, ensure the promise resolves within 30000 milliseconds
:
Promise { <pending> }
Promise { null }
null
Promise { <pending> }
Promise { null }
null
Promise { <pending> }
Promise { null }
null
...
What could I be overlooking here? Is there an alternative method within the Selenium web driver to determine if my object is defined?