2

I have this code:

@FindBy(how = How.CLASS_NAME, using = "loading-class")
WebElement loadingAnimation;

When I try to use this line:

waitDriver.until(ExpectedConditions.invisibilityOf(this.loadingAnimation));

It throws exception:

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for invisibility of Proxy element for: DefaultElementLocator 'By.className: loading-container' (tried for 30 second(s) with 500 milliseconds interval)

for some reason it works when I debug the code.

I should mention that this works also:

while(true)
        {
            try {
                this.loadingAnimation.isDisplayed();
            }
            catch (Exception e) {
                break;
            }
        }

How to make it work using Page Factory?

Roy
  • 123
  • 2
  • 9

1 Answers1

1

When you implement Page Object pattern and initialize your page with PageFactory Selenium offers you the capability of using custom ElementLocator. There is AjaxElementLocator that is supposed to be used for dynamic elements.

In order to apply your own logic of how to consider your element ready for use, there is a method which default implementation is

protected boolean isElementUsable(WebElement element) {
  return true;
}

So you are free to override that one to implement your condition. In a nutshell you need to implement your custom ElementLocatorFactory which would be producing AjaxElementLocator objects with your customized isElementUsable method. Then you would initialize your page with

public class YourPage{

   ...   

   public YourPage(...){
      PageFactory.initElements(new YourCustomLocatorFactory(...), this);
   }

   ...

}

You can find here the completed example of how to wait for certain state of dynamic elements of your page objects not using ExpectedConditions utility.

Alexey R.
  • 8,057
  • 2
  • 11
  • 27