Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(test): use relative path in error-context location (#39676)

Pavel Feldmana6a2ba7d3d21
diff --git a/packages/playwright/src/errorContext.ts b/packages/playwright/src/errorContext.ts
index a7655f4ce0..a5ab393a82 100644
--- a/packages/playwright/src/errorContext.ts
+++ b/packages/playwright/src/errorContext.ts
@@ -19,6 +19,8 @@ import path from 'path';
import { parseErrorStack, stripAnsiEscapes } from 'playwright-core/lib/utils';
+import { relativeFilePath } from './util';
+
import type { TestInfoError } from '../types/test';
const fixTestInstructions = `# Instructions
@@ -46,7 +48,7 @@ export function buildErrorContext(options: {
'# Test info',
'',
`- Name: ${titlePath.join(' >> ')}`,
- `- Location: ${location.file}:${location.line}:${location.column}`,
+ `- Location: ${relativeFilePath(location.file)}:${location.line}:${location.column}`,
];
if (meaningfulErrors.length) {
diff --git a/tests/playwright-test/playwright.artifacts.spec.ts b/tests/playwright-test/playwright.artifacts.spec.ts
index c9dbecf180..1ea81bdca9 100644
--- a/tests/playwright-test/playwright.artifacts.spec.ts
+++ b/tests/playwright-test/playwright.artifacts.spec.ts
@@ -572,6 +572,27 @@ test('should work with trace: retain-on-failure-and-retries', async ({ runInline
]);
});
+test('error-context should use relative path in location', async ({ runInlineTest }, testInfo) => {
+ const result = await runInlineTest({
+ 'a.spec.ts': `
+ import { test, expect } from '@playwright/test';
+ test('fail', async ({ page }) => {
+ expect(1).toBe(2);
+ });
+ `,
+ }, { workers: 1 });
+
+ expect(result.exitCode).toBe(1);
+ expect(result.failed).toBe(1);
+
+ const errorContextPath = testInfo.outputPath('test-results', 'a-fail', 'error-context.md');
+ const content = fs.readFileSync(errorContextPath, 'utf8');
+ const locationLine = content.split('\n').find(line => line.startsWith('- Location:'))!;
+ expect(locationLine).toBeTruthy();
+ // Location should be a relative path, not absolute.
+ expect(locationLine).toContain('- Location: a.spec.ts:');
+});
+
test('should take screenshot when page is closed in afterEach', async ({ runInlineTest }, testInfo) => {
const result = await runInlineTest({
'playwright.config.ts': `