【问题标题】:How to wait for element using mocha, chai & protractor如何使用 mocha、chai 和量角器等待元素
【发布时间】:2020-01-15 11:29:58
【问题描述】:

所以我的想法是创建一个函数来尝试在 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 秒,然后如果找到元素然后我们继续,否则基本上抛出异常。

【问题讨论】:

    标签: javascript protractor mocha.js chai


    【解决方案1】:

    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');
        }
    };
    

    【讨论】:

      【解决方案2】:

      当你通过 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);
      

      【讨论】:

        猜你喜欢
        • 2020-02-28
        • 2016-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-24
        • 1970-01-01
        • 2019-10-28
        相关资源
        最近更新 更多