Waiting for unknown interaction result can be easily satisfied by Retrievers and their ability to wait for change of the conditions and also return the current value.
We can modify the application introduced in the previous example - we do not have application generating sequence of numbers, it generates the random numbers greater than previous one. For this purpose we have one button, and one input box:
ElementLocator GENERATE_RANDOM = ...;
ElementLocator RANDOM_VALUE = ...;
String initialRandomValue = selenium.getText(RANDOM_VALUE);
selenium.click(GENERATE_RANDOM);
waitAjax.waitForChange(initialRandomValue, retrieveText.locator(RANDOM_VALUE));
String randomValue = selenium.getText(RANDOM_VALUE);
assertTrue(Long.valueOf(initialRandomValue) > Long.valueOf(randomValue));
In this example, the retrieveText is the Retriever (with type TextRetriever) and waitForChange is a method which waits for change of the first argument's value to value obtained by retriever specified as second argument.
We can also tune this sample in several ways:
1) use the second version of waitForChange, called waitForChangeAndReturn, which additionally returns the new value:
String randomValue = waitAjax.waitForChangeAndReturn(initialRandomValue, retrieveText.locator(RANDOM_VALUE));
2) We can introduce the retriever e.g. on class properties level, and then we can call it in the test on every place we need the value.
TextRetriever retrieveRandom = retrieveText.locator(RANDOM_VALUE);
...
String initialRandomValue = retrieveRandom.retrieve();
...
String randomValue = waitAjax.waitForChangeAndReturn(initialRandomValue, retrieveRandomValue);
3) we can create own Retriever, which will not return the String, but Long instead (such retriever is not currently implemented in Library). It will little simplify our tests.