Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How do I listen for a custom event, when doing page.goto? #19858

Closed
alexbjorlig opened this issue Jan 4, 2023 · 9 comments
Closed

Comments

@alexbjorlig
Copy link

alexbjorlig commented Jan 4, 2023

My objective is to create a helper function, that navigates to a page and waits for the page hydration event. My initial code looks like this:

await page.goto(url);
await page.evaulate( ... window.addEventListener('sveltekit:start )

Works great, but there is a bug. Sometimes the sveltekit:start event is emitted before the event listener is added.

How can I add the eventListener before calling page.goto?

If I move the listener to before page.goto I get this error : Execution context was destroyed, most likely because of a navigation

@mxschmitt
Copy link
Member

mxschmitt commented Jan 4, 2023

You can do the following, would that work for your use-case?

  await page.addInitScript(() => {
    let resolveSvelteKitStart = () => {};
    window.svelteKitStart = new Promise(resolve => resolveSvelteKitStart = resolve);
    window.addEventListener('sveltekit:start', () => resolveSvelteKitStart());
  });
  await page.goto("...")
  await page.waitForFunction(() => window.svelteKitStart);

The addInitScript needs to be called only once for the whole page. You can also call it on the context level.

@alexbjorlig
Copy link
Author

await page.waitForFunction(() => window.svelteKitStart);

Will this await also work on another page.goto - or only the first time?

@mxschmitt
Copy link
Member

It will work for another page.goto's.

@mxschmitt
Copy link
Member

closing since it seems answered, please re-file if you have more questions.

@thenbe
Copy link

thenbe commented Jan 10, 2023

@alexbjorlig You can also use the strategy implemented in the sveltekit repo itself. Tldr; you wrap(override?) the goto function.

@alexbjorlig
Copy link
Author

@alexbjorlig You can also use the strategy implemented in the sveltekit repo itself. Tldr; you wrap(override?) the goto function.

This looks amazing!

Does this also work/support client-side navigation?

@thenbe
Copy link

thenbe commented Jan 10, 2023

Sure. You can override the default page fixture and use it in all your tests. Client side navigations should breeze through since the started class is available instantly. And durring ssr, the tests should wait for that onMount to happen, signalling that hydration is complete.

// +layout.svelte

onMount(() => {
  // communicate that the app is ready - used for testing
  document.body.classList.add('started');
});
// my-fixture.ts

export const test = base.extend({
  page: async ({
    page,
  }, use) => {
    const goto = page.goto;

    page.goto = async function(url, opts) {
      const res = await goto.call(page, url, opts);

      // https://github.com/sveltejs/kit/pull/6484
      await page.waitForSelector('body.started', {
        timeout: 5000
      });

      return res;
    };

    await use(page);
  },
});

@phil-w
Copy link

phil-w commented Apr 1, 2024

It may be me being thick, but without this little hack, my Playwright Sveltekit tests were completely and horribly unreliable in ways I've not come across since the early days of test tools. This is a life safer... I guess I think maybe it needs a bit more publicity. Thanks anyway.

@jgchk
Copy link

jgchk commented May 21, 2024

It may be me being thick, but without this little hack, my Playwright Sveltekit tests were completely and horribly unreliable in ways I've not come across since the early days of test tools. This is a life safer... I guess I think maybe it needs a bit more publicity. Thanks anyway.

Agreed, this feels like something that should be included in the default SvelteKit scaffolding when users opt to include Playwright, or at the very least featured in documentation somewhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants