BDD automation has two concerns: turning specification statements into testable actions, and exercising the application to perform them. Separating them creates a layered architecture. section 15.2
Keep step definitions as the interface between the specification and automation, where testing decisions are made. Move application interactions into a driver instead of having one step definition call another. section 15.2
Authentication API driver
A driver can send a login request to the application API. Step definitions call it for valid or incorrect passwords and check the resulting behavior. section 15.2
Listing 15.6 Step definition class update to use the driver
*** StepDefinitions/AuthenticationStepDefinitions.cs ***
public class AuthenticationStepDefinitions(
AuthenticationApiDriver authApiDriver)
{
private HttpResponseMessage? loginApiResponse;
[Given("the customer is authenticated")]
public async Task GivenTheCustomerIsAuthenticated()
{
var response = await authApiDriver.PerformLogin( #A
DomainDefaults.CustomerName, DomainDefaults.Password);
Assert.AreEqual(HttpStatusCode.OK, #B
response.StatusCode);
}
[When("the customer attempts to log in with a wrong password")]
public async Task WhenTheCustomerAttemptsToLogInWithAWrongPassword()
{
loginApiResponse = await authApiDriver #A
.PerformLogin(DomainDefaults.CustomerName,
DomainDefaults.WrongPassword);
}
[Then("the login should fail with {string}")]
public async Task ThenTheLoginShouldFailWith(string expectedMessage)
{
Assert.IsNotNull(loginApiResponse);
Assert.AreNotEqual(HttpStatusCode.OK, #B
loginApiResponse.StatusCode);
var errorMessage = await loginApiResponse.Content.ReadAsStringAsync();
StringAssert.Contains(errorMessage, expectedMessage,
"Login should fail with the right error message");
}
}See step definitions delegate login requests to an authentication API driver. section 15.2
