playwrightplaywright-test

Playwright: How to ignore query params in waitForUrl() and toHaveUrl()?


Is it possible to expect page toHaveURL and ignore query params in Playwright?

I'm trying to check:

await expect(page).toHaveURL('/clients/new');

But the current url is e.g.:

https://server.com/clients/new?sort=name&direction=desc

If I use a regular expression, I have to substitute a server name that is different locally, in docker, in ci, and on prod. It's ok for now, but I don't like it :)

I expect toHaveURL to be true if the url has query params.


Solution

  • If you need match /clients/new from http://env/clients/new?sort=name&direction=desc, I think you can use next Regex with Playwright assert.

    await expect(page).toHaveURL(
      new RegExp('.*env\/clients\/new(?!\/)'),
    );
    
    'http://env/clients/new'                           match
    'http://env/clients/new?sort=name&direction=desc'  match
    'http://env/clients/new/other'                     not match