简体   繁体   English

如何在nightwatch js中运行'window'js命令

[英]How to run 'window' js command in nightwatch js

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 我想在功能之前利用夜视仪,我在我的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. 说widnow没有定义。 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 ? 我怎么能在夜视仪上对着我的浏览器使用js窗口? Any help or alternative is highly appreciated 任何帮助或替代方案都非常感谢

update 更新

After reading the answer below, i update my global.js to 阅读下面的答案后,我将我的global.js更新为

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();
    },

};

TO my surprise, self.environment is always undefined. 令我惊讶的是,自我环境总是未定义的。 It does not seem to be running the .exec function. 它似乎没有运行.exec函数。

You can probably use the execute command: 您可以使用execute命令:

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

For more information: http://nightwatchjs.org/api/execute.html 有关更多信息,请访问: http//nightwatchjs.org/api/execute.html

In your case, when using beforeEach you need to move the done() inside the callback function, like so: 在你的情况下,当使用beforeEach你需要在回调函数中移动done() ,如下所示:

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();
    });
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM