概要

Nightwatch を通常のライブラリパッケージとしてインポートすると、API がエクスポートされるため、Nightwatch をプログラムで使用できます。その場合、個々の設定をインラインで指定できます。したがって、設定ファイルを指定する必要はありません。ただし、バージョン 1.3 以降、プロジェクト用に nightwatch.conf.js がデフォルトで作成されます。

構文

Nightwatch 2 は、まったく新しいプログラム API を導入しており、Nightwatch を外部で使用することが非常に容易になります。カスタムランナーを作成するか、Jest、Mocha、Ava などの外部テストランナーを使用して使用できます。

Nightwatch.createClient([options])

WebDriver セッションの作成に使用できる新しい Nightwatch クライアントを作成します。

構文
custom_test_runner.js
const Nightwatch = require('nightwatch');

const client = Nightwatch.createClient({ headless: true, output: true, silent: true, // set to false to enable verbose logging browserName: 'firefox', // can be either: firefox, chrome, safari, or edge
// set the global timeout to be used with waitFor commands and when retrying assertions/expects timeout: 10000,
// set the current test environment from the nightwatch config env: null,
// any additional capabilities needed desiredCapabilities: {
},
// can define/overwrite test globals here; // when using a third-party test runner only the global hooks onBrowserNavigate/onBrowserQuit are supported globals: {},
// when the test runner used supports running tests in parallel; // set to true if you need the webdriver port to be randomly generated parallel: false,
// All other Nightwatch config settings can be overwritten here, such as: disable_colors: false });

client.updateCapabilities([options])

上記の `createClient()` メソッドを使用して作成された既存の `client` がある場合、これは最初に指定された機能を更新するために使用できます。

構文
client.updateCapabilities({
  testCapability: 'one, two, three'
});

client.launchBrowser()

上記の `createClient()` メソッドを使用して作成された既存の `client` がある場合、これは新しいブラウザセッションを作成するために使用できます。

返されるオブジェクトは、Nightwatch のブラウザ API オブジェクトになります。

構文

const browser = await client.launchBrowser();