I have a bunch of automated UI tests running with selenium and NUnit.
After every nunit test I want to check the browser for any JS errors that have occurred. If there are any JS errors the test that caused them should fail. I want this to run for all tests I write without having to copy the check into each test.
I also take screenshots on any failure.
[TearDown]
public void TearDown()
{
Assert.Fail("Some JS error occurred"); //This is to simulate a JS error assertion
if (TestContext.CurrentContext.Result.Status != TestStatus.Passed)
{
Driver.TakeScreenshot(TestContext.CurrentContext.Test.Name, "Failed");
}
}
If I fail an assertion inside teardown it will never execute the screenshot code (as the assert is an exception).
Is there a better way to fail the test here so that I can carry on with my logic?
Assert.Failand use flag instead. E.g.jsErrorsOccuredand use it like following:if (jsErrorsOccured || (TestContext.CurrentContext.Result.Status != TestStatus.Passed)) { ... }? - drets