4

I'm using Protractor with Chai as Promised in order to create a javascript-based testing tool and I'm getting the error

AssertionError: expected 'http://localhost:8888/test/homepage.php' to equal 'http://localhost:8888/test/my_homepage.php'

while I'm checking the url with this step definition:

this.Then(/^The url of the page should be "([^"]*)"$/, function(myUrl, callback){
    expect(browser.getCurrentUrl()).to.eventually.equal(myUrl);

    callback();
  });

I would like to catch this error in order to use a different callback function, how can I do that? I've tried to use a try-catch block but it doesn't seem to work. I can't even understand if AssertionErrors are generated by Protractor, can you gently give me an explaination about this?

Thank you in advance

Ema.jar
  • 2,370
  • 1
  • 33
  • 43
  • Would `notify` help here: `expect(browser.getCurrentUrl()).to.eventually.equal(myUrl).notify(yourcallback)`? – alecxe Jan 13 '16 at 22:54

1 Answers1

2

I couldn't find anything which could catch errors from expect and do something else. If @alecxe's suggestion from comments work, that should be your answer otherwise why not just do

browser.getCurrentUrl().then(function(url) {
    if(url === myUrl) {
       callback();
    } else {
       callback('something went wrong'); 
    }
});

or would this not work?

try {
  expect(browser.getCurrentUrl()).to.eventually.equal(myUrl);
  callback();
} catch(e) {
  callback('something went wrong ')); 
}
nilesh
  • 14,131
  • 7
  • 65
  • 79
  • I don't know why but callback.fail() is not a recognized as function. – Ema.jar Jan 15 '16 at 08:32
  • 1
    Looks like they removed `callback.fail()` in 0.7.0. Check [here](https://github.com/cucumber/cucumber-js/blob/eaedca85928f88b20791bf02d61250b3baad88d5/CHANGELOG.md#v070). You might want to just do, `callback(new Error('something went wrong'));` – nilesh Jan 15 '16 at 19:00
  • The error now is -> SyntaxError: Unexpected token ILLEGAL :( – Ema.jar Jan 16 '16 at 11:38
  • Can you try just putting a string instead of an error object? I updated the answer – nilesh Jan 17 '16 at 20:40
  • Same result :( Does exist a documentation where we can find the parameters for a callback? – Ema.jar Jan 18 '16 at 08:13
  • In [README](https://github.com/cucumber/cucumber-js#step-definitions) there are bunch of references to `callback(new Error(''));` – nilesh Jan 19 '16 at 20:03