Click here to see your recently viewed pages and most viewed pages.

Conditions and Handlers

Conditions and handlers are good ingredients for writing robust tests.

  1. Conditions
    1. Condition Factories
    2. Conditions and Assertions
  2. Handlers
    1. Swing Condition Handling


A Condition encapsulates tests to determine whether a particular situation exists, while a Handler encapsulates actions to be taken when a particular Condition is determined to be true. Conditions can be used to keep test execution in sync with the application being tested or to assert a particular situation is true for test validation. A Condition and Handler pair can be used to react to known but unpredictable situation making the test more robust. Conditions implement ICondition or IUICondition while handlers and condition/handler pairs implement IHandler and IConditionHandler respectively.

For example, when waiting for the UI thread to finish processing, rather than ...



it is better to use a Condition as in ...

ui.wait(new ShellShowingCondition("My Dialog"));
ui.wait(new ShellDisposedCondition("My Dialog"));
ui.wait(new IsEnabledCondition(new ButtonLocator("Finish")));
ui.wait(new FileExistsCondition("MyProject/newFile.txt")), true);

... or in Swing ...

ui.wait(new WindowShowingCondition("My Dialog"));
ui.wait(new WindowDisposedCondition("My Dialog"));
ui.wait(new IsEnabledCondition(new JButtonLocator("Finish")));

More interfaces and various implementers of ICondition and IUICondition can be found in ...

Condition Factories

An alternative way to create conditions is through condition factories. Condition factories associate conditions with their host locators. For example, asserting enablement for an SWT Button could look like this:

ui.assertThat(new ButtonLocator("OK").isEnabled());

For more on condition factories see this document.

Conditions and Assertions

Conditions can also be used to assert or ensure that the application being tested is in a valid state using the ui.assertThat(...) or ui.ensureThat(..) methods respectively. For more on assertions see here.


A Condition/Handler pair is registered with the IConditionMonitor or IShellMonitor to react to unpredictable events, thus making the test more robust. For example, an application that displays a license dialog sometime during application execution make cause tests that normally pass to fail. To address this situation, add a Condition to detect this situation associated with a Handler to react to the situation ...

monitor = (IConditionMonitor) ui.getAdapter(IConditionMonitor.class);
monitor.add(new ShellShowingCondition("License*"), new IHandler() {
      	 public handle(IUIContext ui) { ButtonLocator("OK")); 

Conditions are processed before each UI action is performed. For example:

//conditions processed before menu selection MenuItemLocator("New/File"));
//conditions processed during wait
ui.wait(New ShellShowingCondition("New File"));
//conditions processed before click ButtonLocator("Cancel"));
//conditions processed during wait
ui.wait(new ShellDisposedCondition("New File"));

Swing Condition Handling

The same approach is used to deal with unpredictable events in Swing applications. To address a condition where a Swing dialog may pop up unexpectedly, add a Condition to detect this situation and associate a Handler to react to this situation

monitor = (IConditionMonitor)ui.getAdapter(IConditionMonitor.class);
monitor.add(new WindowShowingCondition("License*"), new IHandler() {
	 public handle(IUIContext ui) { JButtonLocator("OK"));