问题描述
我有一个 Django 项目,我正在尝试设置 nightwatch JS,但我似乎无法解决 selinum 驱动程序的问题。
我有一个这样的 node_modules
目录(显示重要区域)
node_modules
.bin
chromedriver
geckodriver
在我的根文件夹中,我编写了一个名为 custom_test.js
的测试:
module.exports = {
'NW test on Google' : function (client) {
client
.url('http://www.google.com')
.waitForElementVisible('body',1000)
.assert.title('Google')
.assert.visible('input[type=text]')
.setValue('input[type=text]','Nightwatch JS')
.waitForElementVisible('button[name=btnG]',1000)
.click('button[name=btnG]')
.pause(1000)
.assert.containsText('ol#rso li:first-child','Nightwatch.js | Node.js powered End-to-End testing framework')
.end()
}
}
package.json
中的脚本:
"nightwatch": "./node_modules/.bin/nightwatch"
为了以防万一,这里显示了来自 chromedriver 和 geckdriver 的 devDependenices
"chromedriver": "^88.0.0",
"geckodriver": "^1.22.1",
现在nightwatch.json
{
"src_folders" : [""],"webdriver" : {
"start_process": true,"server_path": "node_modules/.bin/geckodriver","port": 9515
},"test_settings" : {
"default" : {
"desiredCapabilities": {
"browserName": "firefox"
}
}
}
}
当我运行时:npm run nightwatch custom_test.js
我简单地得到:
检索新会话时发生错误:“预期的浏览器二进制位置,但无法在默认位置找到二进制文件,未提供‘moz:firefoxOptions.binary’功能,且命令行上未设置二进制标志”
>如果我将 nightwatch.json
文件更改为指向 chromedriver,我会收到此错误:
{
"src_folders" : [""],"server_path": "node_modules/.bin/chromedriver","test_settings" : {
"default" : {
"desiredCapabilities": {
"browserName": "chrome"
}
}
}
}
检索新会话时出错:“未知错误:找不到 Chrome 二进制文件”
这对我来说没有意义,因为配置只是告诉我通过 npm install 运行驱动程序的安装:
https://nightwatchjs.org/gettingstarted/configuration/
我做错了什么?
更新
将配置更改为 nightwatch.conf.js:
module.exports = {
// An array of folders (excluding subfolders) where your tests are located;
// if this is not specified,the test source must be passed as the second argument to the test runner.
src_folders: ["tests"],webdriver: {
start_process: true,port: 4444,server_path: require('chromedriver').path,cli_args: [
]
},test_settings: {
default: {
desiredCapabilities: {
browserName: 'chrome',}
}
}
}
提供此错误:
检索新会话时出错:“未知错误:找不到 Chrome 二进制文件”
这里是整个输出:
npm run nightwatch custom_test.js
> exactestate@1.0.0 nightwatch /opt/project
> nightwatch "custom_test.js"
[Custom Test] Test Suite
========================
⚠ Error connecting to localhost on port 4444.
_________________________________________________
TEST FAILURE: 1 error during execution; 0 tests Failed,0 passed (221ms)
✖ custom_test
An error occurred while retrieving a new session: "unkNown error: cannot find Chrome binary"
Error: An error occurred while retrieving a new session: "unkNown error: cannot find Chrome binary"
at endReadableNT (_stream_readable.js:1187:12)
at processticksAndRejections (internal/process/task_queues.js:84:21)
Error: An error occurred while retrieving a new session: "unkNown error: cannot find Chrome binary"
at endReadableNT (_stream_readable.js:1187:12)
at processticksAndRejections (internal/process/task_queues.js:84:21)
SKIPPED:
- NW test on Google
npm ERR! code ELIFECYCLE
npm ERR! errno 5
npm ERR! exactestate@1.0.0 nightwatch: `nightwatch "custom_test.js"`
npm ERR! Exit status 5
npm ERR!
npm ERR! Failed at the exactestate@1.0.0 nightwatch script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2021-02-09T17_02_25_145Z-debug.log
使用 NPX 更新
Starting: nightwatch custom_test.js
internal/modules/cjs/loader.js:983
throw err;
^
Error: Cannot find module '/opt/project/nightwatch'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:980:15)
at Function.Module._load (internal/modules/cjs/loader.js:862:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47 {
code: 'MODULE_NOT_FOUND',requireStack: []
}
更新 3:整个 PACKAGE.JSON
{
"name": "exactestate","version": "1.0.0","description": "Modern,Secure,and Customizable Real Estate Management Software in the Cloud.","scripts": {
"build": "webpack --mode=production","build:dev": "webpack --mode=development","serve": "webpack-dev-server","test": "jest --colors --coverage --verbose ","nightwatch": "./node_modules/.bin/nightwatch"
},"browserslist": [
"last 2 version","> 1%"
],"dependencies": {
"@fortawesome/fontawesome-pro": "^5.13.0","axios": "^0.21.1","bootstrap": "^4.4.1","chart.js": "^2.9.4","chartjs-plugin-datalabels": "^0.7.0","cleave": "^1.0.0-alpha.10","crypto-js": "^4.0.0","datatables.net": "^1.10.20","gsap": "^3.2.6","intro.js": "^2.9.3","jquery": "^3.5.0","jspdf": "^1.5.3","jspdf-autotable": "^3.4.3","lodash": "^4.17.15","lodash-webpack-plugin": "^0.11.6","mobile-device-detect": "^0.3.3","moment": "^2.24.0","mousetrap": "^1.6.5","node": "^12.16.2","popper.js": "^1.16.1","qs": "^6.9.3","secure-web-storage": "^1.0.2","tiptap": "^1.27.0","tiptap-extensions": "^1.29.0","uglifyjs-webpack-plugin": "^2.2.0","v-mask": "^2.1.0","validator": "^12.2.0","vue": "^2.6.11","vue-async-computed": "^3.8.2","vue-chartjs": "^3.5.1","vue-inputmask": "^0.2.1","vue2-perfect-scrollbar": "^1.5.0","vuedraggable": "^2.23.2","ws": "^7.3.0"
},"devDependencies": {
"@babel/cli": "^7.8.4","@babel/core": "^7.11.6","@babel/plugin-transform-runtime": "^7.11.5","@babel/preset-env": "^7.11.5","@tweenjs/tween.js": "^18.5.0","@vue/test-utils": "^1.1.0","autoprefixer": "^9.7.6","babel-core": "^7.0.0-bridge.0","babel-jest": "^26.5.2","babel-loader": "^8.1.0","babel-preset-es2015": "^6.24.1","chai": "^4.3.0","chromedriver": "^88.0.0","clean-webpack-plugin": "^3.0.0","co": "^4.6.0","co-mocha": "^1.2.2","compression-webpack-plugin": "^3.1.0","copy-webpack-plugin": "^5.1.1","core-js": "^3.6.5","css-loader": "^3.5.2","electron": "^11.2.3","fibers": "^4.0.2","file-loader": "^4.3.0","geckodriver": "^1.22.1","glob": "^7.1.6","gzip-loader": "0.0.1","imagemin-mozjpeg": "^8.0.0","imagemin-webpack-plugin": "^2.4.2","jest": "^26.5.3","mini-css-extract-plugin": "^0.8.2","mocha": "^8.2.1","mocha-generators": "^2.0.0","nightmare": "^3.0.2","nightwatch": "^1.5.1","node-sass": "^4.14.1","postcss-import": "^12.0.1","postcss-loader": "^3.0.0","regenerator-runtime": "^0.13.5","sass": "^1.26.3","sass-loader": "^8.0.2","speed-measure-webpack-plugin": "^1.3.3","style-loader": "^1.1.4","url-loader": "^2.2.0","vue-jest": "^3.0.7","vue-loader": "^15.9.1","vue-style-loader": "^4.1.2","vue-template-compiler": "^2.6.11","webpack": "^4.42.1","webpack-bundle-analyzer": "^3.7.0","webpack-bundle-tracker": "^0.4.3","webpack-cli": "^3.3.11","webpack-dev-server": "^3.11.0"
},"repository": {
"type": "git","url": "git+https://github.com/ViaTechSystems/ExactEstate.git"
},"bugs": {
"url": "https://github.com/ViaTechSystems/ExactEstate/issues"
},"homepage": "https://github.com/ViaTechSystems/ExactEstate#readme"
}
解决方法
在您的根目录中创建一个文件 nightwatch.conf.js 并复制以下内容 -
module.exports = {
// An array of folders (excluding subfolders) where your tests are located;
// if this is not specified,the test source must be passed as the second argument to the test runner.
src_folders: [],webdriver: {
start_process: true,port: 4444,server_path: require('chromedriver').path,cli_args: []
},test_settings: {
default: {
desiredCapabilities: {
browserName: "chrome",chromeOptions: {
args: ['disable-gpu','no-sandbox','headless']
}
}
}
}
}