简体   繁体   中英

protractor wait on condition should not fail after timeout

Is it possible to wait on a ExpectedConditions.visibilityOf without getting a failure if the element has not become visible? I want to handle a situation, where a button might has become visible through an animation and click it away.

browser.wait(conditions.visibilityOf(button), 500).then(function (visible) {
    if (visible) {
        return button.click().then(function () {/*...*/});
    }
});

I found out, that I can handle the rejected promise returned by wait to suppress the timeout error:

browser.wait(conditions.visibilityOf(button), 500).then(function () {
    // It is visible
    return button.click().then(function () {/*...*/});    

}, function() {
    // It is not visible
    if (shouldExpectVisibility) {
        // If I want to fail, I could reject again
        return protractor.promise.rejected('No such button');
    }
    else {
        // If I don't want to fail, I do nothing  
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM