playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Feature] Expose request feature globally

Open Meemaw opened this issue 2 years ago • 7 comments

Would be great if some fixtures (e.g request) would be exposed globally. Currently, one has to pass it around the POM and utility functions to call it which complicates the method/class signatures.

Would be nice if we could do something along these lines from anywhere in the code:

import { getRequestFixture } from `@playwright/test`

const request = getRequestFixture()
request....

Meemaw avatar Dec 20 '22 08:12 Meemaw

Fixtures are deeply integrated with the test lifecycle. They operate on the test-scoped entities such as context, page, etc. There is a global request object that you can use though, it is a part of the Playwright library, so you can use it anywhere you like:

import { request } from '@playwright/test'

See https://playwright.dev/docs/next/auth#sign-in-via-api-request for the example.

pavelfeldman avatar Dec 20 '22 15:12 pavelfeldman

Ah, missed that. Is this global request object also traced so the requests are seen in the playwright trace viewer?

Meemaw avatar Dec 20 '22 19:12 Meemaw

You would need to start/stop tracing yourself on this request context, because testing harness does not do it for you, you are now operating on the library level. Unfortunately, we did not expose tracing object on the request context and did not document it. I'll do it now, as a workaround you could do context._tracing.start() context._tracing.stop(), this tracing object is of type https://playwright.dev/docs/next/api/class-tracing.

pavelfeldman avatar Dec 20 '22 20:12 pavelfeldman

Yeah, main reason for using it in our case would be the tracing that the request fixture provides out of the box. I would hope there would be an easy for the global request object to do that as well (hopefully without having to trace it ourselves).

Meemaw avatar Dec 21 '22 13:12 Meemaw

Yeah, main reason for using it in our case would be the tracing that the request fixture provides out of the box. I would hope there would be an easy for the global request object to do that as well (hopefully without having to trace it ourselves).

The global request does pickup tracing settings from the test config today. So if you set trace: 'on' in your playwright.config.ts, the following request will show up in the trace:

import { test, expect, request } from '@playwright/test';

test('test', async ({ }) => {
  const req = await request.newContext();
  const r = await req.get('https://playwright.dev/');
  expect(r).toBeOK();
  expect(1).toBe(2);
});

Does it work for you?

yury-s avatar Jan 05 '23 18:01 yury-s

@yury-s should we create a new context for each request, or can it be reused across the process?

Meemaw avatar Jan 13 '23 12:01 Meemaw

Ping @yury-s on the above question 🙏

Meemaw avatar May 12 '23 11:05 Meemaw

Why was this issue closed?

Thank you for your involvement. This issue was closed due to limited engagement (upvotes/activity), lack of recent activity, and insufficient actionability. To maintain a manageable database, we prioritize issues based on these factors.

If you disagree with this closure, please open a new issue and reference this one. More support or clarity on its necessity may prompt a review. Your understanding and cooperation are appreciated.

pavelfeldman avatar Nov 17 '23 00:11 pavelfeldman