nightwatch系列教程05——Nightwatch配置详解

本章内容翻译自http://nightwatchjs.org/gettingstarted#settings-file

测试运行接收一个配置文件作为参数,默认是当前目录下的 nightwatch.json 文件。如果在同目录下还找了到了 nightwatch.conf.js 配置文件,那么nightwatch也会加载它作为配置。

我们先在项目根目录下新建一个 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.jsonnightwatch.conf.js 两个配置文件,那么它们都会被加载,nightwatch.conf.js 会优先被加载。

例子:

module.exports = (function(settings) {
  settings.test_workers = false;
  return settings;
})(require('./nightwatch.json'));

基础配置

属性名称 类型 默认值 描述
src_folders string或array none 测试文件所在的目录(不包含子目录)
output_folder (可选) string 'tests_output' Junit XML 测试报告文件生成的目录
custom_commands_path (可选) string或array none 自定义command所在的文件目录
custom_assertions_path (可选) string或array none 自定义assertion 所在的文件目录
page_objects_path (可选) string或array none page objects所在的文件目录
globals_path (可选) string none 外部全局变量的位置。
全局变量也可以在test_settings中被定义或者复写
selenium (可选) object Selenium Server 相关配置,下面有详细介绍
test_settings object 包含所有测试相关的选项配置,下面会有详细介绍
live_output (可选) boolean false 并行运行测试的时候,是否缓存输出
disable_colors (可选) boolean false 运行命令行的时候时候的输出是否带颜色
parallel_process_delay (可选) integer 10 并行运行的时候,指定开启子进程的延时时间(单位:毫秒)
test_workers``(可选) boolean或object false 是否并行运行测试文件。
设置为true的话,会多环境并行运行测试文件,
自动计算worker的数量;设置为object的话,
可以指定workers的数量是auto还是一个具体的数字。
例子:"test_workers" : {"enabled" : true, "workers" : "auto"}
test_runner (可选)``(v0.8.0+) string或object "default" 指定你想用的测试运行器,可以设置为 default 或者 mocha
例子:"test_runner" : {"type" : "mocha", "options" : {"ui" : "tdd"}}

Selenium 设置

以下是一些selenium server进程的一些选项。Nightwatch 可以自动的开启和关闭Selenium的进程,这就让你可以专注于测试而不用手动的管理这些,非常方便。

如果你想启用这个功能,只需要将start_server设置成 true 然后在 server_path 中定义 jar 文件的位置即可。

属性名称 类型 默认值 描述
start_process boolean false 是否让nightwatch自动管理selenium进程
start_session boolean true 是否让Nightwatch自动开启selenium会话。
当不需要与Selenium Server交互的时候,比如运行单元/集成测试等,
这个值一般设置成 false
server_path string none selenium jar文件的位置。如果 start_process 设置为 true 的话,
该值必须指定。例如:bin/selenium-server-standalone-2.43.0.jar
log_path string或boolean none selenium的 output.log 日志输出的位置。
默认是当前文件夹。如果想禁用selenium的日志,
将其设置为 false
port integer 4444 Selenium监听的端口
cli_args object none Selenium进程运行时参数。
你可以设置针对不同浏览器设置不同的选项,例如
webdriver.firefox.profile: Selenium将默认为每个会话创建一个新的Firefox配置文件。
如果你希望使用现有的Firefox配置文件,可以在此处指定其名称。
Firefox驱动程序参数的完整列表请参考这里
webdriver.chrome.driver: Nightwatch可以使用谷歌浏览器来运行测试。
要启用它你首先要下载谷歌驱动(chromedriver),然后在这里配置该驱动的位置。
另外,不要忘记在desiredCapabilities 对象中指定'chrome'作为浏览器名称。
更多内容请参考ChromeDriver
webdriver.ie.driver: nightwatch同样也支持IE。
要启用它你得先下载IE驱动,然后在这里配置该驱动的位置。
另外,不要忘记在desiredCapabilities 对象中指定'internet explorer'作为浏览器名称。

Test Settings

以下是将传递给Nightwatch实例的一些设置。你可以在test_settings中定义多个部分(环境),以便可以覆盖每个环境的特定值。


"default" 环境是必须的。其它所有的环境都将从这个"default"环境中继承,并且根据需要来重写配置。

{
  ...
  "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"
      }
    }
  }
}

然后可以将设置组(test group)的键作为--env参数传递给运行器以使用指定的设置,如下所示:

nightwatch --env integration

当你想要为本地和配置了CI(持续集成)的服务器提供不同的设置时,这将非常有用。

launch_url 属性

该属性值在测试中可以通过nightwatch的api来获取。它的值取决于你使用的具体环境。

如果你像上面的例子那样运行测试(用 --env integration), launch_url 会被设置成 http://staging.host,否则它会使用"default"环境中定义的值(如: http://localhost)

