距離上一隨筆,已經有一個月沒有沒寫。到今天,剛剛好好,是學習e2e測試的一個月。今天有點時間可以總結一下這個月來的收獲。
1、搭建e2e的測試環境
我是使用 Vue 構建項目,所以我也是通過Vue-cli生成已經包含 Selenium 與 Nightwatch 的 Vue 種子項目。
setup e2e tests with Nightwatch?Yes(單元測試需要)
2、e2e的測試目錄

--e2e
--custom-assertions(自定義斷言)
--reports(生成的測試報告存放位置)
--specs(測試代碼)
--nightwatch.conf.js(配置文件)
3、nightwatch.conf.js(配置文件)
require('babel-register')
var config = require('../../config')
// http://nightwatchjs.org/gettingstarted#settings-file
module.exports = {
src_folders: ['test/e2e/specs'],//測試用例源文件路徑
output_folder: 'test/e2e/reports',//測試報告地址
custom_assertions_path: ['test/e2e/custom-assertions'],// 自定義命令,這里用來更新測試信息到custom-assertions
custom_commands_path : "",
page_objects_path : ['test/e2e/page-objects'],
globals_path : "",
//selenium配置
selenium: {
start_process: true,//是否開啟
server_path: require('selenium-server').path,//路徑
host: '127.0.0.1',
port: 4444, //端口
cli_args: { //cli_args指定將要運行的webdriver
'webdriver.chrome.driver': require('chromedriver').path
}
},
// 測試配置
test_settings: {
default: {
selenium_port: 4444,//指定 selenium server 接受的端口。。
selenium_host: 'localhost',//指定 selenium server 接受的 hostname/IP。
silent: true,//是否顯示 selenium 命令日志。
use_xpath:true,//是否用 xpath 做為默認的元素選擇策略。
globals: {//在測試代碼中可以訪問的全局變量,並且每次切換測試環境時可以重寫該值。例如: *”globals” :{“myGlobal” : “some_global”}
devServerURL: 'http://localhost:' + (process.env.PORT || config.dev.port)
},
screenshots : {
"enabled" : false,
"path" : ""
},
},
chrome: {
desiredCapabilities: {
browserName: 'chrome',
javascriptEnabled: true,
acceptSslCerts: true,
"chromeOptions": {
"args" : ["--no-sandbox"]
}
}
},
firefox: {
desiredCapabilities: {
browserName: 'firefox',
javascriptEnabled: true,
acceptSslCerts: true
}
}
}
}
從上面代碼可以看到有三種配置,分別是:Basic settings,Selenium settings和Test settings。我們下面詳細看看這三種配置主要有哪些配置信息。
(1)Basic settings
| Name | type |
default | description |
|---|---|---|---|
| src_folders | string|array | none | 測試代碼目錄(不包含子目錄) |
| output_folder (optional) | string | tests_output | 生成的測試報告存放目錄 |
| custom_commands_path (optional) | string|array | none | Location(s) where custom commands will be loaded from. |
| custom_assertions_path (optional) | string|array | none | 自定義斷言路徑 |
| page_objects_path (Optional since v6.0.1) | string|array | none | Location(s) where page object files will be loaded from. |
| globals_path (optional) | string | none | 外部模塊路徑,為測試一共全局變量. 也可在test_settings中重寫 |
| selenium (optional) | object | Selenium Server相關的設置 | |
| test_settings (optional) | object | 與測試相關的測試,下面有詳細描述 | |
| live_output (optional) | boolean | false | 是否緩存並持續輸出結果 |
| disable_colors (optional) | boolean | false | 控制命令行輸出是否帶有高亮顏色 |
| parallel_process_delay (optional) | integer | 10 | 在並行模式下啟動子進程的時間,單位毫秒 |
| test_workers | boolean|object | false | 是否為運行單個文件測試啟動並行模式,如果設置為true,會進入並行模式,並自動設置線程數。如果設置為對象,要指定enable和workers參數,workers接受數值和’auto’。 例如:”test_workers” : {“enabled” : true, “workers” : “auto”} |
| test_runner (optional) | string|object | “default” | 用什么工具運行測試。值可以是”default”或”mocha”. 例如:”test_runner” : {“type” : “mocha”, “options” : {“ui” : “tdd”}}
|
(2)Selenium settings
如下是 selenium 的配置選項。Nightwatch可以自動管理 selenium 服務進程,以便你專注於測試工作。 如果要啟用 selenium自啟動,設置 start_process 為true 並設置 server_path 值為 selenium jar包路徑。
| Name | type | default | description |
|---|---|---|---|
| start_process | boolean | false | 是否啟用 selenium自啟動 |
| start_session | boolean | true | 是否自動啟動 Selenium session. |
| server_path | string | none | selenium jar 包路徑。如果設置了 start_process 字段,必須設置此字段。例如: node_modules/selenium-server/lib/runner/selenium-server-standalone-2.53.0.jar |
| log_path | string|boolean | none | selenium 生成的 output.log 文件存放位置,默認為當前目錄下。設置false可以禁用 Selenium 日志。 |
| host | string | 127.0.0.1 | 設置 selenium 監聽地址。不常用到,除非指定了 start_process 為true。 |
| port | integer | 4444 | 設置 selenium 監聽端口 |
| cli_args | object | none | 一系列傳遞給 selenium 的命令行參數。 有許多選項可以設置, 例如: |
| webdriver.firefox.profile: 默認情況下會為每個會話創建一個firefox配置文件,如果想要使用現有配置文件,在這里指定文件名。firefox驅動參數的完整列表見這里 。 | |||
| webdriver.chrome.driver: Nightwatch 也可以在 Chrome 上運行測試,如果要在 chrome 上運行測試需要下載 ChromeDriver binary 並為此字段設置 ChromeDriver 路徑。同時要在 desiredCapabilities 對象配置中指定 browserName值為 “chrome”。 更多信息參考ChromeDriver website。 | |||
| webdriver.ie.driver: Nightwatch同時也支持Internet Explorer,要在IE中運行測試,需要下載 IE Driver binary 並為此字段設置 IE Driver 路徑。同時要在 desiredCapabilities 對象配置中指定 browserName值為 “internet explorer”。 |
(3)Test settings
只有”default”配置是必須的,其他配置可以按需要覆蓋default中的配置信息。
測試環境可以通過 -env 參數傳遞給 nightwatch:
nightwatch --env integration
| Name | type | default | description |
|---|---|---|---|
| launch_url | string | none | 測試時要加載的首頁url, 如果有多個測試環境,可以分別指定url。 |
| selenium_host | string | localhost | 指定 selenium server 接受的 hostname/IP。 |
| selenium_port | string | 4444 | 指定 selenium server 接受的端口。 |
| silent | boolean | true | 是否顯示 selenium 命令日志。 |
| output | boolean | rue | 是否在命令行顯示完整輸出。 |
| disable_colors | boolean | false | 命令行輸出是否高亮。 |
| string|boolean | none | 已經棄用 | |
| string | none | 已經棄用 | |
| string | none | 已經棄用 | |
| screenshots | object | none | 當發生錯誤時 Selenium 會生成屏幕截圖。如果 on_failure 設置為 true, 發生錯誤或沒有通過測試時也生成屏幕截圖。 |
| 從 v0.7.5 版本開始,可以為”on_error”字段設置false來禁止錯誤時生成截圖。 | |||
| 例如:“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。像 username 一樣,值也可以是系統變量。 |
| proxy | string | none | 使用代理訪問 selenium server。支持 http, https, socks(v5), socks5, sock4, 和 pac。使用node-proxy-agent。Example: http://user:pass@host:port |
| desiredCapabilities | object | 在新建 session 之前傳遞給 Selenium WebDriver,可以用來指定瀏覽器名稱和其他功能。例如:“desiredCapabilities” : {“browserName” : “firefox”, “acceptSslCerts” :true}。 完整的功能列表在這里。 | |
| globals | object | 在測試代碼中可以訪問的全局變量,並且每次切換測試環境時可以重寫該值。例如: *”globals” :{“myGlobal” : “some_global”} | |
| exclude | array | 不包含的文件夾,接受字符串或模式字符串(relative to the main source folder)。例如:”exclude” : [“excluded-folder”] 或 :”exclude” : [“test-folder/*-smoke.js”] | |
| filter | string | 接受字符串或模式字符串,與之不匹配的文件會被忽略。 | |
| log_screenshot_data | boolean | false | 是否在日志(verbose模式)中記錄屏幕截圖的base64編碼信息。 |
| use_xpath | boolean | false | 是否用 xpath 做為默認的元素選擇策略。 |
| cli_args | object | none | 作用與 selenium 配置中的 cli_args 相同。 你可以在不同的測試環境配置中覆蓋 selenium 中的配置。 |
| end_session_on_fail | boolean | true | 在測試終止的時候,自動關閉會話,通常會在斷言失敗后觸發。 |
| skip_testcases_on_fail | boolean | true | 是否在任意測試用例測試失敗后,跳過剩余的測試用例。 |
| output_folder | string|boolean | 生成的測試報告存放目錄, 該值覆蓋 Basic settings 中的配置, 也可以設置為 false 不生成報告。 | |
| persist_globals | boolean | false | Weather or not to persist use the same object instance for holding globals between testsuite runs or a (deep) copy of it. |
