Skip to content
markysoft1 edited this page May 15, 2016 · 2 revisions

Waiting

Implicit waits are automatically done by Selenium, but there are situations where this mechanism doesn't work correct or could not be applied. For that cases you must do some explicit waits.

Vani provides some utilities for such situations like waiting until specific jQuery ajax call is finished or specific element is visible.

WaitUtil

This class provides some convenience methods to create wait conditions and execute it.

Method Description
.element(WebElement element) target of the wait condition is the provided element
.element(String selector) target should be located with provided selector
.element(String selector, SearchContext rootElement) selector is limited to provided rootElement
.waitTime(long millis) will wait the specified time (by Thread.sleep)
.ajaxJQuery(long timeoutInMillis, WebDriver webDriver) will wait until no jQuery ajax request are pending
.ajaxJQuery(long timeoutInMillis, long delayInMillis, WebDriver webDriver) will wait delayInMillis before calling the above one
.ajaxJQuery(String url, long startInMillis, long timeoutInMillis, WebDriver webDriver) will wait until a jQuery ajax call for given URL (regex is also supported) is sent after provided startInMillis mark or timeoutInMillis is reached

After calling a element-method, you get an instance of WaitOperatorBuilder. With this one, you specify your wait operator. This class provides some methods which supports Java 8 Lamba expressions. So you can write something like that:

waitUtil.element(selector, rootElement).is(Is::displayed)

waitUtil.element(selector, rootElement).is(Is::displayed).not()

waitUtil.element(selector).is(Is::selected)

Furthermore conjunctions are also supported, but you can only define one conjunction per condition:

waitUtil.element(selectorCheckbox).is(Is::selected).and().element(selector, rootElement).is(Is::displayed)

Finally, the timing parameters must be declared and condition execution will be started:

.until(long timeout, long period, WebDriver webDriver)

//example
waitUtil.element(selector).is(Is::selected).until(10*1000,200, webDriver);

The above example will wait 10 seconds until matching element is selected. The check will be repeated after 200 ms.

@ContentWait

You can also declare the waiting condition on class level. So the condition will be automatically executed before locating of each field except caching is enabled for it.

The annotation only needs a jQuery selector. Vani will wait until selector returns any matches or timeout is reached. The timing parameters timeout (default: 10 seconds) and pollingTime (default 1 second) can be specified in the annotation. As waiting condition, you must provide a Spel expression or use the default one (hasMatches()).

@ContentWait("${config.jq.finishIcon}")
public SearchResultPage extends PageObject{
}

@AjaxWait

It works similar like @ContentWait. The wait will be excuted before locating of each field except caching is enabled for the corresponding field. It will wait until no jQuery ajax request is pending or a timeout (default 5 seconds) is reached.

@AjaxWait
public SearchResultPage extends PageObject{
}

@Xhr

If you annotate method with this annotation, Vani will expect that the corresponding method causes a jQuery ajax call. So the framework will wait until the request is made or timeout (default 30 seconds) is reached.

Note: For this feature, xhr tracking must be enabled. This is automatically done during first jQuery call by Vani for current page, because the jQuery will be extended with a tracking logic. This code will save the url of all sent xhr request in a page cache. For example:

public SomeFragment extends FragmentObject{
  @Xhr("${content.xhr.searchUrl}")
  public void checkFilterOption(){
    ...
  }
}
Clone this wiki locally