TESTCAFE EVENTS
The TestCafe events allow you to respond to task completion and worker collection changes.
taskComplete
Occurs when a testing task is completed.
Test tasks are initiated by calling the runTests method. A task can include an individual test, a fixture, or all fixtures within the root tests directory. Once all tests in a task are complete, the TestCafe raises the taskComplete event, which lists detailed information about passed and failed tests (if any). Handle this event to log results, and return a success or error code to the system, depending on the results of the test execution.
testCafe.on('taskComplete', function (report));
The following is a simple example that handles the taskComplete event to check if any of the tests have failed. If so, it will log the report and return an error to the user.
testCafe.on('taskComplete', complete); testCafe.runTests({ browsers: testCafe.listAvailableBrowsers(), emulateCursor: false, quarantineMode: true }); function complete(report) { if (report.failed == 0) { console.log("All tests passed successfully!"); process.exit(0); } else { console.log(JSON.stringify(report, null, 4)); process.exit(1); } }
In the code above, report is an object that contains detailed information about the results of test task execution. To learn about its structure, see Test Run Reports.
taskUpdated
Occurs each time an individual test has been executed.
Test tasks are initiated by calling the runTests method. A task can include an individual test, a fixture, or all fixtures within the root tests directory. The taskUpdated event fires when the task enters the Pending or Running state, and after each individual test has been executed. After all tests have been executed and the task changes its state to Completed, TestCafe raises the taskComplete event.
testCafe.on('taskUpdated', function (report));
The following example handles the taskUpdated event to display condensed information about each task execution stage.
testCafe.on('taskUpdated', updated); testCafe.runTests({ browsers: testCafe.listAvailableBrowsers(), emulateCursor: false, quarantineMode: true }); function updated(report) { console.log('Task: ' + report.name + ', Status:' + report.status); var processedTests = report.failed + report.passed; console.log('Processed ' + processedTests + ' of ' + report.testCount + ', failed: ' + report.failed); console.log('======================'); }
In the code above, report is an object that contains detailed information about the results of test task execution. To learn about its structure, see Test Run Reports.
workerAdded
Occurs immediately after a remote worker has been connected.
testCafe.on('workerAdded', function (workerName));
workerName - String. The worker name that was specified when the worker connected to TestCafe.
Note that browsers registered in the TestCafe configuration file or passed to the constructor in code are also registered as workers, and thus raise this event as well. The following example shows how to differentiate between the predefined and remote workers in order to run tests in remote workers only.
testCafe.on('workerAdded', function (worker) { if (testCafe.listAvailableBrowsers().indexOf(worker) > -1) return; var options = { workers: [worker], emulateCursor: false; } testCafe.runTests(options, function () { testCafe.on('taskComplete', function (report) { console.log(report); }); }); });
workerDisconnected
Occurs immediately after a remote worker has been disconnected.
testCafe.on('workerDisconnected', function (workerName));
workerName - String. The worker name.
Handle this event to perform any cleanup actions required after handling the workerAdded event.
Note that this event does not fire when a browser registered in the TestCafe configuration file or passed to the constructor in code is closed (unlike the workerAdded event that is raised when such a browser is launched).