Nightwatch: How to signal test failure using browser.end()?

Created on 20 May 2016  路  9Comments  路  Source: nightwatchjs/nightwatch

Hey there,

I would love to see support for this. Sometimes, I want to prematurely end a test and output a FAIL signature to Jenkins. For example:

exports.command = function(callback) {
    'use strict';
    var jsdom = require('jsdom');
    var self = this;
    var url = this.globals.urls.dropWizard;

    jsdom.env({
        html: '<html><body></body></html>',
        scripts: ['https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js'],
        done: function pageCreated(err, window) {
            let $ = window.jQuery;

            $.ajax({
                url: url,
                method: 'GET',
                beforeSend: function (request, settings) {
                    console.log('Request URL: %s', settings.url);
                },
                success: function (response) {
                    if (typeof callback === 'function') {
                        callback.call(self, response);
                    }
                },
                error: function (jqXHR) {
                    console.log('ERROR! \n%s', JSON.stringify(jqXHR));
                    self.end(); // SIGNAL FAIL HERE
                }
            });
        }
    });

    // allows command to be chained
    return this;
};

Most helpful comment

For anyone else as confused as I was about where the assert.fail is being defined.. Nightwatch extends the Node.JS assert module. see http://nightwatchjs.org/api/#assertions

All 9 comments

what happens if you throw an exception instead of calling self.end()?

trying that now, will need some time but should be able to get back to you today :+1:

Hmm... when I throw an error, I still need to end the test using .end().

If I set the timeout value to something unachievable on our service (like 10ms) the test ends and outputs the message as expected, but then I get an unhandled exception:

Running:  Dashboard: Apply Filters
Request URL: 
http://myUrl?filter=geography:AR,BO,BR,CL,CO,EC,FK,GF,GY,PE,PY,SR,UY,VE&facet=geography&facet=os
TIMEOUT! Failed to respond in under 10ms

An error occurred while running the tests:
TypeError: Cannot read property 'split' of undefined
    at HttpRequest.<anonymous> (/home/che.fisher/Projects/gapui/node_modules/nightwatch/lib/index.js:341:31)
    at emitTwo (events.js:100:13)
    at HttpRequest.emit (events.js:185:7)
    at HttpRequest.<anonymous> (/home/che.fisher/Projects/gapui/node_modules/nightwatch/lib/index.js:378:15)
    at emitThree (events.js:110:13)
    at HttpRequest.emit (events.js:188:7)
    at IncomingMessage.<anonymous> (/home/che.fisher/Projects/gapui/node_modules/nightwatch/lib/http/request.js:157:16)
    at emitNone (events.js:85:20)
    at IncomingMessage.emit (events.js:179:7)
    at endReadableNT (_stream_readable.js:913:12)

The calling code looks like:

throw new self.globals.timeoutErr(self, timeout);

The error function:

function timeoutErr(browser, timeout) {
    console.error('TIMEOUT! Failed to respond in under %sms', timeout);
    browser.end();
}

Note: this all happens locally.

I would still lean towards extending the .end() function so that it can be supplied, say, an enum and string, so that I can end a test and output a fail signal by doing browser.end('timeout', 'Service took too long to respond') maybe

Any thoughts? I'm using an ugly workaround at the moment to prevent our Jenkins machines crashing (from hanging browsers) which doesn't output informative results.

I think this thread also discusses what you're after.

Current workaround is simply calling this function:

function timeoutErr(browser, timeout) {
    browser
        .assert.urlEquals('false','TIMEOUT! Gap failed to respond in under ' + timeout + 'ms')
        .end();
}

Which allows me to at least output meaningful error messages :+1: haha

This can also be achieved using something like:

'Demo test Google' : function (client) {
    client
      .url('http://google.com', function(result) {
        // second empty string argument is the expected value
        client.assert.fail(JSON.stringify(errorResponse), '', 'More details about the error');
      })
    ...
};

I'm closing this since for now I think this solves the problem.

For anyone else as confused as I was about where the assert.fail is being defined.. Nightwatch extends the Node.JS assert module. see http://nightwatchjs.org/api/#assertions

Was this page helpful?
0 / 5 - 0 ratings

Related issues

t00f picture t00f  路  3Comments

manjero picture manjero  路  4Comments

sgleonardoopitz picture sgleonardoopitz  路  3Comments

chaseconey picture chaseconey  路  4Comments

jvlaar picture jvlaar  路  4Comments