Perform Timed

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

    providing a done callback function for completion as the first argument.

  • two parameters - allows for asynchronous execution with the Nightwatch API

    object passed in as the first argument, followed by the done callback.

The timeout parameter specifies the time in milliseconds, that describes the done invocation timeout.

TheperformTimed() 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.
function (client) {
    // Example of setting perform timed callback
    function setTime(timestamp, done) {
        fetch('https://myendpoint.com/post', {
            method: "post",
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ timestamp })
        })
            .then(() => done())
            .catch(error => done(error));
    };

    client
        .url('https://mypage.com/')
        .takeScreenshot('screenshot_before.png')
        .performTimed(function (done) {
            setTime(new Date().getTime(), done);
        }, 20000)
        .takeScreenshot('screenshot_after.png')
};