Skip to main content

Time Execution

This custom command measures and reports execution time of some function. It allows timing the execution of some flow in a website, e.g., logging in, checking out in an e-commerce site, joining a WebRTC call. Results from this command can be used in post-run analysis.

client.timeExecution(name: string, timedCommand: Function, timeout?: number);

name - identifying name for the timedCommand function. If name is an empty string, then timedCommand.name attribute value will be used if it is available, otherwise name will default to "anonymous". If name is provided then the maximum length for this argument is 150 characters and can only contain alphanumeric characters, underscores and hyphens.

timedCommand - function whose execution will be timed. timedCommand will be executed as part of the NightWatch command queue. timedCommand can have up to two parameters:

  • no parameters - function runs and execution timing completes immediately at the end of the execution of the function.
  • one parameter - allows for asynchronous execution within the function. The parameter must be a done callback function that will indicate the completion of timedCommand function. If done callback is not invoked, function execution will last indefinitely.
  • two parameters - allows for asynchronous execution with the Nightwatch API object passed in as the first argument, followed by the done callback.

timeout - specifies the time in milliseconds, how long to wait for the timedCommand to execute. Timeout value must be positive. If the execution time exceeds the timeout value, then an error will be thrown. If timeout parameter is omitted then no limit is placed on the execution time of timedCommand.

Use of the timeExecution custom command will produce a log that contains timing information of the timedCommand. The log message structure is shown bellow.

[LOADERO] Execution time for '${NAME}': ${DURATION}ms (start: ${START}; end: ${END}).
  • ${NAME} has the value that was supplied to timeExecution name parameter.
  • ${DURATION} is the execution time in milliseconds of the timedCommand function
  • ${START}, ${END} are unix timestamp values in milliseconds.
client => {
const reallyLongPause = 5 * 60 * 1000; // 5 minutes

client
// Example of timing execution without specifying a timeout.
.url("https://duckduckgo.com/")
.timeExecution("locate_search_bar", () => {
client
.waitForElementVisible("#searchbox_input", 10 * 1000)
.sendKeys("#searchbox_input", "QA Processes")
.click("[aria-label='Search']")
.waitForElementVisible("#r1-0 > div > h2", 10 * 1000)
.pause(reallyLongPause);
})
.takeScreenshot("screenshot.png");
}
client => {
client
// Example of timing execution with a timeout.
.url("https://duckduckgo.com/")
.timeExecution(
"locate_search_bar",
() => {
client
.waitForElementVisible(
"#searchbox_input",
10 * 1000
)
.sendKeys("#searchbox_input", "QA Processes")
.click("[aria-label='Search']")
.waitForElementVisible("#r1-0 > div > h2", 10 * 1000);
},
10 * 1000
)
.takeScreenshot("screenshot.png");
}