.perform() Suggest edits
A simple perform command which allows access to the Nightwatch API in a callback. Can be useful if you want to read variables set by other commands.
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.
In the case of asynchronous execution, the timeout can be controlled by setting the asyncHookTimeout
global. See Using test globals for more info.
Usage
Parameters
Name | Type | description |
---|---|---|
callback |
function | The function to run as part of the queue. |
Example
describe('perform example', function() {
var elementValue;
it('basic perform', function(browser) {
browser
.getValue('.some-element', function(result) {
elementValue = result.value;
})
// other stuff going on ...
// self-completing callback
.perform(function() {
console.log('elementValue', elementValue);
// without any defined parameters, perform
// completes immediately (synchronously)
})
// returning a Promise
.perform(async function() {
console.log('elementValue', elementValue);
// potentially other async stuff going on
return elementValue;
})
// DEPRECATED: asynchronous completion including api (client)
.perform(function(client, done) {
console.log('elementValue', elementValue);
done();
});
});
it('perform with async', function(browser) {
const result = await browser.perform(async function() {
return 100;
});
console.log('result:', result); // 100
})
}