Skip to content

Commit 2371fc4

Browse files
committed
Add playwright opentelemetry config (#2)
* Add playwright opentelemetry config * Fix fixture path
1 parent 1ac0605 commit 2371fc4

4 files changed

Lines changed: 53 additions & 6 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"@playwright/test": "1.61.0",
4747
"@types/bun": "^1.3.14",
4848
"endform": "^0.69.0",
49+
"playwright-opentelemetry": "^0.8.1",
4950
"tsx": "^4.22.4"
5051
}
5152
}

‎playwright.config.ts‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { defineConfig, devices } from "@playwright/test";
2+
import type { PlaywrightOpentelemetryUseOptions } from "playwright-opentelemetry/fixture";
23

34
/**
45
* Read environment variables from file.
@@ -11,10 +12,15 @@ import { defineConfig, devices } from "@playwright/test";
1112
export const baseURL =
1213
process.env.BASE_URL || "https://endform-playwright-tutorial.vercel.app";
1314

15+
const playwrightOtelEnabled = Boolean(
16+
process.env.PLAYWRIGHT_TRACE_API_ENDPOINT ||
17+
process.env.OTEL_EXPORTER_OTLP_ENDPOINT,
18+
);
19+
1420
/**
1521
* See https://playwright.dev/docs/test-configuration.
1622
*/
17-
export default defineConfig({
23+
export default defineConfig<PlaywrightOpentelemetryUseOptions>({
1824
testDir: "./tests",
1925
globalSetup: "./global-setup.ts",
2026
globalTeardown: "./global-teardown.ts",
@@ -27,7 +33,12 @@ export default defineConfig({
2733
/* Opt out of parallel tests on CI. */
2834
workers: "50%",
2935
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
30-
reporter: [["html", { open: "never" }]],
36+
reporter: [
37+
["html", { open: "never" }],
38+
...(playwrightOtelEnabled
39+
? [["playwright-opentelemetry/reporter"] as const]
40+
: []),
41+
],
3142
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
3243
use: {
3344
/* Base URL to use in actions like `await page.goto('/')`. */
@@ -36,7 +47,15 @@ export default defineConfig({
3647
// baseURL: "http://localhost:3000",
3748

3849
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
39-
trace: "retain-on-failure",
50+
trace: playwrightOtelEnabled ? "on" : "retain-on-failure",
51+
playwrightOpentelemetry: playwrightOtelEnabled
52+
? {
53+
trace: "on",
54+
storeTraceZip: false,
55+
propagateTraceHeaders: true,
56+
serviceName: "playwright-tutorial-tests",
57+
}
58+
: undefined,
4059
},
4160

4261
/* Run your local dev server before starting the tests */

‎pnpm-lock.yaml‎

Lines changed: 21 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/test.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import { test as base } from "@playwright/test";
2+
import { test as playwrightOpentelemetryTest } from "playwright-opentelemetry/fixture";
23
import { installFaultsForTest } from "./support/faults";
34

4-
export const test = base.extend({
5+
const playwrightOtelEnabled = Boolean(
6+
process.env.PLAYWRIGHT_TRACE_API_ENDPOINT ||
7+
process.env.OTEL_EXPORTER_OTLP_ENDPOINT,
8+
);
9+
10+
const testBase = playwrightOtelEnabled ? playwrightOpentelemetryTest.extend(base) : base;
11+
12+
export const test = testBase.extend({
513
page: async ({ page }, use, testInfo) => {
614
await installFaultsForTest(page, testInfo);
715

0 commit comments

Comments
 (0)