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();
}
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();
}
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:
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
}
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.