JBoss Community Archive (Read Only)

Graphene 2

Waiting API

WebDriver execution is always faster than changing the state of the page. This powerful API, should help you to synchronize with the tested page. See also Graphene Request Guards as the other method of synchronizing with the page.

The starting point for Wait API is Graphene Utility class and its methods:

waitModel(); //waiting for long client-server operations, e.g. database fetch
waitAjax(); //waiting for an Ajax update on the page
waitGui(); //waiting for a GUI client operation

They are just aliases on the same mechanism with different timeouts. Their purpose is to group 3 most common operation types, that require waiting for them to finish.

Please note that waitAjax does not require any XHR request to happen. It's just about how long test should wait!

Refer to Graphene Configuration page to see the configuration properties, in case you want to change default timeouts for those three waiting types.

Fluent API

One of the ways to go with waiting API, is to use its fluent version. The tree of the possible method calls is:

waitGui().until().element(webElementOrBy).is().
                                         .is().not().present();
                                                    .enabled();
                                                    .selected();
                                                    .visible();

//some attribute contains given value
waitGui().until().element(webElementOrBy).attribute("class").contains("foo-bar");

//you can explicitly set how often should Graphene poll the page for the result of the waiting condition
waitGui().pollingEvery(1, TimeUnit.SECONDS);

//you can set explicitly the timeout
waitGui().withTimeout(10, TimeUnit.SECONDS);

//you can set the message with which it would fail
waitGui().withMessage("The popup was not shown within the timeout!");

//or ignore some of the exceptions, which are thrown during waiting
waitGui().ignoring(StaleElementReferenceException.class);

Refer to Graphene javadoc for complete picture of all possible combinations.

Use IDE autocomplete feature to work most efficiently with the fluent API.

Using Predicate

Using the fluent builder is the preferred way to keep your tests readable. Indeed there are cases where you need something more. Use Predicate condition as a fallback:

waitGui().until(new Predicate<WebDriver>() {
       public boolean apply(WebDriver input) {
            //some complex finding out whether the condition is met
       }
});

Using conditions only

Sometimes you just need to assert that a condition is being met, you do not need to wait for something. Following code will return a boolean value:

new WebElementConditionFactory(webElement).isEnabled().apply(webDriverInstance);
new WebElementConditionFactory(webElement)).attribute("class").contains("foo-bar").apply(webDriverInstance);
JBoss.org Content Archive (Read Only), exported from JBoss Community Documentation Editor at 2020-03-10 12:14:27 UTC, last content change 2013-09-10 21:29:39 UTC.