---
title: Usage in a Cucumber Project
slug: suite-documentation/usage-in-a-cucumber-project
docTags: 
createdAt: 2026-07-14T10:20:42.913Z
---

In Cucumber, tracing can start in the @Before hook and stop in the @After hook. Special characters should be sanitized when the scenario name is converted into a file name.

```java
import com.microsoft.playwright.*;
import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java.Scenario;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class PlaywrightHooks {

  private Playwright playwright;
  private Browser browser;
  private BrowserContext context;
  private Page page;

  @Before
  public void beforeScenario(Scenario scenario) throws Exception {
    Files.createDirectories(Paths.get("test-results", "traces"));

    playwright = Playwright.create();
    browser = playwright.chromium().launch(
        new BrowserType.LaunchOptions().setHeadless(true)
    );
    context = browser.newContext();

    context.tracing().start(new Tracing.StartOptions()
        .setScreenshots(true)
        .setSnapshots(true)
        .setSources(true));

    page = context.newPage();
  }

  @After
  public void afterScenario(Scenario scenario) {
    String scenarioName = scenario.getName();

    Path tracePath = Paths.get(
        "test-results",
        "traces",
        scenarioName + ".zip"
    );

    try {
      if (context != null) {
        context.tracing().stop(new Tracing.StopOptions()
            .setPath(tracePath));
      }
    } finally {
      if (context != null) context.close();
      if (browser != null) browser.close();
      if (playwright != null) playwright.close();
    }
  }

  public Page getPage() {
    return page;
  }
}
```

