[英]How to wait for element using mocha, chai & protractor
所以我的想法是創建一個函數,試圖在 x 秒內找到一個元素。 如果元素未呈現(能夠在元素上寫入)和/或無法向元素發送任何鍵,則等待。 如果它通過了給定的等待秒數(等 10 秒),那么它應該拋出異常。
現在我做了:
it('enter email', function (done) {
browser
.then(() => browser.wait(piPage.getEmailValue().isPresent(), 10000))
//getEmailValue = element(by.id('email').getAttribute("value");
.then((isPresent) => {
assert.equal(isPresent, true, 'Email failed entering.')
})
.then(() => piPage.enterEmail("test@test.com"))
.then(() => done());
});
如果值出現,它實際上會找到元素並發送鍵。 但是,似乎 10 秒 browser.wait 似乎並不適用,而是立即觸發而根本不等待。 我不得不手動添加
browser.driver.sleep(10000).then(function() {
console.log('waited 10 seconds');
});
但這不是我想要的。
我想要做的是讓 browser.wait 找到元素被呈現/能夠 send_keys 直到 x 秒,然后如果找到元素然后我們繼續,否則基本上拋出異常。
isPresent()
方法等待元素出現在 html DOM 中,但這並不一定意味着元素是可交互的。 為此,您需要使用像elementToBeClickable(element)
這樣的顯式等待來鏈接它
const EC = protractor.ExpectedConditions;
waitForElement = async () => {
const present = await yourElement.isPresent();
if (present) {
await browser.wait(EC.elementToBeClickable(yourElement));
await yourElement.sendKeys('something');
}
};
當您通過 10 秒時,實際上是說:我會給您 10 秒的時間來找到它,但是如果您在 10 秒之前找到它,則返回預期的值
Schedules a command to wait for a condition to hold. The condition may be specified by a {@link webdriver.Condition}, as a custom function, or as a {@link webdriver.promise.Promise}.
For a {@link webdriver.Condition} or function, the wait will repeatedly evaluate the condition until it returns a truthy value. If any errors occur while evaluating the condition, they will be allowed to propagate. In the event a condition returns a {@link webdriver.promise.Promise promise}, the polling loop will wait for it to be resolved and use the resolved value for whether the condition has been satisified. Note the resolution time for a promise is factored into whether a wait has timed out.
Note, if the provided condition is a {@link WebElementCondition}, then the wait will return a {@link WebElementPromise} that will resolve to the element that satisified the condition.
Example: waiting up to 10 seconds for an element to be present and visible on the page.
據我所知,唯一的解決方案是使用sleep()
我建議使用而不是許多 then()
import { browser, element, by, ExpectedConditions as ec } from 'protractor';
await browser.wait(ec.visibilityOf(element(by.id('menu-id')), 5000);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.