I'm trying to perform testing using the selenium web driver, and part of this is waiting for my object to be defined on the page. NOTE: there is nothing in the DOM that will appear when my object is available and it is not an option to change that, so please don't suggest this. I need to check using the console.
Usually after a page is finished loading, I have to wait anywhere between 0-5 seconds for my object to exist, so the idea is to loop window.myObject !== undefined
until it passes, at which point I'm sure that my object exists and I can call myObject.myFunctionCall()
. If I don't perform this wait and just directly call myObject.myFunctionCall()
after the page finishes loading, there's a good chance that I'll receive an error of myObject is not defined
.
When I perform these steps from the console in my browser, it turns out perfectly fine:
let ret = false;
while (ret === false) {
ret = window.myObject !== undefined;
console.log(ret);
}
//now here ret has the value true. myObject is defined and I can continue with the test
myObject.myFunctionCall()
...
But then I try doing this with the selenium driver (this.driver
) with 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);
//for some reason, ret is always null
}
which provides me with the following printout that repeats endlessly until the test fails on 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 am I missing here? Is there a better way to tell if my object is defined using the selenium web driver?