配置
Nightwatch.js默认配置文件是nightwatch.json,如下:
{
"src_folders" : ["tests"],
"output_folder" : "reports",
"custom_commands_path" : "",
"custom_assertions_path" : "",
"page_objects_path" : "",
"globals_path" : "",
"selenium" : {
"start_process" : false,
"server_path" : "",
"log_path" : "",
"port" : 4444,
"cli_args" : {
"webdriver.chrome.driver" : "",
"webdriver.gecko.driver" : "",
"webdriver.edge.driver" : ""
}
},
"test_settings" : {
"default" : {
"launch_url" : "http://localhost",
"selenium_port" : 4444,
"selenium_host" : "localhost",
"silent": true,
"screenshots" : {
"enabled" : false,
"path" : ""
},
"desiredCapabilities": {
"browserName": "firefox",
"marionette": true
}
},
"chrome" : {
"desiredCapabilities": {
"browserName": "chrome"
}
},
"edge" : {
"desiredCapabilities": {
"browserName": "MicrosoftEdge"
}
}
}
}
使用两个配置文件也是有可能的,如果存在nightwatch.conf.js,优先使用这个。
举例
module.exports = (function(settings) {
settings.test_workers = false;
return settings;
})(require('./nightwatch.json'));
基本设置(☐ 表示可选)
name | type | default | description |
---|---|---|---|
src_folders | string&array | none | 测试文件所在的一组文件夹(不包括子文件夹) |
output_folder ☐ | string | tests_output | 保存JUnit XML报告文件位置 |
custom_commands_path ☐ | string&array | none | 加载自定义命令的地方 |
page_objects_path ☐ | string&array | none | 加载页面对象的地方 |
globals_path ☐ | string | none | 加载外部全局模块的位置,并在客户端实例上 |
selenium ☐ | object | 包含Selenium服务器相关配置选项的对象。详见下文。 | |
test_settings | object | 该对象包含所有与测试相关的选项。详见下文。 | |
live_output ☐ | boolean | false | 是否在并行运行的情况下缓冲输出。详见下文。 |
disable_colors ☐ | boolean | false | 控制是否在全局上禁用cli输出的颜色。 |
parallel_process_delay ☐ | integer | 10 | 指定在并行模式运行时启动子进程的延迟(以毫秒为单位)。 |
test_workers ☐ | boolean&object | false | 是否要并行运行单独的测试文件。如果设置为true,则并行运行测试,并自动确定工人的数量。如果设置为一个对象,可以指定指定工人的数量为auto或一个number。例如"test_workers" : {"enabled" : true, "workers" : "auto"} |
test_runner ☐ | string&object | "default" | 指定在运行测试时使用哪个测试运行程序。值可以是default 或 mocha。 |
Selenium设置
下面是selenium服务器进程的一些选项。Nightwatch可以启动和停止Selenium进程,这是非常方便的,因为你不必自己管理它,只关注测试。
如果你希望启用此功能,设置start_process设置为true,并指定server_path中的jar文件的位置。
Name
Name | type | default | description |
---|---|---|---|
start_process | boolean | false | 是否自动管理selenium进程。 |
start_session | boolean | true | 是否自动启动Selenium会话。当运行与Selenium服务器不交互的单元/集成测试时,通常会将其设置为false。 |
server_path | string | none | selenium jar文件的位置。如果启用了start_process,就需要指定这一点。 E.g.:bin/selenium-server-standalone-2.43.0.jar |
log_path | string&boolean | none | selenium输出日志位置,默认为当前目录。 如果禁用selenium日志输入,设置output.log为false |
port | integer | 4444 | Selenium监听端口号 |
cli_args | object | none | 可以指定Selenium运行的浏览器,例如:"webdriver.chrome.driver" : "./bin/chromedriver.exe",还可以指定firefox和IE,相关driver下载地址:Chrome Driver,Firefox Driver,IE Driver。 |
测试设置
下面是一些将被传递到Nightwatch实例的设置。你可以定义多个部分(环境)的测试设置,你可以覆盖每个环境的特定值。
{
...
"test_settings" : {
"default" : {
"launch_url" : "http://localhost",
"globals" : {
"myGlobalVar" : "some value",
"otherGlobal" : "some other value"
}
},
"integration" : {
"launch_url" : "http://staging.host",
"globals" : {
"myGlobalVar" : "other value"
}
}
}
}
设置组的键可以被传递给运行器用 --env 参数使用指定的设置,如下:
$ nightwatch --env integration
如果你需要为本地开发环境和持续集成服务器设置不同的配置,那么这将是非常有用的。
launch_url属性
该属性主要在Nightwatch api被用到,它的值取决于所使用的环境。
如果你运行你的