Nightwatchが提供するもう1つの便利な概念は、テストグローバルです。最も単純な形式では、設定ファイルで定義された名前と値のペアの辞書です。

グローバルは、"globals" プロパティとして定義するか、"globals_path" プロパティとして指定された外部ファイルとして定義できます。

nightwatch.json"globals" プロパティを使用した定義例を次に示します。

nightwatch.conf.js
{
  "src_folders": [],

  "globals": {
    "myGlobalVar" : "some value",
    "otherGlobal" : "some other value"
  },

  "test_settings": {
    "default": {
      "launch_url": "https://nightwatch.dokyumento.jp",
    }
  }
}

launch_url プロパティと同様に、globals オブジェクトは、テストに渡される Nightwatch API で直接利用できます。

tests/sampleTest.js
module.exports = {
  'Demo test' : function (browser) {
    console.log(browser.globals.myGlobalVar); // myGlobalVar == "some value"
  }
};

事前定義されたグローバル

次のグローバルプロパティを使用して、テストランナーの動作を制御できます。これらは、次のデフォルト値で定義されています。

nightwatch/globals.js
module.exports = {
  // this controls whether to abort the test execution when an assertion failed and skip the rest
  // it's being used in waitFor commands and expect assertions
  abortOnAssertionFailure: true,

  // this will overwrite the default polling interval (currently 500ms) for waitFor commands
  // and expect assertions that use retry
  waitForConditionPollInterval: 500,

  // default timeout value in milliseconds for waitFor commands and implicit waitFor value for
  // expect assertions
  waitForConditionTimeout : 5000,

  // since 1.4.0 – this controls whether to abort the test execution when an element cannot be located; an error
  // is logged in all cases, but this also enables skipping the rest of the testcase;
  // it's being used in element commands such as .click() or .getText()
  abortOnElementLocateError: false,
  
  // this will cause waitFor commands on elements to throw an error if multiple
  // elements are found using the given locate strategy and selector
  throwOnMultipleElementsReturned: false,

  // By default a warning is printed if multiple elements are found using the given locate strategy
  // and selector; set this to true to suppress those warnings
  suppressWarningsOnMultipleElementsReturned: false,

  // controls the timeout value for async hooks. Expects the done() callback to be invoked within this time
  // or an error is thrown
  asyncHookTimeout : 10000,

  // controls the timeout value for when running async unit tests. Expects the done() callback to be invoked within this time
  // or an error is thrown
  unitTestsTimeout : 2000,

  // controls the timeout value for when executing the global async reporter. Expects the done() callback to be 
  // invoked within this time or an error is thrown
  customReporterCallbackTimeout: 20000,

  // Automatically retrying failed assertions - You can tell Nightwatch to automatically retry failed assertions 
  // until a given timeout is reached, before the test runner gives up and fails the test.
  retryAssertionTimeout: 5000,

  // Custom reporter
  reporter: function(results, done) {
    // do something with the results
    done(results);
  }
}

環境固有のグローバル

他のテスト設定と同様に、グローバルはテスト環境ごとに上書きできます。この構成を検討してください。

nightwatch.json
{
  "src_folders": [],

  "test_settings": {
    "default": {
      "launch_url": "https://nightwatch.dokyumento.jp",

      "globals": {
        "myGlobalVar" : "some value",
        "otherGlobal" : "some other value"
      }
    },

    "integration": {
      "globals": {
        "myGlobalVar" : "integrated global"
      }
    }
  }
}

ランナーに --env integration オプションを渡した場合、グローバルオブジェクトは次のようになります。

nightwatch --env integration
module.exports = {
  'Demo test' : function (browser) {
    console.log(browser.globals.myGlobalVar); // myGlobalVar == "integrated global"
  }
};