Nightwatch 設定檔
概觀
Nightwatch 測試執行器二進制檔會預期在目前的工作目錄中找到設定檔。以下是預設載入的可能檔名清單 (如果找到):
nightwatch.conf.js
nightwatch.conf.cjs
(使用 ES 模組時)nightwatch.conf.ts
(使用 TypeScript 時)nightwatch.json
您隨時可以透過 --config
CLI 引數指定設定檔位置。請參閱「參考 > Nightwatch CLI」頁面,深入瞭解 CLI 選項。
從 Nightwatch 2 開始,您也可以指定設定檔傳回 Promise,而且將會
await
其結果。範例
以下是使用 Firefox 作為目標瀏覽器,並假設已安裝 geckodriver
NPM 套件的設定檔範例。
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('geckodriver').path,
cli_args: [
// very verbose geckodriver logs
// '-vv'
]
},
test_settings: {
default: {
launch_url: 'https://nightwatch.dev.org.tw',
desiredCapabilities : {
browserName : 'firefox'
}
}
}
};
自動產生的設定
自版本 1.3 開始,Nightwatch 會在第一次執行時,或如果未找到其他設定檔,根據作業系統和現有的驅動程式套件,產生 nightwatch.conf.js
設定檔。如果在目前資料夾中找到 nightwatch.json
或 nightwatch.conf.js
檔案,則不會有任何動作,且不會產生該檔案。
您可以隨意編輯此檔案,並根據您的需求調整設定。到目前為止,我們已包含對 Chrome、Firefox、Safari 和 Internet Explorer 的支援。
下列套件可以從 NPM 使用,如果已安裝,Nightwatch 會自動偵測並設定測試執行器
- geckodriver – 用於在 Firefox 中執行測試
- chromedriver – 用於在 Chrome 中執行測試
- @nightwatch/selenium-server – 用於使用 Selenium 伺服器