playwright
playwright copied to clipboard
[Feature] Expose request feature globally
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....
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.
Ah, missed that. Is this global request object also traced so the requests are seen in the playwright trace viewer?
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.
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).
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 should we create a new context for each request, or can it be reused across the process?
Ping @yury-s on the above question 🙏
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.