如何使用摩卡,柴和量角器等待元素



所以我的想法是创建一个试图在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);

相关内容

  • 没有找到相关文章

最新更新