Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Try Catch in CodeceptJS

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.

like image 628
Dingredient Avatar asked Aug 03 '17 18:08

Dingredient


1 Answers

Try-catch should be performed on a promise chain. I think you can get it this way:

I.selectOption(this.SELECT, this.OPTION).catch(() => I.say(''));
like image 123
Davert Avatar answered Sep 25 '22 18:09

Davert