0

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

shicky
  • 2,076
  • 5
  • 30
  • 44
  • I think [the answer to this question](https://stackoverflow.com/questions/42648956/what-is-faster-try-catch-vs-promise) could give some insight on what you want. – Rudreshuar Dec 02 '19 at 00:57

1 Answers1

-1

Hope the below answer helps you

await doThing.webBrandLink.click();

var err = \\locator for your error message that is shown in UI after the click action

expect(err.isDisplayed()).toBe(true);
Madhan Raj
  • 1,404
  • 1
  • 7
  • 13
  • Sorry I don't think I've explained the question very well, this is a Selenium exception that is thrown for example because you're trying to catch an element that doesn't exist on the page. This is not a custom application error. – shicky Dec 01 '19 at 11:42