Testing Mavens

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

Thu May 30 2024

AC
Aswin Chembath
thumbnail

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.

Background

Your Quality Gatekeepers,

Partner with us today.