Jenninha
Jenninha

Reputation: 1377

Protractor, Cucumber and chai as promised: When chai assertion fails

I am using protractor to run my cucumber tests. Inside my test I have the following assertion:

label.getText().then(
                function(labelText){
                    labelText = labelText.trim();
                    console.log('label text: ' + labelText);
                    chai.expect(labelText).to.equal(arg1);
                    callback();
                },
                function() {
                    callback.fail('Could not get page label text');
                });

When the Assertion is correct there is no problem. However when my labelText is different from arg1 I would like to still keep running it but I don't know how to add the exception or a fail callback in that. At the moment my application just exits. I know that is because I am not using a fail callback (I would like to know where I should have it).

I am also not sure if I should put the callback(); where it is now.

I am looking for solutions online and all I can find are examples using Mocha. I am not using Mocha or Jasmine. I am just using Cucumber framework with protractor. Since Cucumberjs does not have an assertion library, I added chai-as-promised for that. Thanks!

Upvotes: 2

Views: 1707

Answers (2)

Priyanshu
Priyanshu

Reputation: 3058

If you have chai-as-promised then you can assert async code like this:

this.When(/^I assert async code$/, function(callback) {
    expect(asyncMethod()).to.eventually.equal(true).and.notify(callback);
});

Upvotes: 0

Nathan Thompson
Nathan Thompson

Reputation: 2384

Cucumber.js seems to have issues when expect() calls fail in a callback. Since you have chai-as-promised installed, try doing this:

var labelText = label.getText().then(
  function(labelText){
    labelText = labelText.trim();
    console.log('label text: ' + labelText);
    return labelText;
  });
chai.expect(labelText).to.eventually.equal(arg1).then(callback);

I got this workaround from this comment and it worked well for me.

Upvotes: 2

Related Questions