Devin C
Devin C

Reputation: 1268

Protractor click element only when present

Alright, so I thought this would be easy but I've spend a few hours searching and trying different methods with no avail. I need to tell protractor to only click on a button when it is present. To water it down basically my test checks if a certain value is present on a page. The page can be in one of two states, automatic mode, which contains my value I'm testing or design mode, which contains other values which are irrelevant for this test. If its in design mode, we first need to click on automatic mode which produces a prompt asking us if we are sure we want to continue and switch to automatic mode - this prompt contains the continue button I need to check for and click. If the page is already in automatic mode however, I don't need to perform this check, I can proceed to validate weather or not my value is present. Basically I tried this:

if (continueButton.isDisplayed()) {
continueButton.click(); 
} 

what happens I get a "No element found using locator:..." error. I tried a few different ways such as using isElementPresent, and some of the expected condition options but more or less I get the same results. How can I achieve this scenario where my test checks if a button is present and clicks it, and if it isn't present continues with the test.

Upvotes: 2

Views: 1708

Answers (2)

alecxe
alecxe

Reputation: 473873

browser.wait() and elementToBeClickable Expected Condition is exactly what might help:

var EC = protractor.ExpectedConditions;
browser.wait(EC.elementToBeClickable(continueButton), 5000);

continueButton.click();

Upvotes: 1

KCaradonna
KCaradonna

Reputation: 770

You can achieve this by using promises.

    continueButton.isDisplayed().then(function (isDisplayed) {
        if(isDisplayed){
        continueButton.click();
        }
    });

Upvotes: 1

Related Questions