title: Selenium - check that an object is defined before using it
body: <p>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.
  <strong>NOTE</strong>: 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.</p>
  
  <p>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 <code>window.myObject !== undefined</code> until it passes, at which point I'm sure that my object exists and I can call <code>myObject.myFunctionCall()</code>.  If I don't perform this wait and just directly call <code>myObject.myFunctionCall()</code> after the page finishes loading, there's a good chance that I'll receive an error of <code>myObject is not defined</code>.</p>
  
  <p>When I perform these steps from the console in my browser, it turns out perfectly fine:</p>
  
  <pre><code>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()
  ...
  </code></pre>
  
  <p>But then I try doing this with the selenium driver (<code>this.driver</code>) with the following code:</p>
  
  <pre><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
  }
  </code></pre>
  
  <p>which provides me with the following printout that repeats endlessly until the test fails on <code>Error: function timed out, ensure the promise resolves within 30000 milliseconds</code>:</p>
  
  <pre><code>Promise { &lt;pending&gt; }
  Promise { null }
  null
  Promise { &lt;pending&gt; }
  Promise { null }
  null
  Promise { &lt;pending&gt; }
  Promise { null }
  null
  ...
  </code></pre>
  
  <p>What am I missing here? Is there a better way to tell if my object is defined using the selenium web driver?</p>