Nightwatch: Conditionally exit test early

Created on 18 Sep 2015  Â·  10Comments  Â·  Source: nightwatchjs/nightwatch

I am trying to set up my suite such that the result of getCssProperty() conditionally ends the test early without resulting in a test failure.

    client.getCssProperty(searchPage.elements.errorFour.selector, "display", function(result) {
      if(result != "none") {
        console.log("Bailing");
        client.end(); // Want the test to end here no more asserts will be cecked
      } else {
        searchPage.assert.elementPresent("@searchResults", "Asserting that example results load");
      }
    });

I get the Bailing message in the output, but further asserts and waits are still ran and fail. Is there anyway to stop this from happening?

Thanks

Most helpful comment

@beatfactor that will end the test early but without outputting an error?

I want to end it early AND ensure it errors :) would that be a simple change to the function you've provided?

All 10 comments

There isn't a built in method to do this. You can built a custom command though. Try this (untested):

function Quit() {
}

Quit.prototype.command = function(callback) {
  this.client.queue.reset();
  this.client.queue.empty();
  this.api.end(function() {
    this.finished();
  }.bind(this));
  this.client.queue.run();
};
module.exports = Quit;

Save this in a file called whatever you like your command to be (say terminate.js) and place it in your custom commands folder.

Probably a little late to this. Not sure if this is the "correct" way to do exit early, but for our tests we simply throw an error and that cleanly terminates everything. IIRC, doing just client.end() causes the testing queue to hang and doing a process.exit() causes you to have a zombie selenium process (if you have nightwatch manage the selenium process through start_process).

@akoutmos i'm stumped on how to access whether there were errors in the after or afterEach hooks. is this where where you were throwing your error?

Running a test incorporating the terminate.js custom command results in ReferenceError: self is not defined being thrown.

Defining self as this shows finished to not be a function.

@beatfactor that will end the test early but without outputting an error?

I want to end it early AND ensure it errors :) would that be a simple change to the function you've provided?

Just in case people are still interested/looking for a way to signal test failure from inside a test (or custom command) - check out the answer on this thread

Thanks

Smiles,

Sandeep

On Mon, May 29, 2017 at 4:27 PM, Che Fisher notifications@github.com
wrote:

Just in case people are still interested/looking for a way to signal test
failure from inside a test (or custom command) - check out the answer on
this https://github.com/nightwatchjs/nightwatch/issues/986 thread

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/nightwatchjs/nightwatch/issues/646#issuecomment-304674047,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABV2IVEQCvV_YE8ZuCP3W4GZTGquRZOAks5r-tXigaJpZM4GAKnl
.

function Quit() {
}

Quit.prototype.command = function(callback) {
  this.client.queue.reset();
  this.client.queue.empty();
  this.api.end(function() {
    this.finished();
  }.bind(this));
  this.client.queue.run();
};
module.exports = Quit;

@beatfactor , i am trying to use this command, but it is failing in api.js function addCommand with an error : TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them

@beatfactor , It fails for me and gives an Type error this.finished is not a function

@beatfactor The suggested code works for me ... almost. It closes the browser and no errors are thrown. However, it doesn't terminate my session (I'm running 'node nightwatch tests/mytest.js' from the command line). Is there a way to get the code to also terminate the session?

Was this page helpful?
0 / 5 - 0 ratings