microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
66.88k stars 3.67k forks source link

[Feature]: Search params page assertion #32708

Open joshuajaco opened 1 month ago

joshuajaco commented 1 month ago

🚀 Feature Request

There should be an API to test search params of the page, something like:

await expect(page).toHaveSearchParams({ foo: "bar" });

Example

No response

Motivation

toHaveURL is not sufficient for testing search params for multiple reasons:

joshuajaco commented 1 month ago

As a workaround I currently use:

await expect(() => {
  const url = new URL(page.url());
  expect(url.searchParams.get("foo")).toEqual("bar");
}).toPass();
Darkensses commented 2 weeks ago

You can also use:

await page.goto('https://github.com/search?q=playwright&type=repositories');
let params = {};
new URL(page.url()).searchParams.forEach(function (val, key) {
  params[key] = val;
});    
expect(params).toMatchObject({q: 'playwright', type: 'repositories'});

In this way, you can search for multiple params. I'm also working in the code to make a PR but I'm not sure where to put this piece of code. I was digging mainly in matchers.ts (I only declared the new method here), as well as expect.ts and injectedScript.ts but I couldn't figure out how to implement this feature at all, any help would be appreciated 😄