Explicit Wait

Some UI tests rely on long implicit timeout to find an element that appears as the result of a user action. Instead of waiting for an implicit time, explicitly wait for a condition to be ready for the test to proceed.

This tutorial simulates sending a message to a server and waiting for a response with a success message.

In the page object, we use the compose syntax with the apply value set to waitFor, which is a public method that exercises a fluent wait for an element. You don't need to specify an element when you use waitFor. The element's value defaults to self, which is a reference to the current page object.

The wait wraps one or more functions and repeatedly invokes them within a timeout until they return a truthy value.

A function invoked inside waitFor is defined in the predicate array in the args array, which has these arguments:

  • type is set to function
  • name is any string and isn't relevant in this case
  • predicate is an array of compose statements that is invoked to test for the wait condition.

In our example, the method waits until a success message matches the expected string "Message was sent". This condition requires a stringEquals matcher.

As for any compose method, the return value is determined by the last statement. The waitFor return value is a truthy value returned from the predicate.

Notice that it takes a few seconds for the test to complete because of the delayed "server response", which is simulated for this tutorial.