A comprehensive demonstration of using Serenity BDD with Playwright to test the TodoMVC React application.
This project showcases the recommended three-layer architecture for Serenity Playwright tests:
- Tests - JUnit 5 test classes that orchestrate steps and make assertions
- Step Libraries -
@Stepannotated methods that provide business-readable actions - Page Objects - Encapsulate page interactions and locator strategies
src/test/java/todomvc/
├── pages/
│ └── TodoMvcPage.java # Page Object with locators and interactions
├── steps/
│ └── TodoSteps.java # Step library with @Step annotated methods
└── tests/
├── WhenAddingTodosTest.java
├── WhenCompletingTodosTest.java
├── WhenDeletingTodosTest.java
├── WhenEditingTodosTest.java
└── WhenFilteringTodosTest.java
Step libraries return data, tests make assertions:
// In TodoSteps.java - returns data
@Step("Check if todo '{0}' is completed")
public boolean todoIsCompleted(String todoText) {
return todoMvcPage.isCompleted(todoText);
}
// In test class - makes assertion
@Test
void shouldMarkTodoAsCompleted() {
todo.openApplication();
todo.addTodo("Buy milk");
todo.completeTodo("Buy milk");
assertThat(todo.todoIsCompleted("Buy milk")).isTrue();
}The Page Object is a field in the step library, initialized via setPage():
public class TodoSteps {
private TodoMvcPage todoMvcPage;
public void setPage(Page page) {
this.todoMvcPage = new TodoMvcPage(page);
}
// Steps use the page object...
}Register Playwright pages for automatic screenshot capture:
@BeforeEach
void setUp() {
page = browser.newPage();
PlaywrightSerenity.registerPage(page);
todo.setPage(page);
}
@AfterEach
void tearDown() {
PlaywrightSerenity.unregisterPage(page);
page.close();
}Run all tests and generate Serenity reports:
mvn clean verifyRun a specific test class:
mvn clean verify -Dtest=WhenAddingTodosTestAfter running tests, open the Serenity report:
open target/site/serenity/index.html- Java 17 or higher
- Maven 3.8+
- Playwright browsers (installed automatically on first run)
| Dependency | Version |
|---|---|
| Serenity BDD | 5.1.0 |
| Playwright | 1.57.0 |
| JUnit 5 | 6.0.1 |
| AssertJ | 3.27.3 |