Skip to content

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

feat(test.info): expose information on the currently running test (#10708)

Pavel Feldman518d67add502
diff --git a/docs/src/test-api/class-test.md b/docs/src/test-api/class-test.md
index 532b2ea7da..2736d5d47d 100644
--- a/docs/src/test-api/class-test.md
+++ b/docs/src/test-api/class-test.md
@@ -800,6 +800,10 @@ A function that returns whether to mark as "fixme", based on test fixtures. Test
An optional description that will be reflected in a test report.
+## method: Test.info
+- returns: <[TestInfo]>
+
+Returns information about the currently running test. This method can only be called during the test execution, otherwise it throws.
## method: Test.only
diff --git a/packages/playwright-test/src/testType.ts b/packages/playwright-test/src/testType.ts
index 2a2c5ed932..b7b9e5c720 100644
--- a/packages/playwright-test/src/testType.ts
+++ b/packages/playwright-test/src/testType.ts
@@ -54,6 +54,12 @@ export class TestTypeImpl {
test.use = wrapFunctionWithLocation(this._use.bind(this));
test.extend = wrapFunctionWithLocation(this._extend.bind(this));
test.extendTest = wrapFunctionWithLocation(this._extendTest.bind(this));
+ test.info = () => {
+ const result = currentTestInfo();
+ if (!result)
+ throw new Error('test.info() can only be called while test is running');
+ return result;
+ };
this.test = test;
}
diff --git a/packages/playwright-test/types/test.d.ts b/packages/playwright-test/types/test.d.ts
index 616e6c6793..e57de54a23 100644
--- a/packages/playwright-test/types/test.d.ts
+++ b/packages/playwright-test/types/test.d.ts
@@ -2599,6 +2599,11 @@ export interface TestType<TestArgs extends KeyValue, WorkerArgs extends KeyValue
*/
extend<T, W extends KeyValue = {}>(fixtures: Fixtures<T, W, TestArgs, WorkerArgs>): TestType<TestArgs & T, WorkerArgs & W>;
extendTest<T, W>(other: TestType<T, W>): TestType<TestArgs & T, WorkerArgs & W>;
+ /**
+ * Returns information about the currently running test. This method can only be called during the test execution,
+ * otherwise it throws.
+ */
+ info(): TestInfo;
}
type KeyValue = { [key: string]: any };
diff --git a/tests/playwright-test/test-info.spec.ts b/tests/playwright-test/test-info.spec.ts
index f3a18f7bf1..a69019c908 100644
--- a/tests/playwright-test/test-info.spec.ts
+++ b/tests/playwright-test/test-info.spec.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import { test, expect } from './playwright-test-fixtures';
+import { test, expect, stripAscii } from './playwright-test-fixtures';
test('should work directly', async ({ runInlineTest }) => {
const result = await runInlineTest({
@@ -52,3 +52,39 @@ test('should work via fixture', async ({ runInlineTest }) => {
});
expect(result.exitCode).toBe(0);
});
+
+test('should work via test.info', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'helper.ts': `
+ export const test = pwt.test.extend({
+ title: async ({}, run) => {
+ await run(pwt.test.info().title);
+ },
+ });
+ `,
+ 'a.test.js': `
+ const { test } = require('./helper');
+ test('test 1', async ({title}) => {
+ expect(test.info().title).toBe('test 1');
+ expect(title).toBe('test 1');
+ });
+ test('test 2', async ({title}) => {
+ expect(title).toBe('test 2');
+ });
+ `,
+ });
+ expect(result.exitCode).toBe(0);
+});
+
+test('should throw outside test', async ({ runInlineTest }) => {
+ const result = await runInlineTest({
+ 'a.test.js': `
+ const { test } = pwt;
+ test.info();
+ test('test 1', async ({title}) => {});
+ `,
+ });
+ const output = stripAscii(result.output);
+ expect(result.exitCode).toBe(1);
+ expect(output).toContain('test.info() can only be called while test is running');
+});
diff --git a/utils/generate_types/overrides-test.d.ts b/utils/generate_types/overrides-test.d.ts
index 17e3408353..b81cef0998 100644
--- a/utils/generate_types/overrides-test.d.ts
+++ b/utils/generate_types/overrides-test.d.ts
@@ -267,6 +267,7 @@ export interface TestType<TestArgs extends KeyValue, WorkerArgs extends KeyValue
expect: Expect;
extend<T, W extends KeyValue = {}>(fixtures: Fixtures<T, W, TestArgs, WorkerArgs>): TestType<TestArgs & T, WorkerArgs & W>;
extendTest<T, W>(other: TestType<T, W>): TestType<TestArgs & T, WorkerArgs & W>;
+ info(): TestInfo;
}
type KeyValue = { [key: string]: any };