Skip to content

[Feature]: allow to pass arbitrary location to test.step #30160

Closed

Description

🚀 Feature Request

Currently test.step method binds location in file to the place of it's call.
When test.step is wrapped with helper methods, it produces the same location for different steps.
Suggestion is to allow to pass arbitrary location to test.step. See example below.

Example

Imagine I have a helper.ts where I define step helper for my tests:

export async function clickButton(text: string) {
  return test.step(`User clicks button "${text}"`, async () => {
    // ...
  }); 
}

Now I run the following test from ./e2e/test.ts:

test('test', async ({}) => {
  await clickButton('foo');
  await clickButton('bar');
  await clickButton('baz');
});

In the report I get all steps with the same location:

It would be great to see more meaningful locations in the report, e.g.:

User clicks button "foo"— e2e/test.ts:2
User clicks button "bar"— e2e/test.ts:3
User clicks button "baz"— e2e/test.ts:4

If I were able to provide a custom location to test.step, I would achieve it. Example:

import { Location } from '@playwright/test/reporter';
import errorStackParser from 'error-stack-parser';

export async function clickButton(text: string) {
  const location = getStepLocation();
  return test.step(`User clicks button "${text}"`, async () => {
    // ...
  }, { location }); 
}

function getStepLocation(): Location {
  const frame = errorStackParser.parse(new Error()).find((frame) => frame.fileName.endsWith('.test.ts'));
  return {
      file: frame.getFileName(),
      line: frame.getLineNumber(),
      column: frame.getColumnNumber(),
  };
}

Motivation

In general this feature provides more opportunities for writing high order step helpers.

Personally, I need it for managing playwright-bdd project. I'm wrapping test.step with Given / When / Then helpers to have correct step titles in the report:

const Given = (text: string) => test.step(`Given ${text}`, () => { ... });
const When = (text: string) => test.step(`When ${text}`, () => { ... });
const Then = (text: string) => test.step(`Then ${text}`, () => { ... });

Before Playwright 1.43 I've used private testInfo._runAsStep method to run step with a custom location. Since 1.43 testInfo._runAsStep was replaced with a more complex logic and now I need more tricky hacks to achieve the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions