Blog

How to automatically pause your Selenium WebDriver (Node.js) tests on every failure for debugging

by: Aswin Chembath

Background

At Testing Mavens, we maintain a number of large end to end testing frameworks in JavaScript, developed using either protractor or WebdriverIO with jasmine. These tests are run in the CI/CD pipeline and monitored daily. To analyze the failures, we run the tests locally, set breakpoints for debugging and fix the failures.

Problem  

When the test suite is large and there are multiple failures, deciding where to put the breakpoints itself is a tedious task. Won’t it be nice if the test script can pause automatically for debugging when a failure occurs? Then you can closely look at the application under test, make necessary changes to the test script and finally continue running the tests. This will help us in saving a lot of time while doing the script maintenance. 

Solution

We can achieve the behavior (of pausing the script execution on failure) by making simple changes to the configuration files to call the JavaScript debugger on failure.

For Protractor – Jasmine framework,

Add the below lines of code inside the onPrepare() method in your Protractor config

jasmine.getEnv().addReporter({
    specDone: async function (spec) {
        if (spec.status === "failed") {
            console.log("Error Occurred, pausing script for debugging");
            debugger;
        }
    },
});

For WebdriverIO – Jasmine framework,

We can use the expectationResultHandler() method in the wdio.conf file

expectationResultHandler: function (passed, assertion) {
    if (!passed) {
        console.log(`Error Occurred, pausing script for debugging`);
        debugger;
    }
},

Note :

We need to run the script in Node.js debugging mode for this to work. We use Visual Studio Code editor which has a built-in JavaScript Debug Terminal.

Relevant posts

Capture Real-Time Performance from UI using WDIO

The “wdio-performancetotal-service” plugin for WebdriverIO empowers you to capture real-time performance data from UI tests. By integrating this plugin, you can measure the response times of various UI procedures, identify potential bottlenecks, and make informed decisions to optimize and enhance overall performance. With detailed performance metrics at your disposal, you can improve the effectiveness of your UI testing efforts and deliver a high-performing application. The “wdio-performancetotal-service” plugin provides a valuable solution for ensuring optimal performance in your UI tests.

Dynamics 365 automation through RSAT

The Regression Suite Automation Tool (RSAT) significantly reduces the time and cost of user acceptance testing. It enables functional power users to record business tasks using the Finance and Operations task recorder and convert them into a suite of automated tests without the need to write source code.