總覽

Nightwatch 透過測試全域變數支援測試套件之間的資料持久性及全域測試鉤子定義。最簡單的形式是,它是在您的組態檔案中定義的名稱-值配對字典。

全域變數可以定義為組態檔案中的 "globals" 屬性,也可以定義為外部檔案,該檔案指定為 "globals_path" 屬性。

範例

以下範例是使用 nightwatch.json 組態檔案中的 globals 屬性進行定義

nightwatch.json
{
  "src_folders": [],

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

  "test_settings": {
    "default": {
      "launch_url": "https://nightwatch.dev.org.tw",
    }
  }
}

globals 物件會直接載入至傳遞給測試的 Nightwatch API 物件,並可透過 browser.globals 存取。

sampleTest.js
describe('test globals example', function() {
  
it('Demo test', function(browser) { console.log(browser.globals.myGlobalVar); // myGlobalVar == "some value" });
})

外部測試全域變數

測試全域變數也可以在外部檔案中定義,該檔案由組態檔案中的 globals_path 設定指定,如下所示

nightwatch.json
{
  "src_folders": [],
  "globals_path": "lib/globals.js",
  
"test_settings": { "default": { "launch_url": "https://nightwatch.dev.org.tw" } }

外部全域變數檔案也可以包含

  • 全域測試鉤子
  • 自訂報表器
  • 測試特定設定

預先定義的全域變數

下列 globals 可用來控制測試執行器的行為,並使用下列預設值定義。

您可以使用兩種方式定義這些變數

  • 在外部全域變數檔案中,由 globals_path 組態屬性指定,例如 lib/globals.js
  • 直接在您的 nightwatch.conf.js 組態檔案中
lib/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,

  // use the same browser session to run the individual  test suites
  reuseBrowserSession: false,

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

  // External before hook is ran at the beginning of the tests run, before creating the Selenium session
  before(done) {
    done();
  },

  // External after hook is ran at the very end of the tests run, after closing the Selenium session
  after(done) {
    done();
  },

  // This will be run before each test suite is started
  beforeEach(browser, done) {
    done();
  },

  // This will be run after each test suite is finished
  afterEach(browser, done) {
    done();
  },

  // Called right after the command .navigateTo() is finished
  async onBrowserNavigate(browser) {
    return Promise.resolve();
  },

  // Called right before the command .quit() is finished
  async onBrowserQuit(browser) {
    return Promise.resolve();
  }
}

環境特定全域變數

與其他測試設定一樣,globals 能夠針對每個測試環境覆寫。

請考慮此組態

nightwatch.json
{
  "src_folders": [],
  
"test_settings": { "default": { "launch_url": "https://nightwatch.dev.org.tw",
"globals": { "myGlobalVar" : "some value", "otherGlobal" : "some other value" } },
"integration": { "globals": { "myGlobalVar" : "integrated global" } } } }

讓我們使用一個非常基本的測試來試試看

sampleTest.js
module.exports = {
  'Demo test' : function (browser) {
    console.log('myGlobalVar is: "', browser.globals.myGlobalVar, '"');
  }
};

--env integration 選項傳遞給執行器

npx nightwatch --env integration

那麼我們的全域變數物件看起來會像這樣

myGlobalVar is: "integrated global"