概觀

Nightwatch 提供 測試環境 來維護大型專案的組態設定的更好結構。

您可以在 test_settings 字典下定義多個區段 (環境),以便您可以針對每個環境覆寫特定的值。

需要「預設」環境。所有其他環境都繼承自預設環境,並且可以根據需要覆寫設定。

環境會繼承所有基本設定以及「預設」環境下定義的所有設定。

範例

在以下範例中,有兩個環境

  • 預設 (始終需要)
  • 整合
nightwatch.json
{
  "src_folders": ["./tests"],
  
"test_settings" : { "default" : { "launch_url" : "https://127.0.0.1", "globals" : { "myGlobalVar" : "some value", "otherGlobal" : "some other value" } }, "integration" : { "launch_url" : "http://staging.host", "globals" : { "myGlobalVar" : "other value" } } } }

用法

測試環境可以透過其在命令列測試執行器中的索引鍵作為 --env 引數來參考

nightwatch --env integration

如果您需要針對本機和持續整合伺服器使用不同的設定,這會很有用。

內建環境

由 Nightwatch 自動產生的組態檔 (nightwatch.conf.js) 包含多個預先定義的測試環境,用於針對多個瀏覽器 (Firefox、Chrome、Edge、Safari) 執行測試,以及使用 Selenium Server 或熱門雲端測試提供者 Browserstack 和 Saucelabs 執行測試。

以下是 nightwatch.conf.js 組態檔的摘錄,該檔案可在 Github 上的 nightwatch-examples 專案中找到

github.com/nightwatchjs/nightwatch-examples/.../nightwatch.conf.js
module.exports = {
  src_folders: ['tests'],

  test_settings: {
    default: {
      webdriver: {
        start_process: true,
        server_path: ''
      }
    },

    safari: {
      desiredCapabilities : {
        browserName : 'safari',
        alwaysMatch: {
          acceptInsecureCerts: false
        }
      },
      webdriver: {
        port: 4445
      }
    },
    
    firefox: {
      desiredCapabilities : {
        browserName : 'firefox'
      },
      
      webdriver: {
        start_process: true,
        port: 4444
      }
    }
  }
}