Код: Выделить всё
namespace Hooks.SharedHooks
{
[Binding]
public class RetryHook
{
private int _retryCount;
private const int DefaultMaxRetries = 2;
private static readonly Regex RetryTag = new Regex(@"^Retry");
private static readonly Regex RetryTagNumbereRegex = new Regex(@"^Retry\(\d+)\)$");
[BeforeScenario("Retry","Retry*")]
public void BeforeScenario()
{
_retryCount = 0;
}
[AfterScenario("Retry", "Retry*", Order = 0)]
public void AfterScenario(ScenarioContext scenarioContext, ITestRunnerManager testRunnerManager)
{
var maxRetries = GetMaxRetriesFromScenario(scenarioContext) ?? DefaultMaxRetries;
while (_retryCount < maxRetries && scenarioContext.TestError != null)
{
_retryCount++;
Console.WriteLine($"Attempting scenario '{scenarioContext.ScenarioInfo.Title}' try {_retryCount} of {maxRetries}");
ScenarioExecutionRetryAsync(scenarioContext, testRunnerManager);
}
}
private int? GetMaxRetriesFromScenario(ScenarioContext scenarioContext)
{
foreach (var tag in scenarioContext.ScenarioInfo.Tags)
{
var match = RetryTagNumbereRegex.Match(tag);
if (match.Success && int.TryParse(match.Groups[1].Value, out int retries))
{
return retries;
}
}
return null;
}
private void ScenarioExecutionRetry(ScenarioContext scenarioContext,ITestRunnerManager manager)
{
// Logic to re-run the failed scenario
}
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... kin-syntax