-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Comments
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. |
Will this |
It will work for another page.goto's. |
closing since it seems answered, please re-file if you have more questions. |
@alexbjorlig You can also use the strategy implemented in the sveltekit repo itself. Tldr; you wrap(override?) the |
This looks amazing! Does this also work/support client-side navigation? |
Sure. You can override the default page fixture and use it in all your tests. Client side navigations should breeze through since the // +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);
},
}); |
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. |
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:
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
The text was updated successfully, but these errors were encountered: