如何使用 nightwatch 进行端到端测试 运行?

How can I get e2e test running using nightwatch?

我使用 nightwatch 创建了一个测试,看起来像这样:

var config = require('../nightwatch.conf.js');
pages=require('./pages/general.js');
module.exports = {
    'stuff': function () {
        pages.load();
    }
};

一般.js我得到:

var config = require('../../nightwatch.conf.js');

module.exports = {
    load: function () {
        return this.client
            .url('http://www.bing.com')
            .waitForElementVisible('body', 1000)
            .assert.title('Bing')
    }
};

当我从命令行 运行 执行此操作时,它 运行 执行了两次 (?)、"load" 和 "stuff"。这是输出:

Running:  load                                                                
 √ Element <body> was visible after 163 milliseconds.                         
 √ Testing if the page title equals "Bing".                                   

OK. 2 assertions passed. (5.769s)                                             

[Test] Test Suite                                                             
=====================                                                         

Running:  stuff                                                               
 √ Element <body> was visible after 116 milliseconds.                         
 × Testing if the page title equals "Bing".  - expected "Bing" but got: ""    

如何解决stuff部分的问题?

为了解决您的问题,我建议您将 client 对象传递给 general 模块。 然后注意nightwatch.json文件中的配置。

为了帮助您,我创建了一个完整的 运行ning 示例:

我创建了 3 个文件 app.jsgeneral.jsnightwatch.json

当我运行测试时,结果是:

app.js

var config = require('./nightwatch.json');
pages=require('./general.js');
module.exports = {
    'stuff': function (client){
    pages.load(client);
  }
};

general.js

var config = require('./nightwatch.json');
module.exports = {
    load: function (client) {
        return client
            .url('http://www.bing.com')
            .waitForElementVisible('body', 1000)
            .assert.title('Bing')
    }
};

nightwatch.json

{
    "src_folders": [
        "app.js"
    ],
    "live_output": true,
    "tests_output": "test/tests_output/",
    "detailed_output": true,
    "selenium": {
        "start_process": false,
        "host": "hub.browserstack.com",
        "port": 80
    },
    "test_workers": {
        "enabled": false,
        "workers": "auto"
    },
    "test_settings": {
        "chrome": {
            "selenium_port": 80,
            "selenium_host": "hub.browserstack.com",
            "silent": true,
            "desiredCapabilities": {
                "os": "Windows",
                "os_version": "10",
                "browserName": "chrome",
                "resolution": "1024x768",
                "javascriptEnabled": true,
                "acceptSslCerts": true,
                "browserstack.local": "true",
                "browserstack.video": "true",
                "browserstack.debug": "true",
                "browserstack.localIdentifier": "<yourLocalIdentifier>",
                "browserstack.user": "<yourYserName>",
                "browserstack.key": "<yourKey>"
            }
        }
    }
}

这对你有帮助吗?