diff --git a/package.json b/package.json index 22f9f01..a1f6cf3 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "@playwright/test": "1.61.0", "@types/bun": "^1.3.14", "endform": "^0.69.0", + "playwright-opentelemetry": "^0.8.1", "tsx": "^4.22.4" } } diff --git a/playwright.config.ts b/playwright.config.ts index 6fc4185..c5d05b5 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -1,4 +1,5 @@ import { defineConfig, devices } from "@playwright/test"; +import type { PlaywrightOpentelemetryUseOptions } from "playwright-opentelemetry/fixture"; /** * Read environment variables from file. @@ -11,10 +12,15 @@ import { defineConfig, devices } from "@playwright/test"; export const baseURL = process.env.BASE_URL || "https://endform-playwright-tutorial.vercel.app"; +const playwrightOtelEnabled = Boolean( + process.env.PLAYWRIGHT_TRACE_API_ENDPOINT || + process.env.OTEL_EXPORTER_OTLP_ENDPOINT, +); + /** * See https://playwright.dev/docs/test-configuration. */ -export default defineConfig({ +export default defineConfig({ testDir: "./tests", globalSetup: "./global-setup.ts", globalTeardown: "./global-teardown.ts", @@ -27,7 +33,12 @@ export default defineConfig({ /* Opt out of parallel tests on CI. */ workers: "50%", /* Reporter to use. See https://playwright.dev/docs/test-reporters */ - reporter: [["html", { open: "never" }]], + reporter: [ + ["html", { open: "never" }], + ...(playwrightOtelEnabled + ? [["playwright-opentelemetry/reporter"] as const] + : []), + ], /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ use: { /* Base URL to use in actions like `await page.goto('/')`. */ @@ -36,7 +47,15 @@ export default defineConfig({ // baseURL: "http://localhost:3000", /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */ - trace: "retain-on-failure", + trace: playwrightOtelEnabled ? "on" : "retain-on-failure", + playwrightOpentelemetry: playwrightOtelEnabled + ? { + trace: "on", + storeTraceZip: false, + propagateTraceHeaders: true, + serviceName: "playwright-tutorial-tests", + } + : undefined, }, /* Run your local dev server before starting the tests */ diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 24213df..9e43e64 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -96,6 +96,9 @@ importers: endform: specifier: ^0.69.0 version: 0.69.0 + playwright-opentelemetry: + specifier: ^0.8.1 + version: 0.8.1(@playwright/test@1.61.0) tsx: specifier: ^4.22.4 version: 4.22.4 @@ -1892,6 +1895,10 @@ packages: '@types/ws@8.18.1': resolution: {integrity: sha512-ThVF6DCVhA8kUGy+aazFQ4kXQ7E1Ty7A3ypFOe0IcJV8O/M511G99AW24irKrW56Wt44yG9+ij8FaqoBGkuBXg==} + '@zip.js/zip.js@2.8.26': + resolution: {integrity: sha512-RQ4h9F6DOiHxpdocUDrOl6xBM+yOtz+LkUol47AVWcfebGBDpZ7w7Xvz9PS24JgXvLGiXXzSAfdCdVy1tPlaFA==} + engines: {bun: '>=0.7.0', deno: '>=1.0.0', node: '>=18.0.0'} + aria-hidden@1.2.6: resolution: {integrity: sha512-ik3ZgC9dY/lYVVM++OISsaYDeg1tb0VtP5uL3ouh1koGOaUMDPpbFIei4JkFimWUFPn90sbMNMXQAIVOlnYKJA==} engines: {node: '>=10'} @@ -2343,6 +2350,11 @@ packages: engines: {node: '>=18'} hasBin: true + playwright-opentelemetry@0.8.1: + resolution: {integrity: sha512-VDZpyPP1uZGeM0xVe3PVSu3rrTWy5OZjoBSKeHFH/PWtj49UM7b3M1U62vUga/uz7MyM/rFYsAanxlbnZP7yIw==} + peerDependencies: + '@playwright/test': '*' + playwright@1.61.0: resolution: {integrity: sha512-Z+7BeeqQPRRzklHsVFP4KTGIyMxKUmfeRA4WisM6G3/XW6nwGeX6fX9qYaDa+CiUqpOkb2f6X3nar05R3kSuJQ==} engines: {node: '>=18'} @@ -3998,7 +4010,7 @@ snapshots: '@types/better-sqlite3@7.6.13': dependencies: - '@types/node': 25.0.3 + '@types/node': 24.13.2 optional: true '@types/bun@1.3.14': @@ -4025,6 +4037,8 @@ snapshots: dependencies: '@types/node': 25.0.3 + '@zip.js/zip.js@2.8.26': {} + aria-hidden@1.2.6: dependencies: tslib: 2.8.1 @@ -4064,7 +4078,7 @@ snapshots: bun-types@1.3.14: dependencies: - '@types/node': 25.0.3 + '@types/node': 24.13.2 caniuse-lite@1.0.30001760: {} @@ -4450,6 +4464,11 @@ snapshots: playwright-core@1.61.0: {} + playwright-opentelemetry@0.8.1(@playwright/test@1.61.0): + dependencies: + '@playwright/test': 1.61.0 + '@zip.js/zip.js': 2.8.26 + playwright@1.61.0: dependencies: playwright-core: 1.61.0 diff --git a/tests/test.ts b/tests/test.ts index e46894e..dc57a06 100644 --- a/tests/test.ts +++ b/tests/test.ts @@ -1,7 +1,15 @@ import { test as base } from "@playwright/test"; +import { test as playwrightOpentelemetryTest } from "playwright-opentelemetry/fixture"; import { installFaultsForTest } from "./support/faults"; -export const test = base.extend({ +const playwrightOtelEnabled = Boolean( + process.env.PLAYWRIGHT_TRACE_API_ENDPOINT || + process.env.OTEL_EXPORTER_OTLP_ENDPOINT, +); + +const testBase = playwrightOtelEnabled ? playwrightOpentelemetryTest.extend(base) : base; + +export const test = testBase.extend({ page: async ({ page }, use, testInfo) => { await installFaultsForTest(page, testInfo);