.waitForElementNotPresent() 建議編輯
與 waitForElementPresent
相反。等待指定的毫秒時間(預設為 5000 毫秒),直到頁面中的元素不存在(即已移除),然後再執行任何其他命令或斷言。
如果經過指定時間後元素仍然存在,則測試會失敗。
您可以透過在您的 nightwatch.json
中或在您的外部全域檔案中定義 waitForConditionPollInterval
屬性(以毫秒為單位)來變更輪詢間隔。
同樣地,可以將預設逾時指定為全域 waitForConditionTimeout
屬性(以毫秒為單位)。
用法
.waitForElementNotPresent([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.waitForElementNotPresent('#dialog');
// specify the locate strategy (css selector/xpath) as the first argument
browser.waitForElementNotPresent('css selector', '#dialog');
// with explicit timeout (in milliseconds)
browser.waitForElementNotPresent('#dialog', 1000);
// continue if failed
browser.waitForElementNotPresent('#dialog', 1000, false);
// with callback
browser.waitForElementNotPresent('#dialog', 1000, function() {
// do something while we're here
});
// with custom output message - the locate strategy is required
browser.waitForElementNotPresent('css selector', '#dialog', 'The dialog container is removed.');
// with custom Spanish message
browser.waitForElementNotPresent('#dialog', 1000, 'elemento %s no era presente en %d ms');
// many combinations possible - the message is always the last argument
browser.waitForElementNotPresent('#dialog', 1000, false, function() {}, 'elemento %s no era presente en %d ms');
},
'demo Test with selector objects': function(browser) {
browser.waitForElementNotPresent({
selector: '#dialog',
timeout: 1000
});
browser.waitForElementNotPresent({
selector: '#dialog',
locateStrategy: 'css selector'
}, 'Custom output message');
browser.waitForElementNotPresent({
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..waitForElementNotPresent('@dialogContainer', function(result) {
console.log(result);
});
}
}
參數
名稱 | 類型 | 描述 |
---|---|---|
using 選用 |
字串 | 要使用的定位策略。請參閱 W3C Webdriver - 定位策略 |
selector |
字串 | 物件 | 用於定位元素的選取器 (CSS/Xpath)。可以是字串或指定元素屬性的物件。 |
time=waitForConditionTimeout 選用 |
數字 | 在失敗之前等待的總毫秒數。 |
poll=waitForConditionPollInterval 選用 |
數字 | 檢查之間等待的毫秒數。您只有在同時指定 time 參數時才能使用此參數。 |
abortOnFailure=abortOnAssertionFailure 選用 |
布林值 | 預設情況下,如果找不到元素,測試將會失敗。如果您希望即使斷言失敗測試也繼續進行,請將此設定為 false。若要全域設定此項,您可以在全域變數中定義 |
callback 選用 |
函式 | 命令完成時要呼叫的選用回呼函式。 |
message 選用 |
字串 | 要在輸出中顯示的選用訊息;此訊息支援兩個預留位置:%s 表示目前的選取器,%d 表示時間(例如:元素 %s 在 %d 毫秒內不在頁面上)。 |