Synchronization Pattern – Ladder
I was scripting up something this week and found that the normal ‘wait for element present’ tricks that would normally be applied to a dynamic page (where ‘latching’) were not working. Turns out the reason for this was that the page content was being updated before a critical bit of session information was updated. And since this script was for a load test, the timing of the delay was unpredictable so the brute-force ‘sleep’/’pause’ would have been too inefficient.
What did work is what I’ll call a ‘ladder’ wait. I have no idea if it has another name, but I’m calling it a ladder.
<pre lang="javascript">var ladder = 1000;
var success = false;
while (success != true) {
// do stuff that could be successful
// wait for some period of time, determined by the ladder
selenium.pause(ladder);
// check for the absence of the failure mode
if (! selenium.isTextPresent("Affiliated Organization is required")) {
success = true;
} else {
ladder = ladder + 1000;
}
}
The ‘magic’ of the ladder is that the wait time starts at some nice, acceptable value and then increases each time a failure occurs. Eventually the pause gets to the point where the state and script are synch and we move on.
A further refinement that could be done is to put an upper limit on the ladder so the script fails when the ladder hits the ceiling (ha!).