我是E2E测试和Grunt的新手。我正在使用Nightwatch.js, PahantomJS和Mocha进行端到端测试。
如果没有grunt,测试通过,但是如果有grunt,我得到一个错误的"client"。
没有咕噜声:
-
启动Selenium server
$ selenium-server -p 4444 -role hub
-
Register Ghost Driver to Selenium Hub
$ node_modules/phantomjs/bin/phantomjs --webdriver=8080 --webdriver-selenium-grid-hub=http://127.0.0.1:4444
-
执行守夜
$ node_modules/nightwatch/bin/nightwatch -c bin/nightwatch.json -t tests/test.js
,然后测试通过。
With grunt:
我得到这个错误TypeError: undefined is not a function
与客户端。
console.log(client);
结果为
function (err){
if (err instanceof Error || toString.call(err) === "[object Error]") return done(err);
if (null != err) {
if (Object.prototype.toString.call(err) === '[object Object]') {
return done(new Error('done() invoked with non-Error: ' + JSON.stringify(err)));
} else {
return done(new Error('done() invoked with non-Error: ' + err));
}
}
done();
}
我的代码就像下面的脚本。
. js
describe("sample test", function() { it("should say no item", function(client) { console.log("client:", client); client .url("http://sample.com/") .waitForElementVisible("body", 10000) .click("a.link1") .waitForElementVisible(".article", 10000) .expect.element("div.cart").text.to.contain("no item"); }); });
Gruntfile.js
module.exports = function(grunt) { var nightwatch = require('nightwatch'); nightwatch.initGrunt(grunt); grunt.initConfig({ nightwatch: { options: { cwd: "./" }, "default" : {}, browserstack: { argv: { env: "browserstack" }, settings: { silent: true } }, "all" : { argv: { env: "default, browserstack" } } }, mochacli: { options: { colors: true, "check-leaks": false, ui: "bdd", reporter: "spec", timeout: 20000 }, e2e: ["tests/*.js"] } }); require("load-grunt-tasks")(grunt); grunt.registerTask("nightwatch", [ "selenium_phantom_hub", "mochacli:e2e", "selenium_stop" ]); grunt.registerTask("default", ["nightwatch"]); };
package.json
{ "name": "Nightwatch-sample", "description": "test using Nightwatch.js", "version": "1.1.0", "dependencies": { "phantomjs": "^1.9.8", "nightwatch": "^0.8.4", "chai-nightwatch": "~0.1.x", "mocha-nightwatch": "2.2.6" }, "devDependencies": { "chai": "^3.2.0", "chai-nightwatch": "^0.1.1", "grunt": "~0.4.4", "grunt-mocha-cli": "^1.14.0", "grunt-selenium-webdriver": "^0.2.451", "load-grunt-tasks": "^3.3.0" } }
nightwatch.json
{ "src_folders" : ["tests"], "output_folder" : "reports", "custom_commands_path" : "", "custom_assertions_path" : "", "page_objects_path" : "", "globals_path" : "", "selenium" : { "start_process" : false, "server_path" : "", "log_path" : "", "host" : "127.0.0.1", "port" : 4444, "cli_args" : { "webdriver.chrome.driver" : "", "webdriver.ie.driver" : "" } }, "test_settings" : { "default" : { "launch_url" : "http://localhost", "selenium_port" : 4444, "selenium_host" : "localhost", "silent": true, "firefox_profile": false, "chrome_driver" : "", "screenshots" : { "enabled" : true, "path" : "tests/screenshots", "on_failure": true }, "test_runner": "mocha", "desiredCapabilities": { "browserName": "phantomjs", "javascriptEnabled": true, "acceptSslCerts": true, "phantomjs.page.settings.userAgent": "Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4" } } } }
@ filtype已经在注释中提到为什么你的代码不能像你想的那样工作
详细说明一下:你使用'client'作为函数参数的地方被mocha用来传递一个'done'函数,你可以用它来通知mocha你的测试是在异步情况下完成的-这就是为什么console.log(client)正在打印它是什么。你可能会得到这样的代码,因为nightwatch有这样一个结构来提供客户端。
所以要让它运行,你应该将参数'client'重命名为'done',并在测试完成时调用done()。您还应该以某种方式自己实例化客户端。
然而,我确实认为nightwatch和mocha并不是一个很好的组合,因为它们提供了类似的功能,不能很好地合并。也许你应该直接在值班的时候不加摩卡?谷歌说可以做到:-)https://github.com/gextech/grunt-nightwatch
Mocha足够灵活,如果你真的想让它做很多事情,但我想说在Mocha中运行另一个类似系统的测试运行器是在拉伸它。
更新:显然夜巡最近增加了一些摩卡支持https://github.com/nightwatchjs/nightwatch/issues/501我不知道在什么级别。如果你没有使用mocha,我仍然认为你最好独立运行它。