playwright: [BUG] baseURL type definition error

Context:

  • Playwright Version: “@playwright/test”: “1.22.2”,
  • Operating System: Windows
  • Node.js version: v16.15.1

Describe the bug

I’m using this config:

const baseURL = "someURL";

const config: PlaywrightTestConfig = {
  // ...
  use: {
    baseURL,
    // ...
  },
  // ...
};

and in my tests:

test.describe.parallel("test", () => {
  test.beforeEach(async ({ page, baseURL }) => {
    await page.goto(baseURL); // typescript error, see below
	});
  // ...
})

but this is the Typscript error:

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
  Type 'undefined' is not assignable to type 'string'.ts(2345)

And it is understandable.

How to fix this?

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 20 (3 by maintainers)

Most upvoted comments

@ville-nvt, @dgozman this doesn’t work.

If I use await page.goto('./foo/bar') or await page.goto('/') it throws with:

page.goto: Protocol error (Page.navigate): Cannot navigate to invalid URL
=========================== logs ===========================
navigating to "/", waiting until "load"
============================================================

even if baseURL === "http://localhost:3000".

I think we should re-open this.

@ville-nvt, @dgozman this doesn’t work.

If I use await page.goto('./foo/bar') or await page.goto('/') it throws with:

page.goto: Protocol error (Page.navigate): Cannot navigate to invalid URL
=========================== logs ===========================
navigating to "/", waiting until "load"
============================================================

even if baseURL === "http://localhost:3000".

I think we should re-open this.

I am still facing this issue

As @ville-nvt suggests, you don’t usually need to reference baseURL directly, you use relative urls instead.

await page.goto('./foo/bar');

If you really need baseURL, silence TypeScript by adding an exclamation mark.

await page.goto(baseURL!);

I actually resolve it. I was not providing the config reference in the command when running a single file

I might be wrong because I don’t know TypeScript, but isn’t this code wrong?

In the documentation, baseURL configuration expects a string, but in the code below, at the top we set a custom const baseURL and in the use block, isn’t the baseURL wrong? Like, there, the baseURL is the configuration option that is expecting = some variable or string from the top constant. Or using this way will spread/destruct the top constant?

I’m sure I explained correctly my thoughts.

const baseURL = "someURL";

const config: PlaywrightTestConfig = {
  // ...
  use: {
    baseURL,
    // ...
  },
  // ...
};

For me, it should be like this:

const mybaseURL = "someURL";

const config: PlaywrightTestConfig = {
  // ...
  use: {
    baseURL = mybaseURL ,
    // ...
  },
  // ...
};

@frederikhors The example above works for me with Playwright v1.23. Could you please share a full repro that we can run locally to debug the issue?

The baseUrl is not working when navigating to “/” in global setup files. It works only in the tests.