Nightwatch API 建議編輯

Nightwatch 的運作方式是在單一 API 物件上載入所有必要的指令、設定和其他屬性,該物件會在執行階段以單一引數的形式提供給所有測試腳本。這樣做的目的是讓所有內容都準備好開始編寫實際的測試腳本,而無需實例化其他物件。

「browser」物件

我們將主要 API 物件稱為 browser – 為了與其他 Selenium 相關的 JS 測試框架保持一致性,並且因為自 v2 起,它也作為 global 提供

module.exports = {
  demoTest: function (browser) {
    browser.init();
  }
};

您可能還會看到來自先前 Nightwatch 版本的範例,使用 client。當然,這樣做完全沒問題,但對於剛接觸 JavaScript 語法的用戶來說,可能會造成混淆。

module.exports = {
  demoTest: function (client) {
    client.init();
  }
};

作為全域使用

從 Nightwatch 2 開始,browser 作為全域變數提供,因此以下也是有效的

module.exports = {
  demoTest: function () {
    browser.init();
  }
};

以及這個

describe('Nightwatch APIs', function() {
  it('demoTest', function () {
    browser.init();
  })
};

API 內容

以下是在 browser 物件上提供的所有公共屬性和方法的列表。

  • WEBDRIVER_ELEMENT_ID

    類型:string

    W3C 網頁元素識別碼,等於字串常數:"element-6066-11e4-a52e-4f735466cecf"

  • browserName

    類型:string

    在 capabilities 物件中指定的 browserName。

  • isChrome

    類型:function isChrome(): boolean

    返回 true|false,表示使用的瀏覽器是否為 Google Chrome。

  • isFirefox

    類型:function isFirefox(): boolean

    返回 true|false,表示使用的瀏覽器是否為 Mozilla Firefox。

  • isSafari

    類型:function isSafari(): boolean

    返回 true|false,表示使用的瀏覽器是否為 Apple Safari。

  • isEdge

    類型:function isEdge(): boolean

    返回 true|false,表示使用的瀏覽器是否為 Microsoft Edge。

  • isInternetExplorer

    類型:function isInternetExplorer(): boolean

    返回 true|false,表示使用的瀏覽器是否為 Microsoft InternetExplorer。

  • isOpera

    類型:function isOpera(): boolean

    返回 true|false,表示使用的瀏覽器是否為 Opera。

  • baseUrl

    類型:string

    返回 true|false,表示使用的瀏覽器是否為 Opera。

    在 Nightwatch 配置中定義的 baseUrl 值,將用作 .init() 指令的預設 URL。其他別名為:base_urllaunch_urllaunchUrl

  • actions

    類型:function actions(options?: { async?: boolean; bridge?: boolean }): Actions

    返回 Selenium 的 Actions 類別的新實例。詳情請參閱使用者動作 API 章節。

  • capabilities

    類型:object

    WebDriver 會話功能,由瀏覽器驅動程式用於傳達支援的功能。

    WebDriver 功能用於傳達會話支援的功能。用戶端也可以使用功能來定義在建立新會話時,驅動程式需要滿足哪些功能。

    當建立 WebDriver 會話時,它會傳回一組功能,描述會話的協商的有效功能。此集合中包含的一些功能是標準的,並且在所有瀏覽器之間共享,但該集合也可能包含瀏覽器特定的功能,這些功能總是帶有前綴。更多關於 WebDriver 功能

    範例

  {
    acceptInsecureCerts: false,
    browserName: 'chrome',
    browserVersion: '96.0.4664.55',
    'goog:chromeOptions': { debuggerAddress: 'localhost:50427' },
    // ... continued
  }
  • currentTest

    類型:NightwatchTestSuite

    一個包含目前正在執行的測試案例相關資訊的物件。

    可用的屬性

  {
    // name of the current running testcase
    name: ' ... ',
    // name of the current running testsuite, i.e. the test file
    module: ' ... ',
    // name of the current running test group, if any
    group: '', 
    // the results object is shared among all testcases in the current testsuite
    results: {
      time: 0,
      assertions: [Array],
      passed: 0,
      errors: 0,
      failed: 0,
      retries: [Number],
      skipped: 0,
      tests: 0,
      steps: [],
      stackTrace: '',
      // an object accumulating the results of each testcase
      testcases: [Object]
    },
    // the current timestamp, in the format: Wed, 01 Dec 2021 08:34:00 GMT
    timestamp: ''
  }
  • desiredCapabilities

    類型:NightwatchDesiredCapabilities

    一個包含 Nightwatch 發送到 WebDriver 的功能的物件,如 Nightwatch 設定檔中所定義(詳情請參閱設定章節)。

    範例

  {
    browserName: 'chrome',
    'goog:chromeOptions': {},
    name: 'Example Test'
  }
  • driver

    類型:WebDriver

    Nightwatch 在底層使用的 Selenium WebDriver 實例。當需要擴展 Nightwatch 提供的主要功能時,在自訂指令中可能很有用。如果在常規測試案例腳本中使用,則需要將其包裝在 .perform() 指令中。

    範例

    在下面的範例中,我們將檢索 WebDriver 會話 實例。


  describe('Nightwatch APIs', function() {
    it('driver demoTest', async function () {
      const session = await browser
        .init()
        .perform(function() {
          return this.driver.getSession();
        });
    })
  };
  • sessionId

    類型:string

    WebDriver 會話 ID。WebDriver 為每個會話提供一個唯一的會話 ID,可用於區分不同的會話。有關 WebDriver 會話的更多資訊,請參閱 W3C WebDriver 頁面Selenium WebDriver 文件

    範例

  console.log(browser.sessionId); // e0b40362dcec8ec501ac2b42b62bdce2
  • globals

    類型:NightwatchGlobals

    處理過的 Nightwatch 全域物件,包含所有目前的全域屬性和方法。詳情請參閱測試全域章節

  • options

    類型:NightwatchOptions

    處理過的 Nightwatch 配置物件,包含所有目前使用的屬性和設定。詳情請參閱設定章節

  • Keys

    類型:NightwatchKeys

    Selenium 中 Key 列舉的連結,其中包含所有非文字的可按壓按鍵。這通常在使用向頁面發送文字的指令(如 .sendKeys())時需要。

  • page

    類型:NightwatchPage & NightwatchCustomPageObjects

    包含目前測試案例建立的目前頁面物件的字典。詳情請參閱 使用頁面物件 章節。

  • assert

    類型:Assert

    詳情請參閱 Assert API 章節。

  • verify

    類型:Assert

    詳情請參閱 Assert API 章節。

  • ensure

    類型:Ensure

    詳情請參閱 Ensure API 章節。

  • expect

    類型:Expect

    詳情請參閱 Expect API 章節。

  • chrome

    類型:object

    Chromium 特定指令。詳情請參閱 API 指令 章節。

  • firefox

    類型:object

    Firefox 特定指令。詳情請參閱 API 指令 章節。