我使用Nightwatch.js进行了简单的烟雾测试。他们在本地通过,但是在CircleCI上失败了。
源代码:
const WAIT = 1000;
const NODE_PORT = process.env.NODE_PORT || 3000;
module.exports = {
'Smoketest' (browser) {
browser
.url(`http://localhost:${NODE_PORT}/`)
.waitForElementVisible('body', WAIT)
.pause(300)
.assert.containsText('body', 'Client render')
.end();
}
};
本地输出:
> cross-env NODE_PORT=3000 NODE_PATH=source babel-node source/test/e2e.js
Running with config:
{ logDir: '/Users/eric/Dropbox/dev/react-easy-universal/reports',
reportDir: '/Users/eric/Dropbox/dev/react-easy-universal/reports/test-e2e',
nightwatchConfig: '/Users/eric/Dropbox/dev/react-easy-universal/nightwatch.json',
webpackConfig: '/Users/eric/Dropbox/dev/react-easy-universal/webpack.config.js',
port: '3000',
server: [Object] }
[Smoketest] Test Suite
======================
Running: Smoketest
✔ Element <body> was visible after 58 milliseconds.
✔ Testing if element <body> contains text: "Client render".
OK. 2 assertions passed. (2.831s)
CircleCI输出失败:
REPORT_DIR=${CIRCLE_TEST_REPORTS} LOG_DIR=${CIRCLE_ARTIFACTS} npm run test:e2e -s
Running with config:
{ logDir: '/tmp/circle-artifacts.ddv7oJ3',
reportDir: '/tmp/circle-junit.hhJgS6N',
nightwatchConfig: '/home/ubuntu/react-easy-universal/nightwatch.json',
webpackConfig: '/home/ubuntu/react-easy-universal/webpack.config.js',
port: '3000',
server: [Object] }
[Smoketest] Test Suite
======================
Running: Smoketest
✔ Element <body> was visible after 85 milliseconds.
✖ Testing if element <body> contains text: "Client render". - expected "Client render" but got: Untitled
at Object.Smoketest (smoketest.js:10:15)
at Module.call (/home/ubuntu/react-easy-universal/node_modules/nightwatch/lib/runner/module.js:63:34)
at /home/ubuntu/react-easy-universal/node_modules/nightwatch/lib/runner/testcase.js:97:29
at _fulfilled (/home/ubuntu/react-easy-universal/node_modules/q/q.js:834:54)
at self.promiseDispatch.done (/home/ubuntu/react-easy-universal/node_modules/q/q.js:863:30)
at Promise.promise.promiseDispatch (/home/ubuntu/react-easy-universal/node_modules/q/q.js:796:13)
at /home/ubuntu/react-easy-universal/node_modules/q/q.js:556:49
at runSingle (/home/ubuntu/react-easy-universal/node_modules/q/q.js:137:13)
at flush (/home/ubuntu/react-easy-universal/node_modules/q/q.js:125:13)
at nextTickCallbackWith0Args (node.js:452:9)
FAILED: 1 assertions failed and 1 passed (3.242s)
----------------------------------------------------
TEST FAILURE: 1 assertions failed, 1 passed (3.543s)
✖ smoketest
- Smoketest
Testing if element <body> contains text: "Client render". - Expected "Client render" but got: "Untitled" REPORT_DIR=${CIRCLE_TEST_REPORTS} LOG_DIR=${CIRCLE_ARTIFACTS} npm run test:e2e -s returned exit code 1
最初,浏览器从服务器的一些渲染输出开始。服务器渲染中的标题设置为“无标题”。然后,一些JavaScript会在客户端中加载并运行,然后发生客户端渲染,将文本更改为“客户端渲染”。该测试旨在确保客户端渲染按预期工作。
这是
nightwatch.json
:{
"src_folders" : ["source/test/functional/browser"],
"output_folder" : "./reports/test-e2e",
"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,
"screenshots" : {
"enabled" : false,
"path" : ""
},
"desiredCapabilities": {
"browserName": "chrome",
"javascriptEnabled": true,
"acceptSslCerts": true
}
},
"chrome" : {
"desiredCapabilities": {
"browserName": "chrome",
"javascriptEnabled": true,
"acceptSslCerts": true
}
}
}
}
我怀疑这可能是一种竞争状况,认为断言在CircleCI有机会加载和处理JavaScript之前运行,因此我尝试在断言之前插入一个暂停。
任何想法,将不胜感激。
最佳答案
我也遇到了这个问题,看起来(仍然需要重新检查)我找到了解决方案。
最后,请确保您在每个测试中都拥有最后的代码。
browser.end();
关于nightwatch.js - 为什么Nightwatch.js测试在CircleCI上失败?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35305651/