最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to run 'window' js command in nightwatch js - Stack Overflow

programmeradmin0浏览0评论

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.

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

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

};

and i my test

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

However, my code failed at

self.environment = browser.window.navigator.userAgent;

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

update

After reading the answer below, i update my global.js to

var self = module.exports = {
    environment: undefined,

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

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

};

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

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.

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

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

};

and i my test

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

However, my code failed at

self.environment = browser.window.navigator.userAgent;

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

update

After reading the answer below, i update my global.js to

var self = module.exports = {
    environment: undefined,

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

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

};

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

Share Improve this question edited Jun 6, 2016 at 6:45 Nuru Salihu asked Jun 6, 2016 at 5:57 Nuru SalihuNuru Salihu 4,95818 gold badges73 silver badges120 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

You can probably use the execute mand:

browser.execute(function(data) {
    return window.navigator.userAgent;
}, [], function(result) {
    self.environment = result.value;
});

For more information: http://nightwatchjs/api/execute.html

In your case, when using beforeEach you need to move the done() inside the callback function, like so:

beforeEach: function (browser, done) {
    browser.execute(function(data) {
        return window.navigator.userAgent;
    }, [], function(result) {
        console.log('it es here ', result);
        self.environment = result.value;
        console.log("Run against: " + self.environment);
        done();
    });
},
发布评论

评论列表(0)

  1. 暂无评论