テスト環境の定義と使用
概要
環境は、設定ファイルの"test_settings"
辞書にあります。 他の環境が設定を継承するdefault
環境は、常に必要です。 必要に応じて、各環境のテスト設定を上書きできます。
このガイドでは、Google Chromeブラウザでテストを実行するために使用する、"chrome-local"
という新しいテスト環境を作成します。
新しいテストプロジェクトを作成する
まず、新しい空のプロジェクトを作成し、その中にNightwatchをインストールしましょう。
mkdir ./test-project && cd ./test-project
NPMからnightwatch
とchromedriver
をインストールします (chromedriver
は、Google Chromeブラウザでテストを実行するためのW3C WebDriver実装です)。
npm i nightwatch chromedriver
nightwatch.conf.js
という名前の空のファイルを作成します
nano nightwatch.conf.js
そして、以下を貼り付けます
nightwatch.conf.js
module.exports = {
src_folders: ['tests'],
test_settings: {
default: {
launch_url: 'https://home.cern',
webdriver: {
start_process: true,
server_path: ''
}
}
}
}
テスト環境は、--env
CLI引数を使用して参照されます。 default
環境しか定義されていないため、chrome-local
環境を参照しようとするとエラーが発生します。
npx nightwatch --env chrome-local
~/workspace/test-project % npx nightwatch --env chrome-local
┌──────────────────────────────────────────────────────────────────┐
│ │
│ Error: Invalid testing environment specified: chrome-local. │
│ │
│ Available environments are: │
│ [ 'default' ] │
│ │
│ │
└──────────────────────────────────────────────────────────────────┘
新しい "chrome-local" 環境を定義する
次に、nightwatch.conf.js
ファイルを再び開き、default
の下にchrome-local
オブジェクトを追加します。
nightwatch.conf.js
module.exports = {
src_folders: ['tests'],
test_settings: {
default: {
launch_url: 'https://home.cern',
webdriver: {
start_process: true,
server_path: ''
}
},
'chrome-local': {
desiredCapabilities: {
browserName: 'chrome'
}
}
}
}
"chrome-local" 環境に対してサンプルテストを実行する
次に、`tests`フォルダ内にサンプルテストを追加します
mkdir tests && nano ./tests/sample-nightwatch-test.js
tests/sample-nightwatch-test.js
describe('sample nightwatch test', function() {
it('opens the browser and checks for input', function(browser) {
browser
.init()
.assert.titleEquals('Home | CERN')
.end();
});
})
サンプルを実行し、`--env chrome-local`引数を渡します
npx nightwatch --env chrome-local
出力は次のようになります
[sample nightwatch test] Test Suite
──────────────────────────────────────────────────────────────────────
ℹ Connected to ChromeDriver on port 9515 (844ms).
Using: chrome (101.0.4951.64) on MAC OS X.
Running opens the browser and checks for input:
────────────────────────────────────────────────────────────────────────────────────────────────────────────────
ℹ Loaded url https://home.cern in 5531ms
✔ Testing if the page title equals 'Home | CERN' (6ms)
OK. 1 assertions passed. (5.604s)