我使用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 之前运行,因此我尝试在断言之前插入暂停。
如有任何想法,将不胜感激。