Search code examples
node.jsselenium-webdriverwebdriver-io

How to Get Window Variable Using WebdriverIO


I am trying to run webdriverio with PhantomJS/Chrome to load a page and then grab the window object for use with other scripts. For some reason I am unable to get the window object. Everytime I get, I end up seeing output like this:

Title is: XXXXX
{ state: 'pending' }

Using the following script:

var webdriverio = require('webdriverio');
var options = {
    desiredCapabilities: {
        browserName: 'chrome',
        logLevel: 'verbose'
    }
};

var client = webdriverio.remote(options);

client
     .init()
     .url('https://xxxx.com')
     .waitUntil(function () {
         return client.execute(function () {
             return Date.now() - window.performance.timing.loadEventEnd > 40000;
        }).then(function (result) {
             console.log(window);
             return window;
         });
     })
     .end();

Does anyone know how I can fix my code so that the window object is returned to my NodeJS console app after the page is completely loaded?

Thanks!


Solution

  • Window is an object in the browser's DOM, so it's only available inside of the 'execute' function. If you wanted access to it, you could return it from your 'execute' function:

    return client.execute(function () {
         return window;
    }).then(function (result) {
         console.log(result);
    });