如何使用 mocha、chai 和量角器等待元素
How to wait for element using mocha, chai & protractor
所以我的想法是制作一个函数,尝试在 x 秒内找到一个元素。如果元素未出现(能够在元素上书写)and/or 无法向元素发送任何键,则等待。如果它通过了给定的等待秒数(等 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 找到元素是 presented/able 到 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 秒之前找到它 return 预期值
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);
所以我的想法是制作一个函数,尝试在 x 秒内找到一个元素。如果元素未出现(能够在元素上书写)and/or 无法向元素发送任何键,则等待。如果它通过了给定的等待秒数(等 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 找到元素是 presented/able 到 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 秒之前找到它 return 预期值
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);