Intercept server-side API calls with Playwright?

Are there resources out there where I can read about intercepting server-side API calls with Playwright (particularly with the NextJS app router). page.route does not work. No errors, it just doesn’t intercept the call as expected. Hopefully with the new wave being server-side everything in web dev this has been figured out and standardized already or will be soon. Will have a few use cases where server-side API calls will need to be intercepted in the future. Google search results so far has been a mix or “use page.route” and “Playwright is a popular new testing software made by Microsoft…” Thanks in advance!
2 Replies
peterp
peterp9mo ago
You want to pretend to be a server-side API in the browser? Mabye Mock Service Worker is what you're looking for.
owenwexler
owenwexler9mo ago
Exactly it, basically. Lots of necessary use cases for this in the future across various projects (mocking external API results, Supabase, etc). Theoretically I could also set up a local mock Express API to mock those things if Playwright allows for a separate testing environment/testing env variables (Cypress did in theory but I could never get it working), or maybe someone already has a package available for mocking Supabase? I’ll look into all of this and look into MSW too. Thank you!
Want results from more Discord servers?
Add your server