waitForElementVisible 相反。在執行任何其他指令或斷言之前,等待指定的時間(毫秒,預設為 5000 毫秒)讓元素在頁面中變為不可見(即隱藏但存在)。
如果在指定的時間內元素無法隱藏,則測試將失敗。

您可以通過在 nightwatch.json 或外部全域檔案中定義 waitForConditionPollInterval 屬性(以毫秒為單位)來變更輪詢間隔。
同樣,可以將預設逾時指定為全域 waitForConditionTimeout 屬性(以毫秒為單位)。

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

用法

                    .waitForElementNotVisible([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.waitForElementNotVisible('#dialog');

    // specify the locate strategy (css selector/xpath) as the first argument
    browser.waitForElementNotVisible('css selector', '#dialog');

    // with explicit timeout (in milliseconds)
    browser.waitForElementNotVisible('#dialog', 1000);

    // continue if failed
    browser.waitForElementNotVisible('#dialog', 1000, false);

    // with callback
    browser.waitForElementNotVisible('#dialog', 1000, function() {
      // do something while we're here
    });

    // with custom output message - the locate strategy is required
    browser.waitForElementNotVisible('css selector', '#dialog', 'The dialog container is not visible.');

    // with custom Spanish message
    browser.waitForElementNotVisible('#dialog', 1000, 'elemento %s no era visible en %d ms');

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

  'demo Test with selector objects': function(browser) {
     browser.waitForElementNotVisible({
       selector: '#dialog',
       timeout: 1000
     });

     browser.waitForElementNotVisible({
       selector: '#dialog',
       locateStrategy: 'css selector'
     }, 'Custom output message');

     browser.waitForElementNotVisible({
       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.waitForElementNotVisible('@mainDialog', function(result) {
       console.log(result);
     });
  }
}

參數

名稱 類型 描述
using
可選
字串

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

selector 字串 | 物件

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

time=waitForConditionTimeout
可選
數字

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

poll=waitForConditionPollInterval
可選
數字

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

abortOnFailure=abortOnAssertionFailure
可選
布林值

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

callback
可選
函式

命令完成時要呼叫的可選回呼函式。

message
可選
字串

要在輸出中顯示的可選訊息;訊息支援兩個佔位符:%s 代表目前選擇器,%d 代表時間(例如,元素 %s 在 %d 毫秒內不在頁面上)。

另請參閱