.waitForElementPresent() 編集を提案する
他のコマンドやアサーションを実行する前に、ページに要素が存在するまで、ミリ秒単位で指定された時間(デフォルトは5000ミリ秒)待機します。
指定された時間内に要素が存在しない場合、テストは失敗します。`abortOnFailure`を`false`に設定することで、これを変更できます。
`nightwatch.json`または外部グローバルファイル内のグローバルプロパティとして、ミリ秒単位で`waitForConditionPollInterval`プロパティを定義することで、ポーリング間隔を変更できます。
同様に、デフォルトのタイムアウトは、グローバルな`waitForConditionTimeout`プロパティ(ミリ秒単位)として指定できます。
使用方法
.waitForElementPresent([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.waitForElementPresent('#index-container');
// specify the locate strategy (css selector/xpath) as the first argument
browser.waitForElementPresent('css selector', '#index-container');
// with explicit timeout (in milliseconds)
browser.waitForElementPresent('#index-container', 1000);
// continue if failed
browser.waitForElementPresent('#index-container', 1000, false);
// with callback
browser.waitForElementPresent('#index-container', 1000, function() {
// do something while we're here
});
// with custom output message - the locate strategy is required
browser.waitForElementPresent('css selector', '#index-container', 'The index container is found.');
// with custom Spanish message
browser.waitForElementPresent('#index-container', 1000, 'elemento %s no era presente en %d ms');
// many combinations possible - the message is always the last argument
browser.waitForElementPresent('#index-container', 1000, false, function() {}, 'elemento %s no era presente en %d ms');
},
'demo Test with selector objects': function(browser) {
browser.waitForElementPresent({
selector: '#index-container',
timeout: 1000
});
browser.waitForElementPresent({
selector: '#index-container',
locateStrategy: 'css selector'
}, 'Custom output message');
browser.waitForElementPresent({
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.waitForElementPresent('@featuresList', function(result) {
console.log(result);
});
}
}
パラメータ
名前 | 型 | 説明 |
---|---|---|
使用 オプション |
文字列 | 使用するロケーター戦略。 W3C Webdriver - ロケーター戦略を参照してください。 |
セレクター |
文字列 | オブジェクト | 要素を見つけるために使用するセレクター(CSS/XPath)。文字列、または要素のプロパティを指定するオブジェクトのいずれかです。 |
time=waitForConditionTimeout オプション |
数値 | 失敗するまでの待ち時間(ミリ秒)。 |
poll=waitForConditionPollInterval オプション |
数値 | チェック間の待ち時間(ミリ秒)。timeパラメータも指定した場合のみ使用できます。 |
abortOnFailure=abortOnAssertionFailure オプション |
ブール値 | デフォルトでは、要素が見つからない場合、テストは失敗します。アサーションが失敗してもテストを続行する場合は、これをfalseに設定します。グローバルに設定するには、globalsに`abortOnAssertionFailure`プロパティを定義します。 |
コールバック オプション |
関数 | コマンドが完了したときに呼び出されるオプションのコールバック関数。 |
メッセージ オプション |
文字列 | 出力に表示されるオプションのメッセージ。このメッセージは、現在のセレクターを表す%sと時間(例:要素%sは%dミリ秒間ページに存在しませんでした)を表す%dの2つのプレースホルダーをサポートしています。 |