Wait
The Wait modules lets the flow wait for various events.
Wait for seconds
Wait the given amount of seconds.
Parameters
timeoutthe number of seconds to wait
Example
Wait.forSeconds(2);Wait for milliseconds
Wait the given amount of milliseconds.
Parameters
timeoutthe number of milliseconds to wait
Example
Wait.forMilliseconds(200); // Wait for 0.2 secondsWait for field
Wait for the given field to appear - will return when field appear or throw an exception when the given amount of seconds has elapsed.
Parameters
fieldthe field to wait for e.g.Fields["myfield"]timeoutthe max amount of seconds to wait for the field to appearoptionsadditional optional argumentspollDelayInMsint, how many ms between checks that the field is present or not - default is200
Example
Wait.forField(Fields["myfield"], 10);
// Poll every 1s
Wait.forField(Fields["myField"], 10, { pollDelayInMs: 1000 });Wait for field to disappear
Wait for the given field to disappear - will return when field disappears or throw an exception when the given amount of seconds has elapsed.
Parameters
fieldthe field to wait for e.g.Fields["myfield"]timeoutthe max amount of seconds to wait for the field to disappear
Wait.forFieldToDisappear(Fields["myfield"], 10);Wait for window
Wait for the given window to appear - will return when a matching window appears or throw an exception when the given amount of seconds has elapsed. There is also a forWindowToDisappear variant.
Parameters
titlethe title of the window to wait fortimeoutthe max amount of seconds to wait for the field to appear
Example
// Wait for a window with Notepad in its title to appear, max 10s
Wait.forWindow("Notepad", 10);
// Wait for Notepad to disappear again
Wait.forWindowToDisappear("Notepad", 10);Wait for lock
Wait.forLock(...) allows for exclusive access to a shared resource among concurrently running flows (from separate sessions) or from other asynchronous tasks (e.g. when using the Task module).
Parameters
lockNameThe name of the lockcallbackThe function to call with exclusivity under the named lockoptsOptions (default: { timeout: 3000 })
Return value
true if the lock was obtained within the timeout. false otherwise
Example
To access a shared resource with exclusivity:
function accessSharedResourceFn() {
// Access the shared resource here...
}
if (!Wait.forLock("resourceLock", accessSharedResourceFn, { timeout: 5000 })) {
throw Error("Failed to access the shared resource");
}Wait for click
Wait.forClick(...) and Wait.forRightClick(...) can be used to wait for a user to click a given field.
Parameters
fieldan instance of aFieldto wait for click onoptionsan object containing optional arguments;throwsbool, iftruethen an exception is thrown if the field was not clicked beforetimouthas elapsed - default istruetimeoutint, for how many ms should we wait before giving up - default is60000
Return value
If option.throw is false then true is returned if the field was clicked, false otherwise.
Example
// Simple wait - will throw error if "OK" is not clicked within 60s
Wait.forClick(Fields["OK"]);
// Do not throw an error and wait only 5s
if (!Wait.forRightClick(Fields["Cancel"], { throws: false, timeout: 5000 })) {
// No click
} else {
// "Cancel" was clicked
}Wait for predicate
Wait.for(...) can be used to wait for an arbitrary condition to be met. Use this for any condition that isn’t directly supported by other methods in the Wait api.
Parameters
predicatea function returning a truthy value when the awaited condition is met and falsy values otherwise.optionsan optional object containing arguments;throwsbool, iftruethen an exception is thrown if the predicate has not returned a truthy value beforetimouthas elapsed - default istruetimeoutthe number of ms we wait before giving up - default is10000intervalthe number of ms to pause between each invocation of the predicate. Default is200
The first truthy value returned by the predicate is returned or null if no such value was produced. If the throw option is true (default) and no truthy value is produced by the predicate, an exception is thrown in stead.
function predicate() {
return new Field("**/combobox").read();
}
// Wait 3 seconds for field to get a value - no timeout exception
var maybeValue = Wait.for(predicate, { throws: false, timeout: 3000 });
if (!maybeValue) {
// Handle timeout
}
// Wait 10 seconds with exception on timeout
try {
Wait.for(predicate);
} catch (e) {
// Handle the timeout
}If you need to pass arguments, you can use a second-order function:
function hasValue(field, value) {
return new Field(field).read() === value;
}
if (Wait.for(hasValue("**/combobox", "Florida"))) {
// handle the case where the combobox has the value "Florida"
}