0

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;

}
Umesh_IoT
  • 59
  • 1
  • 11
  • Why do you want that - what use case do you want to cover? Is it something like: you have an element that is initially invisible, but then becomes visible and you want to catch it being invisible first..? – alecxe Sep 14 '16 at 13:27

1 Answers1

0

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,
    }
Optimworks
  • 2,537
  • 17
  • 20
  • but if the element is not visible webdriver will wait for sometime until it is visible right? I want to get the visibility status without waiting. Can you please confirm? – Umesh_IoT Sep 14 '16 at 11:20
  • I dnt think so. And why do want that specific condition yaar – Optimworks Sep 14 '16 at 11:29
  • Or try to perform any action on element and use code - targetElement.click().then(function(value) { // fulfillment }, function(reasonForRejection) { // rejection }); – Optimworks Sep 14 '16 at 11:35
  • in above case, if element is not visible or clickable, it will immediately throw an error that will be sent as parameter to second function(reasonForRejection){} method in then() – Optimworks Sep 14 '16 at 11:37
  • And you can do what do you want – Optimworks Sep 14 '16 at 11:37
  • This is something I was looking for. Thanks for the information I will check it. – Umesh_IoT Sep 14 '16 at 11:41
  • When you define var targetElement=element(locator); and if the element is not displayed, WebDriver waits until it appear with its default timeout.So still its not going to solve it. – Umesh_IoT Sep 15 '16 at 04:02
  • @Umesh_IoT, just change _defaultTimeoutInterval:_, to zero (0) in conf.js file. I've updated my post – Optimworks Sep 15 '16 at 04:56