How to run 'window' js command in nightwatch js
<p>Please i have some tests that varies depending on my device. I have to determine whether my device is mobile or desktop before beginning of a test.</p>

<p>I wanted to exploit the nightwatch before function and i added below in my globals.js</p>

<pre><code>var self = module.exports = {
    environment: undefined,
    beforeEach: function (done) {
        self.environment = browser.window.navigator.userAgent;
        console.log("Run against: " + self.environment);
        done();
    },

};
</code></pre>

<p>and i my test </p>

<pre><code>module.exports = {
    tags: ['assetindex'],
    'visit': function(browser) {
        (/Mobile/.test(browser.globals.environment)) ?
            browser
                .page.assetindex().mobileVisit()
                .end()
        :
            browser
                .page.assetindex().mobileVisit()
                .end()
    }
};
</code></pre>

<p>However, my code failed at </p>

<p><code>self.environment = browser.window.navigator.userAgent;</code></p>

<p>saying widnow not defined. The error makes sense considering i am not running against a browser object . Please how do i achieve that? How can i the js window against my browser in nightwatch ? Any help or alternative is highly appreciated</p>

<p><strong>update</strong> </p>

<p>After reading the answer below, i update my global.js to </p>

<pre><code>var self = module.exports = {
    environment: undefined,

    beforeEach: function (browser, done) {
        browser.execute(function(data) {
            return window.navigator.userAgent;
        }, [], function(result) {
            console.log('it comes here ', result);
            self.environment = result.value;
        });

        console.log("Run against: " + self.environment);
        done();
    },

};
</code></pre>

<p>TO my surprise, self.environment is always undefined. It does not seem to be running the .exec function. </p>