Perform Timed
Built-in .perform()
command uses asyncHookTimeout
value for the done
invocation timeout.
By default asyncHookTimeout
value is set to 10 seconds, but there may be cases
where this value should be customized in order to control callback maximum
execution time. So to allow to define this value for each case separately and
avoid asyncHookTimeout
redefining .performTimed()
command was created.
client.performTimed((callback: function), (timeout: int));
The callback
parameter defines the function that will be executed as part of
the command queue. Same as built-in
.perform()
command
the callback signature can have up to two parameters:
- no parameters - callback runs and perform completes immediately at the end of the execution of the callback.
- one parameter - allows for asynchronous execution within the callback.
The parameter must be a
done
callback function that will indicate the completion ofperformTimed
callback function. - two parameters - allows for asynchronous execution with the Nightwatch
API
object passed in as the first argument, followed by thedone
callback.
The timeout
parameter specifies the time in milliseconds, that describes the
done
invocation timeout.
The performTimed()
command will be useful in cases where
done
callback is used within the custom callback function in order to notify
the
Nightwatch command queue
about the end of command execution. Without the done
callback invocation
performTimed()
command behaves the same as built-in perform()
command and
completes immediately after the callback is run.
client => {
let version = "";
client
.url("https://www.bing.com")
.performTimed((done) => {
// Get latest ChromeDriver version
getChromeDriverVersion(done);
}, 20000)
// Insert Bing search query for ChromeDriver version
.perform(() => {
client.setValue("[type=search]", [`chromedriver ${version}`, client.Keys.ENTER])
})
// Wait up to 10 seconds until search results are visible
.waitForElementVisible("[aria-label='Search Results']", 10 * 1000);
function sleepFor(sleepDuration) {
const now = new Date().getTime();
while (new Date().getTime() < now + sleepDuration) { }
}
function getChromeDriverVersion(done) {
retry = 60;
request(
{
url: `https://chromedriver.storage.googleapis.com/LATEST_RELEASE`,
},
function (error, response, body) {
if (error) {
throw new Error(error);
}
if (response.statusCode != 200) {
sleepFor(1000);
retry--;
if (retry > 0) {
getChromeDriverVersion(done, retry);
} else {
done();
}
} else {
version = body;
done();
}
}
);
}
}