一個簡單的 perform 指令,允許在回呼中存取 Nightwatch API。如果您想讀取其他指令設定的變數,這會很有用。

回呼簽章最多可以有兩個參數。

  • 沒有參數:回呼執行,且 perform 在回呼執行結束時立即完成。
  • 一個參數:允許在回呼內進行非同步執行,並將 done 回呼函式作為第一個引數,以便完成。
  • 兩個參數:允許進行非同步執行,並將 Nightwatch api 物件作為第一個引數傳遞,後接 done 回呼。

在非同步執行的情況下,可以透過設定 asyncHookTimeout 全域變數來控制逾時。請參閱使用測試全域變數以取得更多資訊。

用法

範例

describe('perform example', function() {
  var elementValue;

  it('basic perform', function(browser) {
    browser
      .getValue('.some-element', function(result) {
        elementValue = result.value;
      })
      // other stuff going on ...

      // self-completing callback
      .perform(function() {
        console.log('elementValue', elementValue);
        // without any defined parameters, perform
        // completes immediately (synchronously)
      })

      // returning a Promise
      .perform(async function() {
        console.log('elementValue', elementValue);
        // potentially other async stuff going on

        return elementValue;
      })

      // DEPRECATED: asynchronous completion including api (client)
      .perform(function(client, done) {
        console.log('elementValue', elementValue);
        done();
    });
  });

  it('perform with async', function(browser) {
    const result = await browser.perform(async function() {
      return 100;
    });
    console.log('result:', result); // 100
  })
}

參數

名稱 類型 描述
callback function

要作為佇列一部分執行的函式。