0

I am into Development and new to QA/Automation testing. I am trying to understand the following code;

public WebElement getVisibleElement( final By by, final WebElement parentElement, int timeoutValue, TimeUnit timeoutPeriod, int pollingInterval, TimeUnit pollingPeriod ) {
    return fluentWait(timeoutValue, timeoutPeriod, pollingInterval, pollingPeriod).until( new Function<WebDriver, WebElement>() {
        public WebElement apply(WebDriver driver) {
            try {
            } catch {
            }
            return null; 
        }
    });
}   

In my same class, I also have;

public Wait<WebDriver> fluentWait(int timeoutValue, TimeUnit timeoutPeriod, int pollingInterval, TimeUnit pollingPeriod) {

    return new FluentWait<WebDriver>(this.webDriver)
                        .withTimeout(timeoutValue, timeoutPeriod)
                        .pollingEvery(pollingInterval, pollingPeriod)
                        .ignoring(NoSuchElementException.class);
}

Specifically 2 things I wanted to understand;

  1. What is return fluentWait() doing exactly?
  2. What does the use of until() mean here?
Eliran Malka
  • 15,821
  • 6
  • 77
  • 100
copenndthagen
  • 49,230
  • 102
  • 290
  • 442

1 Answers1

2

They tie in together so I won't answer them separately as such:

Selenium's FluentWait approach is generally just to wait for a certain condition to be true. You can give it a number of different possible conditions and it will keep evaluating it until it's true or your timeout value is passed, whichever is first.

a) return, in most programming languages is just returning something from a method.

b) until is the method you call on the FluentWait to have it physically evaluate a condition. Everything before that, is just setting it up, using .until(....) is telling the FluentWait instance to go and evaluate the code I'm giving you. In your case, I assume by the name of the method (the actual method is incomplete), by calling .until(....) you are telling the FluentWait to keep trying to grab an element from the page until it's physically visible to the user.

The fluentWait method is merely setting up a FluentWait instance to use, whilst the apply/until code section is setting up what condition you want to evaluate. In your getVisibleElement method you are returning a WebElement, so in your apply code section, you'll need to return the WebElement once you are satisfied it's visible to the user.

What I would assume you would want to do within the apply, is find the element using the normal .findElement and check it's visible property. If it's true, return the WebElement found, if not, return null to force the FluentWait to keep going.

The return fluentWait therefore, is merely saying "return whatever this FluentWait returns". Which, since the method has been already declared as returning a WebElement instance, you are therefore saying "return whatever WebElement this FluentWait returns".

Arran
  • 24,648
  • 6
  • 68
  • 78