将测试模块导入主测试 - Nightwatch
Import test module into main test - Nightwatch
我正在学习 Nightwatch 并且已经成功通过了我的第一个测试 运行ning。我现在想更好地组织我的测试,并使用一堆可重用的通用测试,我可以构建一个主测试并继续进行。我尝试使用下面的代码进行设置,但我不太熟悉 Node 导出,(事实上我更愿意使用 es6 导入/导出,所以如果有人能对此有所了解,我将不胜感激) 所以我可能犯了一些基本错误:
文件结构
Tests
-auth
-- auth.js
index.js
nightwatch.json
{
"src_folders" : ["tests"],
"output_folder" : "reports",
"selenium" : {
"start_process" : true,
"server_path" : "./bin/selenium-server-standalone-3.8.1.jar",
"log_path" : "",
"port" : 4444,
"cli_args" : {
"webdriver.chrome.driver" : "./bin/chromedriver"
}
},
"test_settings" : {
"default" : {
"launch_url" : "http://localhost",
"selenium_port" : 4444,
"selenium_host" : "localhost",
"desiredCapabilities": {
"browserName": "chrome",
"javascriptEnabled": true,
"acceptSslCerts": true
}
}
}
}
package.json
{
"name": "mostesting",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test-e2e": "nightwatch"
},
"author": "",
"license": "ISC",
"dependencies": {
"babel-plugin-add-module-exports": "^0.2.1",
"nightwatch": "^0.9.19",
"selenium-webdriver": "^4.0.0-alpha.1"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-preset-es2015": "^6.24.1"
}
}
index.js
const auth = require('./auth/auth.js')
module.exports = {
'Log into system': auth.login()
}
auth.js
exports.login = function (client) {
client
.url('http://myurl.co.uk')
.waitForElementVisible('body', 1000)
.assert.title('Reach - Log in')
.assert.visible('#UserName')
.setValue('#UserName', 'XXXX')
.assert.visible('#Password')
.setValue('#Password', 'XXXX')
.assert.visible('input[value="Login"]')
.click('input[value="Login"]')
.waitForElementVisible('img.test', 10000, false)
}
当我 运行 测试打开时,它 运行 并通过了,但最后我收到以下控制台错误:
OK. 6 assertions passed. (8.549s)
There was an error while starting the test runner:
TypeError: Cannot read property 'url' of undefined
at Object.exports.login (/Applications/MAMP/htdocs/mostesting/tests/auth/auth.js:3:5)
at Object.<anonymous> (/Applications/MAMP/htdocs/mostesting/tests/index.js:4:27)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at new Module (/Applications/MAMP/htdocs/mostesting/node_modules/nightwatch/lib/runner/module.js:7:23)
谁能指出我哪里出错了?
非常感谢。
定义测试 运行 时,无需调用测试 运行 函数 auth.login()
。您需要导出此函数,以便 Nightwatch 可以执行它:
// index.js
const auth = require('./auth/auth.js')
module.exports = {
'Log into system': auth.login
}
我正在学习 Nightwatch 并且已经成功通过了我的第一个测试 运行ning。我现在想更好地组织我的测试,并使用一堆可重用的通用测试,我可以构建一个主测试并继续进行。我尝试使用下面的代码进行设置,但我不太熟悉 Node 导出,(事实上我更愿意使用 es6 导入/导出,所以如果有人能对此有所了解,我将不胜感激) 所以我可能犯了一些基本错误:
文件结构
Tests
-auth
-- auth.js
index.js
nightwatch.json
{
"src_folders" : ["tests"],
"output_folder" : "reports",
"selenium" : {
"start_process" : true,
"server_path" : "./bin/selenium-server-standalone-3.8.1.jar",
"log_path" : "",
"port" : 4444,
"cli_args" : {
"webdriver.chrome.driver" : "./bin/chromedriver"
}
},
"test_settings" : {
"default" : {
"launch_url" : "http://localhost",
"selenium_port" : 4444,
"selenium_host" : "localhost",
"desiredCapabilities": {
"browserName": "chrome",
"javascriptEnabled": true,
"acceptSslCerts": true
}
}
}
}
package.json
{
"name": "mostesting",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test-e2e": "nightwatch"
},
"author": "",
"license": "ISC",
"dependencies": {
"babel-plugin-add-module-exports": "^0.2.1",
"nightwatch": "^0.9.19",
"selenium-webdriver": "^4.0.0-alpha.1"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-preset-es2015": "^6.24.1"
}
}
index.js
const auth = require('./auth/auth.js')
module.exports = {
'Log into system': auth.login()
}
auth.js
exports.login = function (client) {
client
.url('http://myurl.co.uk')
.waitForElementVisible('body', 1000)
.assert.title('Reach - Log in')
.assert.visible('#UserName')
.setValue('#UserName', 'XXXX')
.assert.visible('#Password')
.setValue('#Password', 'XXXX')
.assert.visible('input[value="Login"]')
.click('input[value="Login"]')
.waitForElementVisible('img.test', 10000, false)
}
当我 运行 测试打开时,它 运行 并通过了,但最后我收到以下控制台错误:
OK. 6 assertions passed. (8.549s)
There was an error while starting the test runner:
TypeError: Cannot read property 'url' of undefined
at Object.exports.login (/Applications/MAMP/htdocs/mostesting/tests/auth/auth.js:3:5)
at Object.<anonymous> (/Applications/MAMP/htdocs/mostesting/tests/index.js:4:27)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at new Module (/Applications/MAMP/htdocs/mostesting/node_modules/nightwatch/lib/runner/module.js:7:23)
谁能指出我哪里出错了?
非常感谢。
定义测试 运行 时,无需调用测试 运行 函数 auth.login()
。您需要导出此函数,以便 Nightwatch 可以执行它:
// index.js
const auth = require('./auth/auth.js')
module.exports = {
'Log into system': auth.login
}