0

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?

Michael Hackman
  • 491
  • 2
  • 6
  • 21

1 Answers1

0

I ended up with a structure like the following:

// Checks if myObject exists
const checkObjectDefined = () => window.myObject !== undefined;

let ObjectExists = false;
while (ObjectExists !== true) {
    ObjectExists = await this.driver.executeScript(checkObjectDefined);
    await this.driver.sleep(50);
}

//continue with test

With a caveat that if I move the checkObjectDefined definition to any other file and import it, it fails with the complaint InvalidArgumentError: invalid argument: 'script' must be a string.

Check this out:

I have a file helpers/myObject_helper.js that I want to put the definition in, but the following fails with the error InvalidArgumentError: invalid argument: 'script' must be a string:

helpers/myObject_helper.js:

// Checks if myObject exists
const checkObjectDefined = () => window.myObject !== undefined;

testcases/myObject_testcase.js:

const ObjectHelper = require('helpers/myObject_helper.js');

let ObjectExists = false;
while (ObjectExists !== true) {
    ObjectExists = await this.driver.executeScript(ObjectHelper.checkObjectDefined);
    await this.driver.sleep(50);
}
Michael Hackman
  • 491
  • 2
  • 6
  • 21