プログラマティックAPI

通常のライブラリパッケージとして含まれている場合、Nightwatchはプログラムによって使用できるAPIをエクスポートします。v1.3以降、デフォルトで設定ファイル(nightwatch.conf.js)が作成されますが、設定ファイルの指定は必要ありません。

構文

Nightwatch 2は、Nightwatchを外部から非常に簡単に使用できるようにする、まったく新しいプログラマティックAPIを導入しています。カスタムランナーを作成するか、Jest、Mocha、Avaなどの外部テストランナーを使用することで実現できます。

サードパーティのテストランナーでNightwatchを使用する方法の詳細な例は、近日公開予定です。

Nightwatch.createClient([options])

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

構文

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();