How to use Protractor / WebDriverJS to check if an element is displayed without waiting?

243 Views Asked by At

How to use protractor to check if an element is visible without waiting? I'm from Java + WebDriver background and new to Protractor. In Java I used the following solution. I'm looking for a similar functionality.

More Information: Currently if I use isDisplayed(), WebDriver will wait until the element is visible (if it is not already displayed). I want to get the visibility status without waiting.

protected void turnOffImplicitWaits() {

    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);

}



protected void turnOnImplicitWaits() {

    driver.manage().timeouts().implicitlyWait(StartupConstants.TIMEOUT, TimeUnit.SECONDS);

}



protected boolean isElementHiddenNow(By by) {

    turnOffImplicitWaits();

    boolean result = ExpectedConditions.invisibilityOfElementLocated(by).apply(driver);

    turnOnImplicitWaits();

    return result;

}
1

There are 1 best solutions below

8
Optimworks On

If you don't want any default waits while checking for visibility of element, do one thing, that is just perform any operation like click() on target element and add then() function with two functions as parameters - one for success and other for failure. You follow the code below:

 var targetElement=element(locator);
 targetElement.click()
                   .then(function(toBeCalledWhenSuccess) { // fulfillment },    
                         function(reasonForRejection) { // rejection }
                        );

Add below code in Conf.js

   jasmineNodeOpts: {
       // Default time to wait in ms before a test fails.
       defaultTimeoutInterval: 0,
    }