简体   繁体   中英

C# Selenium SpecFlow Handling failing tests and continuing

Can anyone tell me what is the correct method to handle a failing test in Selenium / c#? I have the below code, which works fine and fails when it should, but the Assert.Fail() ends the run and skips any subsequent tests. Is there a method of failing this specflow step and the overall job, but still executing subsequent steps?

[Then(@"I click the data input box and say hello")]
public void ThenIClickTheDataInputBox()
{
try
{
Driver.Browser.FindElement(By.Id("lst-ibs")).SendKeys("Hello - This is a   test!!");
}
catch(Exception e)
{
Console.WriteLine("Element 'lst-ib' not found on page");
Assert.Fail();
}
}

Assuming you mean that it stops executing subsequent steps and not subsequent tests . This is how a test, including a SpecFlow test, normally behaves: whenever a failure (exception) occurs in any step along the way, the test fails immediately, and the next test starts. (You can also have a BeforeScenario and AfterScenario hooks to initialize and cleanup things before and after the tests if you need to)

Note that unlike manual tests where you can use your judgement to decide whether it makes sense to continue the test or not, an automated test cannot.

If you still want the step not to fail the test, then you shouldn't put the Assert.Fail() in there. You can turn on some flag and in your last Then or AfterScenario check that flag and fail the test.

BTW, there's no much point in catching the exception, writing to the console and then using Assert.Fail() . If you do want to fail the step in case of an exception, you can eliminate the try / catch clauses completely and simply let the exception flow back. This will have the same effect as what you're doing, in terms of failing the step. Moreover, your code swallows all of the details of the exception (it's type, message, stack-trace, inner exceptions, etc.) which can be invaluable when you come to investigate failures. Letting the exception be thrown away without catching it, retains all of this information and shows it to you in Test Explorer or wherever you see your test results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM