> ## Documentation Index
> Fetch the complete documentation index at: https://docs.trunk.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Playwright

> A guide for generating Trunk-compatible test reports for Playwright

You can automatically [detect and manage flaky tests](../../detection/) in your Playwright projects by integrating with Trunk. This document explains how to configure Playwright to output JUnit XML reports that can be uploaded to Trunk for analysis.

## Setup steps

Work through the steps below in order. Once you've finished the last one, you'll be ready to move on to [configure uploads in CI](../ci-providers/).

**1. [Generate a compatible test report](#generating-reports)**

**2. [Configure the report file path or glob](#report-file-path)**

**3. [Test uploads locally](#try-it-locally)**

## Generating Reports

Playwright has multiple built-in reporters, including JUnit XML which Trunk can ingest. To get XML reports, add the following to your Playwright config:

```typescript title="playwright.config.ts" theme={"system"}
import { defineConfig } from '@playwright/test';

export default defineConfig({
  reporter: [
    ['junit', { outputFile: 'junit.xml' }]
  ],
});
```

Alternatively, you can specify reporting behavior inline in your CI:

```sh theme={"system"}
npx playwright test --reporter=junit
```

### Report File Path

You can specify the report's output location with the `PLAYWRIGHT_JUNIT_OUTPUT_FILE` environment variable:

```sh theme={"system"}
export PLAYWRIGHT_JUNIT_OUTPUT_FILE=junit.xml
```

You can also specify the report's location in your `playwright.config.ts` file:

```typescript theme={"system"}
export default defineConfig({
  reporter: [
    ['junit', { outputFile: 'junit.xml' }]
  ],
});
```

### Include Retries

If you use Playwright's `retries` option, set `includeRetries: true` so the report keeps each failed attempt, not only the final result:

```typescript title="playwright.config.ts" theme={"system"}
export default defineConfig({
  retries: 2,
  reporter: [
    ['junit', { outputFile: 'junit.xml', includeRetries: true }]
  ],
});
```

You can also set `PLAYWRIGHT_JUNIT_INCLUDE_RETRIES=1` instead.

> [!NOTE]
>
> A test that fails and then passes on retry will now be detected by the [pass-on-retry monitor](https://docs.trunk.io/flaky-tests/detection/pass-on-retry-monitor.md), so expect more flaky tests. This is the recommended setup, because the results then show how the test actually behaves.

## Try It Locally

### The Validate Command

You can validate your test reports using the [Trunk Analytics CLI](https://docs.trunk.io/flaky-tests/reference/cli-reference.md). If you don't have it installed already, you can install and run the `validate` command like this:

```bash Linux (x64) theme={"system"}
SKU="trunk-analytics-cli-x86_64-unknown-linux.tar.gz"
VERSION=$(curl -fsSL https://trunk.io/releases/analytics-cli/prod/channel.json | cut -d'"' -f4)
curl -fL --retry 3 \
  "https://trunk.io/releases/analytics-cli/prod/${VERSION}/${SKU}" \
  | tar -xz

chmod +x trunk-analytics-cli
./trunk-analytics-cli validate --junit-paths "./junit.xml"
```

```bash Linux (arm64) theme={"system"}
SKU="trunk-analytics-cli-aarch64-unknown-linux.tar.gz"
VERSION=$(curl -fsSL https://trunk.io/releases/analytics-cli/prod/channel.json | cut -d'"' -f4)
curl -fL --retry 3 \
  "https://trunk.io/releases/analytics-cli/prod/${VERSION}/${SKU}" \
  | tar -xz

chmod +x trunk-analytics-cli
./trunk-analytics-cli validate --junit-paths "./junit.xml"
```

```bash macOS (arm64) theme={"system"}
SKU="trunk-analytics-cli-aarch64-apple-darwin.tar.gz"
VERSION=$(curl -fsSL https://trunk.io/releases/analytics-cli/prod/channel.json | cut -d'"' -f4)
curl -fL --retry 3 \
  "https://trunk.io/releases/analytics-cli/prod/${VERSION}/${SKU}" \
  | tar -xz

chmod +x trunk-analytics-cli
./trunk-analytics-cli validate --junit-paths "./junit.xml"
```

```bash macOS (x64) theme={"system"}
SKU="trunk-analytics-cli-x86_64-apple-darwin.tar.gz"
VERSION=$(curl -fsSL https://trunk.io/releases/analytics-cli/prod/channel.json | cut -d'"' -f4)
curl -fL --retry 3 \
  "https://trunk.io/releases/analytics-cli/prod/${VERSION}/${SKU}" \
  | tar -xz

chmod +x trunk-analytics-cli
./trunk-analytics-cli validate --junit-paths "./junit.xml"
```

**This will not upload anything to Trunk**. To improve detection accuracy, you should **address all errors and warnings** before proceeding to the next steps.

### Test Upload

Before modifying your CI jobs to automatically upload test results to Trunk, try uploading a single test run manually.

You make an upload to Trunk using the following command:

```sh theme={"system"}
./trunk-analytics-cli upload --junit-paths "./junit.xml" \
    --org-url-slug <TRUNK_ORG_URL_SLUG> \
    --test-collection-id <TRUNK_TEST_COLLECTION_ID> \
    --token <TRUNK_ORG_TOKEN>
```

You can find your Trunk organization slug and token in the settings or by following these [instructions](https://docs.trunk.io/flaky-tests/get-started/ci-providers/otherci.md#trunk-organization-slug-and-token). The collection ID identifies the [test collection](https://docs.trunk.io/flaky-tests/test-collections.md) the results belong to, and you copy it from that collection in the Trunk app. After your upload, you can verify that Trunk has received and processed it successfully in the **Uploads** tab. Warnings will be displayed if the report has issues.

![](https://mintcdn.com/trunk-4cab4936/FEDBBs5EPDEY6kRF/assets/_shared/data-uploads-light.png?fit=max\&auto=format\&n=FEDBBs5EPDEY6kRF\&q=85\&s=6f68ed972b84c450784f8e1bd6840dc5)

## Next Step

Configure your CI to upload test runs to Trunk. Find the guides for your CI framework below:

> [card] [Azure DevOps Pipelines](https://docs.trunk.io/flaky-tests/get-started/ci-providers/azure-devops-pipelines.md)

> [card] [BitBucket Pipelines](https://docs.trunk.io/flaky-tests/get-started/ci-providers/bitbucket-pipelines.md)

> [card] [BuildKite](https://docs.trunk.io/flaky-tests/get-started/ci-providers/buildkite.md)

> [card] [CircleCI](https://docs.trunk.io/flaky-tests/get-started/ci-providers/circleci.md)

> [card] [Drone CI](https://docs.trunk.io/flaky-tests/get-started/ci-providers/droneci.md)

> [card] [GitHub Actions](https://docs.trunk.io/flaky-tests/get-started/ci-providers/github-actions.md)

> [card] [GitLab](https://docs.trunk.io/flaky-tests/get-started/ci-providers/gitlab.md)

> [card] [Jenkins](https://docs.trunk.io/flaky-tests/get-started/ci-providers/jenkins.md)

> [card] [Semaphore](https://docs.trunk.io/flaky-tests/get-started/ci-providers/semaphoreci.md)

> [card] TeamCity

> [card] [Travis CI](https://docs.trunk.io/flaky-tests/get-started/ci-providers/travisci.md)

> [card] [Other CI Providers](https://docs.trunk.io/flaky-tests/get-started/ci-providers/otherci.md)
