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

The Chatbot Testing

Imagine visiting a new website. A lot of things pop up on the homepage. One such pop-up is a virtual chat box with someone from the company that can answer all your preliminary questions and queries and redirects you to the related page on the website or the concerned people in the company.

Tale of Exhausting API Regression to Efficient Automation with Postman

Performing API regression testing manually and not getting the desired result is a meticulous task and it also requires to re-run the APIs multiple times just to make sure the outcome you are getting is still, correct.