Rayrun
← Back to Discord Forum

Is there a better way to extend goto()?

I want to extend goto() to use custom await logic, e.g.

const page = await context.newPage();

const goto = page.goto.bind(page);

page.goto = async (url, options) => {
  console.log('GOTO', url);

  await goto(url, options);

  await page.waitForSelector('.loaded');
};

Is that the way to do it?

This thread is trying to answer question "Is there a better way to extend goto() to use custom await logic?"

3 replies

This should work. Although I'd suggest not to overwrite built-in page.goto as you may need several navigations in test and on some pages .loaded selector may not present. I think it's better to use some helper like function openAndWait(page, url) { ... } or use Page Object Model.

The actual code was:

page.goto = async (url, options) => {
  console.time('navigation');

  const response = await goto(url, {
    waitUntil: 'networkidle',
    ...options,
  });

  console.timeEnd('navigation');

  return response;
};

This appears to improve things reasonably well, at least until we figure out a better solution.

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 [email protected].