Usage in a Gauge Project
1 min
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.
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;
}
}