module.exports = {
  'Demo test' : function (browser) {
    browser
      .url(browser.launchUrl)
      // ...
      .end();
  }
};

全局变量

Nightwatch提供的一个非常有用的概念是全局变量。最简单的定义方式,是在你的nightwatch.json配置文件中定义一些键值对。和 launch_url一样,你在测试文件中也可以直接使用nightwatch的api来获取这些值。它的值也取决于你的运行环境,你可以根据不同的环境来设置不同的值。

如果仍然将 --env integration 选项传给测试运行器,那么我们的全局对象就长这样:

module.exports = {
  'Demo test' : function (browser) {

    console.log(browser.globals);
    /*
      {
        "myGlobalVar" : "some value",
        "otherGlobal" : "some other value"
      }
    */
  }
};

默认情况下,每个测试套件运行时都会使用全局对象的深拷贝副本。如果你想在整个测试运行期间保持相同的对象,可以将 persist_globals 选项设置为true,如下所述。

test_settings 列表

属性名称 类型 默认值 描述
launch_url string none 可以在以后的测试中被加载的主URL。
如果你在不同的环境中运行测试,每个环境都会有对应的不同的URL,这会很有帮助。
selenium_host string localhost selenium服务器接受连接的主机名/ IP
selenium_port integer 4444 selenium服务器接受连接的端口号
request_timeout_options object 60000/0 在达到超时时间之前,对Selenium服务器的HTTP请求将保持打开的毫秒数。
超时后,请求可以自动重试指定的次数,该次数由retry_attempts属性定义,
例如:"request_timeout_options": {"timeout":15000,"retry_attempts": 5}
silent boolean true 是否显示扩展的selenium的日志
output boolean true 是否完全禁用终端的输出
disable_colors boolean false 终端的输出是否可以带颜色
firefox_profile (过时) string或boolean none 已过时,现请参考上面的 seleniumcli_args 配置
chrome_driver (过时) string none 已过时,现请参考上面的 seleniumcli_args 配置
ie_driver string none 已过时,现请参考上面的 seleniumcli_args 配置
screenshots object none 当命令发生错误Selenium会生成截图。
on_failure 设置为 true,还会为失败或错误测试生成截图。
这些都保存在磁盘上。v0.7.5以后,你可以通过设置on_errorfalse来阻止生成命令错误的截图,
例如:"screenshots" : { "enabled" : true, "on_failure" :true,"on_error" : false,"path" : ""}
username string none 如果selenium服务器需要凭据,则此用户名
将用于计算Authorization的header.它的值可以从环境变量获得,
写成:"username" : "${SAUCE_USERNAME}"
access_key string none 该属性会和 username 一起使用来计算出Authorization的header,
它的值也可以从环境变量获取:"access_key" : "${SAUCE_ACCESS_KEY}"
proxy (v0.8.6) string none 请求到 selenium 服务器的代理,
可以是http, https, socks(v5), socks5, sock4 和 pac,使用的是 node-proxy-agent
例如:http://user:pass@host:port
desiredCapabilities object 开启一个新会话的时候,传递到Selenium WebDriver的一个对象。
你可以指定实例的浏览器名称以及其他功能。例子:"desiredCapabilities" :{"browserName" : "firefox", "acceptSslCerts" : true}
更多能力配置请参考这里
globals object 全局对象
exclude array 要跳过的文件夹或者匹配指定模式的文件(相对于主源文件夹)。
如:"exclude" : ["excluded-folder"]"exclude" : ["test-folder/*-smoke.js"]
filter string 加载测试时使用的文件夹或者指定模式的文件,不匹配该模式的将会被忽略:例如:"filter" : "tests/*-smoke.js"
log_screenshot_data boolean false 在截屏时,是否在(详细)日志中显示Base64图像数据。
use_xpath boolean false 是否使用 xpath 作为默认的元素定位器
cli_args object none Selenium 中的 cli_args一样。你可以基于每个环境覆盖全局cli_args。
end_session_on_fail boolean true 当测试终止时是否自动结束会话, 通常在一个断言失败后
skip_testcases_on_fail boolean true 当一个测试用例(步骤,testcase/step)失败后,是否跳过同文件中剩余的测试用例
output_folder (v0.8.18) `string boolean`
persist_globals (v0.8.18) boolean false 如果你想在testsuite运行之间保留相同的全局对象,或者每个testsuite都有一个(深层)副本,请将此设置为true
compatible_testcase_support (v0.9.0) boolean false 适用于单元测试。设置为true时,允许将测试写入标准Exports接口,该接口可与Mocha框架互换。 不推荐使用先前的单元测试接口支持,这将成为未来版本的默认设置。
detailed_output (v0.9.0) boolean true 默认情况下,在测试运行时会显示详细的断言输出。如果你只想查看显示的测试用例名称和通过/失败状态,请将此项设置为false。这在并行运行测试时特别有用。
原文地址:https://www.cnblogs.com/jerryyj/p/9621541.html