Gri*_*rim 5 selenium node.js selenium-server selenium-webdriver nightwatch.js
我正在使用selenium-webdriver并想尝试使用nightwatch.js以查看它是否更易于使用.我按照这里的说明操作.我决定让Nightwatch自动为我启动selenium服务器,所以我做了我认为基于上面提供的链接的正确配置.我得到一个我无法弄清楚的错误,输出结果如下:
Starting selenium server... started - PID: 1760
[Test] Test Suite
=================
Running: demoTestGoogle
Error retrieving a new session from the selenium server
Error: connect ECONNREFUSED 127.0.0.1:8080
at Object.exports._errnoException (util.js:856:11)
at exports._exceptionWithHostPort (util.js:879:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1053:14)
Connection refused! Is selenium server started?
Process finished with exit code 1
Run Code Online (Sandbox Code Playgroud)
selenium调试日志文件说明了这一点
13:43:03.394 INFO - Launching a standalone Selenium Server
13:43:03.474 INFO - Java: Oracle Corporation 25.73-b02
13:43:03.474 INFO - OS: Windows 7 6.1 amd64
13:43:03.483 INFO - v2.52.0, with Core v2.52.0. Built from revision 4c2593c
13:43:03.530 INFO - Driver class not found: com.opera.core.systems.OperaDriver
13:43:03.530 INFO - Driver provider com.opera.core.systems.OperaDriver is not registered
13:43:03.536 INFO - Driver provider org.openqa.selenium.safari.SafariDriver registration is skipped:
registration capabilities Capabilities [{browserName=safari, version=, platform=MAC}] does not match the current platform VISTA
13:43:03.665 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:4444/wd/hub
13:43:03.665 INFO - Selenium Server is up and running
Run Code Online (Sandbox Code Playgroud)
这是我的nightwatch.json文件
{
"src_folders": [ "tests" ],
"output_folder": "reports",
"custom_commands_path": "",
"custom_assertions_path": "",
"page_objects_path": "",
"globals_path": "",
"selenium": {
"start_process": true,
"server_path": "./bin/selenium-server-standalone-jar/jar/selenium-server-standalone-2.52.0.jar",
"start_session" : true,
"log_path": "",
"host": "",
"port": 4444,
"cli_args": {
"webdriver.chrome.driver": "",
"webdriver.ie.driver": ""
}
},
"test_settings": {
"default": {
"launch_url": "http://localhost",
"selenium_port": 8080,
"selenium_host": "localhost",
"silent": true,
"screenshots": {
"enabled": false,
"path": ""
},
"desiredCapabilities": {
"browserName": "firefox",
"javascriptEnabled": true,
"acceptSslCerts": true
}
},
"chrome": {
"desiredCapabilities": {
"browserName": "chrome",
"javascriptEnabled": true,
"acceptSslCerts": true
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:添加了demoTestGoogle,我有一个nightwatch.js文件,我在其中运行,然后运行demoTestGoogle函数.
运行demoTestGoogle的nightwatch.js
require('nightwatch/bin/runner.js');
Run Code Online (Sandbox Code Playgroud)
demoTestGoogle函数在单独的JS文件中
this.demoTestGoogle = function (browser) {
browser
.url('http://www.google.com')
.waitForElementVisible('body', 1000)
.setValue('input[type=text]', 'nightwatch')
.waitForElementVisible('button[name=btnG]', 1000)
.click('button[name=btnG]')
.pause(1000)
.assert.containsText('#main', 'The Night Watch')
.end();
};
Run Code Online (Sandbox Code Playgroud)
正如 Nightwatch 伟大指南dwyl-learn-nightwatch所建议的,您可以将 nightwatch.json 文件替换为 .js 文件,以添加变量、全局变量甚至 Selenium 中的 require 等功能,以便 Nightwatch 可以看到并运行它。
下面是我从 GitHub 源代码修改而来的一个简单示例,用于启动 selenium 及其测试。确保首先在项目中安装依赖项:
npm install --save-dev nightwatch chromedriver selenium-server
Run Code Online (Sandbox Code Playgroud)
然后将该 JSON 文件替换为 .js 文件(可能名为nightwatch.conf.js),并注意配置文件中 selenium 键下的配置选项:
nightwatch.conf.js
const seleniumServer = require("selenium-server");
const chromedriver = require("chromedriver");
const SCREENSHOT_PATH = "./screenshots/";
module.exports = {
"src_folders": [
"tests/e2e"
],
"output_folder": "./reports",
"selenium": {
"start_process": true, // tells nightwatch to start/stop the selenium process
"server_path": seleniumServer.path,
"host": "127.0.0.1",
"port": 4444, // standard selenium port
"cli_args": {
"webdriver.chrome.driver" : chromedriver.path
}
},
"test_settings": {
"default": {
"screenshots": {
"enabled": true, // if you want to keep screenshots
"path": SCREENSHOT_PATH // save screenshots here
},
"globals": {
"waitForConditionTimeout": 5000 // set a (default) timeout period, maybe 5s
},
"desiredCapabilities": { // use Chrome as the default browser for tests
"browserName": "chrome"
}
},
"chrome": {
"desiredCapabilities": {
"browserName": "chrome",
"javascriptEnabled": true
}
}
}
}
function padLeft (count) { // theregister.co.uk/2016/03/23/npm_left_pad_chaos/
return count < 10 ? '0' + count : count.toString();
}
var FILECOUNT = 0; // "global" screenshot file count
/**
* The default is to save screenshots to the root of your project even though
* there is a screenshots path in the config object above! ... so we need a
* function that returns the correct path for storing our screenshots.
* While we're at it, we are adding some meta-data to the filename, specifically
* the Platform/Browser where the test was run and the test (file) name.
*/
function imgpath (browser) {
var a = browser.options.desiredCapabilities;
var meta = [a.platform];
meta.push(a.browserName ? a.browserName : 'any');
meta.push(a.version ? a.version : 'any');
meta.push(a.name); // this is the test filename so always exists.
var metadata = meta.join('~').toLowerCase().replace(/ /g, '');
return SCREENSHOT_PATH + metadata + '_' + padLeft(FILECOUNT++) + '_';
}
module.exports.imgpath = imgpath;
module.exports.SCREENSHOT_PATH = SCREENSHOT_PATH;
Run Code Online (Sandbox Code Playgroud)
我用来运行它的命令是这样的,使用本地安装的 nightwatch 版本:
nightwatch --config nightwatch.conf.js
Run Code Online (Sandbox Code Playgroud)
希望有帮助!祝你好运,祝你测试你的代码。
| 归档时间: |
|
| 查看次数: |
3346 次 |
| 最近记录: |