This recently came up in some code reviews in our team, a test is written, asserting on an expected exception. This is correct, however, the method used was a try/catch, which no one thinks is the most performant way to go. This begs the question of what is the most performant way to assert on an exception being thrown? I'm aware the try-catch needs to be modified to handle the case of the exception not being thrown and making sure the test fails.
try/catch example:
it('should be unable to click', async () => {
await expect(doThing.blockingLayer.isPresent).toBeTruthy();
await browser.switchTo().defaultContent();
await browser.waitForAngularEnabled(false);
try {
await doThing.webBrandLink.click();
}
catch(err){
console.log(err);
await expect(err.message).toContain('is not clickable');
}
});
I found a similar question here and I'm curious if this is the best solution? It's written moreso to handle an exception rather than asserting the correct one is thrown but could be adapted - How to handle element not found exception in Protractor