Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Support attachment for particular test step #32748

Open
sttaran opened this issue Sep 21, 2024 · 4 comments · May be fixed by #34037
Open

[Feature]: Support attachment for particular test step #32748

sttaran opened this issue Sep 21, 2024 · 4 comments · May be fixed by #34037
Assignees
Labels

Comments

@sttaran
Copy link

sttaran commented Sep 21, 2024

🚀 Feature Request

I heavily use playwright for API tests. I highly appreciate and extremely love that we can see all requests made by APIRequestContext in traces.

However sometimes test scenarios require a lot of API calls so having possibility to see request/response payloads right in the test step in html report would make debugging much easier.

Example

I am not really good at designing but that's a simple option how it would look like

image

Motivation

This feature would make debugging much easier not only for people who write automated tests but also for developers who faced with failed tests and want to reproduce the failure or just to investigate the root cause.

UPD: I provided single use case I want to have the feature to. But I know that some teams do not use APIRequestContext and use some external libraries as http clients. E.g. graphql-codegen for generating api methods on client. In this case it would also help them because they are not going to have network info in traces.
However I believe such feature could be helpful not only for logging API calls.

@vitalets
Copy link
Contributor

vitalets commented Sep 26, 2024

I raised a similar issue before, there is an interesting point about concurrent steps: #29323 (comment)

@bdcarr
Copy link

bdcarr commented Nov 6, 2024

Oh my friend thank you, I wanted to request this too, but my use case is different and I didn't have much hope of a positive response so I didn't bother.

We already used Playwright for UI tests for a long time before we started doing API testing with it, so we've built up quite a library of functions for interacting with our API and they all use a customised instance of Axios. When we started doing API stuff in the same framework, we had a look at the built-in HTTP client but it was missing certain config that we need, so I hacked a slightly dodgy workaround for attaching requests/responses to the test steps. This function gets set as a request & response interceptor on Axios:

private async attachResponseToTest(responseOrError: AxiosResponse | AxiosError) {
  if (process.env.ATTACH_RESPONSES_TO_TEST !== 'true') return;
  const response = axios.isAxiosError(responseOrError) ? responseOrError?.response : responseOrError;

  const { data, config } = response;
  await test.step(`${config?.method?.toUpperCase()} ${config?.url}`, async () => {
    await test.step(`Request: ${config?.data}`, async () => {});
    await test.step(`Response: ${response?.status} ${response?.statusText} ${stringify(data)}`, async () => {});
  });
}

It's not perfect (you have to inspect HTML in the report to see more than the first few dozen chars of the 'step name', and it does eventually truncate) but it's done the job well enough for now:

Image

My additional request

Whatever function you create for attaching request info to test steps, could you expose it publicly so those of us using 3rd party HTTP clients can also use it? Even if I have to massage the request object to make it fit, that would be fantastic.

@Skn0tt
Copy link
Member

Skn0tt commented Nov 6, 2024

We've started some work on implementing per-step attachments. If that ends up successful, you'll be able to attach request and response info as attachments, and even format them as HTML to make them read nice.

it was missing certain config that we need

I'm curious, what's that config that you're missing in the builtin API Client? What would we need to build so you can migrate from Axios?

could you expose [the network tracing API] publicly so those of us using 3rd party HTTP clients can also use it?

Could you open that as a separate feature request, so we can track user demand on that?

@bdcarr
Copy link

bdcarr commented Nov 6, 2024

Thanks mate, opened a separate one and answered your Q about the API client config at the bottom: #33481

@mxschmitt mxschmitt added v1.50 and removed v1.49 labels Nov 11, 2024
@Skn0tt Skn0tt linked a pull request Dec 16, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
7 participants