appium / appium/java-client

Can present element be tested for visibility without polling view tree?

Open
#1,020 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.3k
Forks
752
Avg merge
6d 21h
Merged PRs (30d)
8

Description

I don't know if this is the right place to ask this question, and if so, please direct me to the correct forum.

Basically, I want to know if when checking for visibility of an element, should I use the WebDriver to poll the view tree for visibility, or is it sufficient for me to poll the element for presence, and subsequently once I have the present element to test for the visibility of the element separately.

Long explanation for clarity:

When determining if a view is displayed (present but also visible) to the user, I use the expected condition

ExpectedConditions.visibilityOfElementLocated(By.name(name))

rather than

ExpectedConditions.presenceOfElementLocated(By.name(name))

and then I use the waiter with the condition to get the element

new WebDriverWait(user.mDriver, timeOut)
  .ignoring(StaleElementReferenceException.class)
  .ignoring(NullPointerException.class)
  .ignoring(java.lang.ClassCastException.class)
  .pollingEvery(Duration.ofMillis(250))
  .until(condition);

And by inspecting the implementation of ExpectedConditions.visibilityOfElementLocated(By.name(name)) I see that the each time the view is polled, it is checked for visibility

public static ExpectedCondition<WebElement> visibilityOfElementLocated(final By locator) {
  return new ExpectedCondition<WebElement>() {
    @Override
    public WebElement apply(WebDriver driver) {
      try {
        return elementIfVisible(findElement(locator, driver));
      } catch (StaleElementReferenceException e) {
        return null;
      }
    }

    @Override
    public String toString() {
      return "visibility of element located by " + locator;
    }
  };
}

So my question is, should I poll the view tree each time to check if the view is visible, or will it be sufficient for me to poll for presence using ExpectedConditions.presenceOfElementLocated(By.name(name)), get the present element, and pass the element to a method to test for visibility e.g.

public static boolean isDisplayed(Duration timeOut, WebElement webElement)
{
  long startTimeMills = System.currentTimeMillis();
  while(!webElement.isDisplayed())
  {
     // check for time out
     long timeDiffMills = System.currentTimeMillis() - startTimeMills;
     if(timeDiffMills > timeOut.toMillis())
     {
        // could not confirm visibility within timeout
        Log.d(TAG,"could not confirm visibility of element, " +
              "timed-out in "+timeDiffMills+" milliseconds");
        return false;
     }
     // check visibility every 250 mills
     Util.waitTest(250);
  }
  // visibility confirmed within time out
  Log.d(TAG,"verified visibility of element");
  return true;
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file or test is named. Start by tracing ExpectedConditions.visibilityOfElementLocated, presenceOfElementLocated, WebDriverWait, and WebElement.isDisplayed in the Java client; done would require a project-backed answer or targeted documentation and tests clarifying the supported visibility-checking approach.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
testing-qa
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.