Skip to content

Waits and timeouts

Lucia Jelinkova edited this page Nov 16, 2015 · 24 revisions

Waits are used in cases when tests have to wait for some time, state or condition. Best practice is to use conditional waiting. Test continues only when specific wait condition pass and therefore developer can be sure that the test is in proper state. Unfortunately there are situations when such a condition doesn't exist. In that case it's fine to create your own condition. It's not advised to use fixed waiting time from AbstractWait class. Try to use relevant wait condition or create your own which handle waiting in better way by periodically testing a wait condition.

Conditional waiting

RedDeer implements two basic waits to perform conditional waiting:

  • WaitUntil - waiting until a specific condition is met, then testing ends

  • WaitWhile - wait while a specific condition is met, then testing ends

Wait duration can be set by setting specific timeout using TimePeriod. If timeout is not specified TimePeriod.NORMAL is used as a default. Once the wait condition timeout, a new WaitTimeoutExpiredException is thrown.

User can also set his own time period between performing two tests of a wait condition in a constructor of a conditional waiting. This is suitable for such cases when condition could be met significantly faster or slower and it would either spare time to find out that condition was met or it would do less useless testing of a test condition.

Usage

Waits until shell with text "Delete Server" is available with default timeout:

new WaitUntil(new ShellWithTextIsAvailable("Delete Server"));

Waits while there are running jobs with TimePeriod.LONG timeout:

new WaitWhile(new JobIsRunning(), TimePeriod.LONG);

Wait until shell with text "Cheatsheet available" is Available, with TimePeriod.LONG timeout, and testing of a test condition every 5 seconds. If shell is not found, no exception is thrown:

new WaitUntil(new ShellWithTextIsAvailable("Cheatsheet available"),
              TimePeriod.LONG, false, TimePeriod.getCustom(5));

Waiting for exact time period (not recommended)

To pause test for exact time period use static method sleep(timePeriod) of class AbstractWait. It will sleep current thread for a specified time period.

Usage

Pauses test for 5 seconds:

AbstractWait.sleep(TimePeriod.getCustom(5));

TimePeriod

TimePeriod class contains several predefined time periods. If there is not desired time period, you can use static method getCustom(timeInSeconds) to create your own time period.

Sometimes you may experience that the build-in wait time periods are not suitable for slow machines (e.g. in CI environment). For those cases you may define timeout factor - factor by which are the timeout periods multiplied.

Example: By setting property -Drd.time.period.factor=5 the tests will wait 5 times longer for conditions to fulfil.

Usage

Creates custom TimePeriod with duration of 20 seconds:

TimePeriod.getCustom(20);

Set the timeout factor for all tests via command line:

-Drd.time.period.factor=5
Clone this wiki locally