playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Question] Getting baseURL?

Open msmith1114 opened this issue 1 year ago • 2 comments

I noticed a similar issue here: https://github.com/microsoft/playwright/issues/9439

Basically I need a way to get baseURL outside of a fixture? I need to be able to get the current baseURL in a class for being able to merge baseURL w/ another url (Some methods we use require the baseURL to produce headers).

Not sure if this is possible?

msmith1114 avatar Feb 16 '24 15:02 msmith1114

You can pass the baseURL from your test over to your class in order to have access to it, would that work for you?

mxschmitt avatar Feb 16 '24 15:02 mxschmitt

That's what I ended up doing. Something like:

In the test:

  test.beforeEach(async ({ request, baseURL }) => {
    widget= new Widget(request, baseURL);
  });

in the class:

  constructor(protected apiContext: APIRequestContext, protected baseUrl: string) {
    super(apiContext, baseUrl);
//super calls the base class
  }

Does that seem correct?

msmith1114 avatar Feb 16 '24 17:02 msmith1114

This is correct and the recommend way.

mxschmitt avatar Feb 19 '24 08:02 mxschmitt