Rayrun
← Back to Discord Forum

Append query parameters to page.goto() urls

combat_wombat__posted in #help-playwright
Open in Discord
combat_wombat__
combat_wombat__

How can I modify the default page.goto() method to automatically append query parameters to all calls to page.goto() in my tests?

This thread is trying to answer question "How can I modify the default `page.goto()` method to automatically append query parameters to all calls to `page.goto()` in my tests?"

8 replies

Make an abstracted function

combat_wombat__
combat_wombat__

@skorp32 can you please elaborate?

I've tried the following:

const test = base.extend({
  page: async ({ page }, use) => {
    const goto = page.goto;
    async function modifiedGoto(url, options) {
      url += "?test";
      return goto(url, options);
    }
    page.goto = modifiedGoto;
    await use(page);
  },
});

but it is returning the following error:

TypeError: Cannot read properties of undefined (reading '_mainFrame')

export async function goto(page: Page, params: string) {
  await page.goto() // do smth here
}
combat_wombat__
combat_wombat__

I'm looking for a solution that allows me to continue to use page.goto() and appends query parameters "behind the scenes".

not really possible

maybe with page.route to catch whatever you want, append url and continue but not sure if it works for navigational requests

combat_wombat__
combat_wombat__

I tried that. It didn't work for navigations

combat_wombat__
combat_wombat__

Fixed it. Here's the final code:

const test = base.extend({
  page: async ({ page }, use) => {
    const goto = page.goto.bind(page);
    function modifiedGoto(url, options) {
      url += "?test";
      return goto(url, options);
    }
    page.goto = modifiedGoto;
    await use(page);
    page.goto = goto;
  },
});

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.