在執行任何其他命令或斷言之前,等待指定的毫秒數 (預設為 5000 毫秒) 讓元素在頁面中可見。

如果元素在指定的時間內未能出現並可見,測試將被標記為失敗,並且通常不會執行測試案例/區段中的後續步驟/命令。 但是,您可以選擇將 `abortOnFailure` 設定為 `false`,以防止跳過剩餘的步驟/命令。

您可以透過在 `nightwatch.json` 或外部全域檔案中,將 `waitForConditionPollInterval` 屬性 (以毫秒為單位) 定義為全域屬性來變更輪詢間隔。

同樣地,預設逾時時間可以指定為全域 `waitForConditionTimeout` 屬性 (以毫秒為單位)。

如需更多關於在 Nightwatch 中使用 DOM 元素的資訊,請參閱尋找與 DOM 元素互動 指南頁面。

用法

                    .waitForElementVisible([using], selector, [timeout], [pollInterval], [abortOnAssertionFailure], [callback], [message]);
                

範例

module.exports = {
 'demo Test': function(browser) {
    // with default implicit timeout of 5000ms (can be overwritten in settings under 'globals.waitForConditionTimeout')
    browser.waitForElementVisible('#index-container');

    // specify the locate strategy (css selector/xpath) as the first argument
    browser.waitForElementVisible('css selector', '#index-container');

    // with explicit timeout (in milliseconds)
    browser.waitForElementVisible('#index-container', 1000);

    // continue if failed
    browser.waitForElementVisible('#index-container', 1000, false);

    // with callback
    browser.waitForElementVisible('#index-container', 1000, function() {
      // do something while we're here
    });

    // with custom output message - the locate strategy is required
    browser.waitForElementVisible('css selector', '#index-container', 'The index container is found.');

    // with custom Spanish message
    browser.waitForElementVisible('#index-container', 1000, 'elemento %s no era presente en %d ms');

    // many combinations possible - the message is always the last argument
    browser.waitForElementVisible('#index-container', 1000, false, function() {}, 'elemento %s no era visible en %d ms');
  },

  'demo Test with selector objects': function(browser) {
     browser.waitForElementVisible({
       selector: '#index-container',
       timeout: 1000
     });

     browser.waitForElementVisible({
       selector: '#index-container',
       locateStrategy: 'css selector'
     }, 'Custom output message');

     browser.waitForElementVisible({
       selector: '.container',
       index: 2,
       retryInterval: 100,
       abortOnFailure: true
     });
  }

  'page object demo Test': function (browser) {
     var nightwatch = browser.page.nightwatch();
     nightwatch
       .navigate()
       .assert.titleContains('Nightwatch.js');

     nightwatch.waitForElementVisible('@featuresList', function(result) {
       console.log(result);
     });
  }
}

參數

名稱 類型 描述
using
選用
字串

要使用的定位器策略。 請參閱 W3C Webdriver - 定位器策略

selector 字串 | 物件

用於定位元素的選取器 (CSS/Xpath)。 可以是字串或指定元素屬性的物件。

time=waitForConditionTimeout
選用
數字

在失敗之前等待的總毫秒數。

poll=waitForConditionPollInterval
選用
數字

每次檢查之間等待的毫秒數。 只有在您也指定 time 參數時,才能使用此參數。

abortOnFailure=abortOnAssertionFailure
選用
布林值

預設情況下,如果找不到元素,測試將會失敗。 如果您希望即使斷言失敗,測試也能繼續,請將此項設定為 false。 若要全域設定此項,您可以在全域設定中定義 `abortOnAssertionFailure` 屬性。

callback
選用
函式

選用的回呼函式,會在命令完成時呼叫。

message
選用
字串

選用的訊息,會顯示在輸出中;訊息支援兩個預留位置:%s 代表目前的選取器,而 %d 代表時間 (例如,元素 %s 在 %d 毫秒內未出現在頁面中)。