Code reuse applies existing automation logic in multiple business flows instead of repeating low-level interactions. Reusable tasks can combine navigation, form entry, and other tasks into readable, composable operations. section 12.9.2
Effective reuse in test automation follows several practical patterns:
- Hide repeated navigation behind a task such as toTheLoginPage(), so tests express intent rather than page mechanics. section 12.9.2
- Combine navigation, credential entry, and submission into a Login.usingCredentials() task that higher-level tests can call directly. section 12.9.2
- Separate default test data from scenario-specific overrides, keeping feature files readable while supporting negative cases. section 15.1.6
- Use dynamic task arguments when actor-specific data comes from a notepad, and merge partial overrides only where a scenario requires them. section 15.1.6
Reusable sign-up flow
A SignUp task can accept traveler details and reuse the same registration sequence across scenarios. A normal scenario uses generated actor data, while a duplicate-email scenario overrides only the email with smiths@example.org, preserving the shared flow and making the business rule prominent. section 15.1.6
public class Login {
public static Performable usingCredentials(String username,
String password) {
return Task.where("{0} logs in as " + username,
Navigate.toTheLoginPage(),
SendKeys.of(username).into("#email"),
SendKeys.of(password).into("#password"),
Click.on("#login-button")
);
}
}
Shows a reusable login task composed from navigation, credential entry, and button interaction. section 12.9.2
import { Actor, Question } from '@serenity-js/core'
import { When, DataTable } from '@cucumber/cucumber'
When('{actor} tries to sign up using:', (actor: Actor, data: DataTable) =>
actor.attemptsTo(
SignUp.using(
Question.fromObject<TravelerDetails>(
notes<TravelerNotes>().get('travelerDetails'),
data.rowsHash() as Partial<TravelerDetails>,
)
)
));
Shows how a scenario-specific partial data set can be merged with an actor's default traveler details. section 15.1.6





