---
title: Usage in a Gauge Project
slug: suite-documentation/usage-in-a-gauge-project
docTags: 
createdAt: 2026-07-14T10:22:09.545Z
---

In Gauge, the trace lifecycle can be managed with @BeforeScenario/@AfterScenario hooks depending on the project structure. Scenario-level tracing produces smaller and easier-to-read files for failure analysis.

```java
import com.microsoft.playwright.*;
import com.thoughtworks.gauge.AfterScenario;
import com.thoughtworks.gauge.BeforeScenario;
import com.thoughtworks.gauge.ExecutionContext;

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

public class GaugePlaywrightHooks {

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

  @BeforeScenario
  public void beforeScenario(ExecutionContext executionContext) 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();
  }

  @AfterScenario
  public void afterScenario(ExecutionContext executionContext) {
    String scenarioName = executionContext
        .getCurrentScenario()
        .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;
  }
}
```

