How to Know When MobileElement Is Invisible With PageFactory

My test is trying to verify that:

  1. A banner is shown when the device lost network
  2. Few seconds later, same banner is gone when network is established

I am using the PageFactory model to keep track of this banner view:

class SomeScreenObject {
    protected static final String UI_SELECTOR_NO_INTERNET_BANNER = "some UiSelector text";

    @AndroidFindBy(uiAutomator = UI_SELECTOR_NO_INTERNET_BANNER)
    public AndroidElement noInternetBanner;

I have a SomeScreen class that uses noInternetBanner with some helper methods:

class SomeScreen {
    private final SomeScreenObject object = new SomeScreenObject();

    public SomeScreen() {
        // Additional setup
        PageFactory.initElements(new AppiumFieldDecorator(driver), object);

    public SomeScreen isNoInternetBannerShown() { 
        Assert.assertTrue(object.noInternetBanner.isDisplayed(), "No internet banner should be displayed");

        return this;

    public SomeScreen isNoInternetBannerGone() {   
        wait.until(ExpectedConditions.invisibilityOfElementLocated(MobileBy.AndroidUIAutomator(UI_SELECTOR_NO_INTERNET_BANNER)));    // This passes
        wait.until(ExpectedConditions.invisibilityOf(noInternetBanner));    // This fails every time with TimeoutException
        Assert.assertFalse(checkIfElementIsPresent(object.noInternetBanner), "No internet banner should not be displayed");

        return this;

I run my test in the following way

// initialize configuration, driver, etc.
SomeScreen someScreen = new SomeScreen(driver);

// Turn off WiFi
someScreen.isNoInternetBannerShown()   // This always passes
// Turn on WiFi
someScreen.isNoInternetBannerShown().   // Only passes if I use MobileBy locator

My question is why does this work:

wait.until(ExpectedConditions.invisibilityOfElementLocated(MobileBy.AndroidUIAutomator(UI_SELECTOR_NO_INTERNET_BANNER)));    // This passes

...and this fail with a TimeoutException:

wait.until(ExpectedConditions.invisibilityOf(noInternetBanner));    // This fails every time

When I'm calling wait.until(ExpectedConditions.invisibilityOf()), in that moment, the banner is still shown on the screen. But since using MobileBy causes it to pass, that indicates to me that it must have something to do with the PageFactory and how it updates its reference to noInternetBanner. I know that it follows a lazy initialization pattern. Why am I running into this error?

Read more here:

Content Attribution

This content was originally published by coolDude at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: