How to Try Catch in CodeceptJS

Viewed 3076

I'm doing this in my page object:

try{
    I.selectOption(this.SELECT, this.OPTION);
}
catch(error){
    I.say('Option missing, but thats sometimes expected ' + error);
}

But it still fails the test when the locator doesn't match an option element.

I want to catch and continue the test, without failing.

UPDATE:

It looks like it depends on what's in the try block.

If I put an assertion there, like I.see('something'); Then the catch block is not skipped. But non-assertions in the try block, like I.selectOption('something') throw errors which are not caught by the catch.

2 Answers
I.selectOption(this.SELECT, this.OPTION)
.then(() => I.say('try block'))
.catch(() => I.say('catch block'));
Related