Have you ever faced a situation where you have 50 test cases in a single JS file and say suppose you got a failure at the 5th test case, and surprisingly the rest 45 Test cases were skipped, without execution?

Wouldn’t it have been nice if we could have executed all the test cases in one go despite some of them getting failed? Fortunately, I found a very simple solution to avoid this problem.

First, let’s try to reproduce the problem. Let’s create a test suite with three test cases. We would be performing three searches on google with different keywords.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module.exports = {
    before: function (browser) {
        //Declaring Global Timeout
        browser.globals.waitForConditionTimeout = 7000;
    },

    beforeEach: function (browser) {
        //Opening Google homepage and Clicking on I agree button for cookie consent
        browser
            .url('https://www.google.com')
            .waitForElementVisible('iframe[src*="consent.google.com"]')
            .frame(0)
            .click('#introAgreeButton')
    },

    'Google Search for Hello World': function (browser) {
        browser
            .waitForElementVisible('input[name="q"]')
            .setValue('input[name="q"]', 'hello world')
            .keys(browser.Keys.ENTER)
            .assert.urlContains('q=hello+world')
    },

    'Google Search for Fruits': function (browser) {
        browser
            .waitForElementVisible('input[name="q"]')
            .setValue('input[name="q"]', 'Fruits')
            .keys(browser.Keys.ENTER)
            .assert.urlContains('q=Fruits')
    },

    'Google Search for Food': function (browser) {
        browser
            .waitForElementVisible('input[name="q"]')
            .setValue('input[name="q"]', 'Food')
            .keys(browser.Keys.ENTER)
            .assert.urlContains('q=Food')
    },

    afterEach: function (browser) {
        browser.end();
    }
}

Nightwatch JS google cookie consent and search

Now we will deliberately fail the second Test case Google Search for Fruits by commenting out the line:

1
.keys(browser.Keys.ENTER)

After the changes, we should have something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module.exports = {
    before: function (browser) {
        //Declaring Global Timeout
        browser.globals.waitForConditionTimeout = 7000;
    },

    beforeEach: function (browser) {
        //Opening Google homepage and Clicking on I agree button for cookie consent
        browser
            .url('https://www.google.com')
            .waitForElementVisible('iframe[src*="consent.google.com"]')
            .frame(0)
            .click('#introAgreeButton')
    },

    'Google Search for Hello World': function (browser) {
        browser
            .waitForElementVisible('input[name="q"]')
            .setValue('input[name="q"]', 'hello world')
            .keys(browser.Keys.ENTER)
            .assert.urlContains('q=hello+world')
    },

    'Google Search for Fruits': function (browser) {
        browser
            .waitForElementVisible('input[name="q"]')
            .setValue('input[name="q"]', 'Fruits')
            //.keys(browser.Keys.ENTER)
            .assert.urlContains('q=Fruits')
    },

    'Google Search for Food': function (browser) {
        browser
            .waitForElementVisible('input[name="q"]')
            .setValue('input[name="q"]', 'Food')
            .keys(browser.Keys.ENTER)
            .assert.urlContains('q=Food')
    },

    afterEach: function (browser) {
        browser.end();
    }
}

Nightwatch JS fail test case

Now, lets execute the Test by using the command:

1
npx nightwatch tests/TC002_GoogleSearch.js

Afer execution the console should like this:

Nightwatch JS tests skipped terminal output

As you can see since the second test case failed, the third test (Google Search for Food) skipped without execution. Now, to prevent this we will be adding skip_testcases_on_fail as false in our nightwatch.conf.js file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
module.exports = {
    src_folders: ["tests"],
    skip_testcases_on_fail: false,
    test_settings: {
        default: {
            desiredCapabilities: {
                browserName: 'chrome'
            },
            webdriver: {
                start_process: true,
                port: 4444,
                server_path: require('chromedriver').path,
            }
        },

        test_workers: {
            enabled: true,
            workers: 'auto'
        },

        safari: {
            desiredCapabilities: {
                browserName: 'safari',
                alwaysMatch: {
                    acceptInsecureCerts: false
                }
            },
            webdriver: {
                port: 4445,
                start_process: true,
                server_path: '/usr/bin/safaridriver'
            }
        },

        firefox: {
            desiredCapabilities: {
                browserName: 'firefox'
            },
            webdriver: {
                start_process: true,
                port: 4446,
                server_path: require('geckodriver').path
            }
        }
    }
}

nightwatch conf js with skip test case as false

Let’s execute the same test again and see what happens.

Nightwatch JS test execution continues even after failure

As you can see, even though the second test case (Google Search for Fruits) failed, the third test case (Google Search for Food) was executed successfully and not skipped.

Do check out 🙂

Github: https://github.com/alapanme/NightwatchJS
All Nightwatch JS Articles: https://testersdock.com/nightwatch-js-tutorial/