A test action is an executable step that prepares, exercises, or verifies an aspect of an application. It gives a driver a way to wrap an operation in shared concerns such as logging and timing, rather than repeating that logic in every driver method. section 15.4 section 15.9
A driver can create several actions, such as login and registration. Step definitions execute them; more complex tests can retain an action to check execution errors or expected results. Actions can use a shared base class, or a lambda-based implementation when a separate derived class is unnecessary. section 15.4
Listing 15.13 Base class for test actions
*** Support/TestAction.cs ***
public abstract class TestAction<TResult>(string actionName)
{
public string TestActionName => testActionName;
protected abstract Task<TResult> DoExecute(); #A
public async Task<TResult> Execute()
{
try
{
Console.WriteLine($"Executing {TestActionName}...");
var stopwatch = Stopwatch.StartNew();
var result = await DoExecute(); #B
Console.WriteLine(
$"{TestActionName} executed successfully in {stopwatch.Elapsed}.");
return result;
}
catch (Exception ex)
{
Console.WriteLine($"{TestActionName} failed: {ex.Message}");
throw;
}
}
}See how a base action wraps execution with logging, timing, and exception reporting while delegating the operation to DoExecute. section 15.4
