Jacob
Jacob

Reputation: 29

Why isDisplayed returns No element found using locator instead of boolean? - protractor

I have a method isElementDisplayed with element.isDisplayed call inside.

Why does the isDisplayed return No element found when it could return a boolean?

isElementDisplayed(element: ElementFinder) {
    return element.isDisplayed();
}

Upvotes: 0

Views: 803

Answers (3)

Ali Azam
Ali Azam

Reputation: 2115

isDisplayed() would check if an element is visible or not, but you need to check whether an element is present in DOM or not, use isElementPresent() or isPresent():

expect(browser.isElementPresent(element(by.id('ELEMENT_ID_HERE')))).toBe(false);
expect(element(by.id('ELEMENT_ID_HERE')).isPresent()).toBe(false);

See also:

Upvotes: 1

Helping Hands
Helping Hands

Reputation: 5396

You should try global method and call it where you need. Something like this :

public static boolean ElementVisible(By element, WebDriver driver) {

        // Maximum wait time is 60 seconds
        int maxAttempts = 2;
        long sleepTimeInMillisForEachIteration = 500;

        for (int counter = 0; counter <= maxAttempts; counter++) {
            try {

                    driver.findElement(element);        

                return true;
            } catch (NoSuchElementException | ElementNotVisibleException e) {
                if (counter == maxAttempts) {
                    return false;
                }
                try {
                    Thread.sleep(sleepTimeInMillisForEachIteration);
                    continue;
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return false;
    }

So here it will interact with element if it is visible or display otherwise it will handle exception and return false.

Upvotes: 0

Thareek
Thareek

Reputation: 11

exception was thrown by find element. Use try & catch block to return false.

try{
return element.isDisplayed();
} 
catch (Exception e) 
{
return false; 
// e.getMessage(); // you can console the exception message if required
}

Upvotes: 0

Related Questions