I am writing a JavaScript application and using Playwright to test my application.
I found a bug in my code such that on selecting an item in a list, my app got stuck in an infinite loop calling a server API.
How can you write a playwright tests to ensure an action only causes a single server call.
Sample Code
I wait for the item to be visible using something like the following
await page.locator('list1','file1').waitFor();
Then I select and wait for the server request with code similar to beloe
const responsePromise = page.waitForResponse(resp =>
resp.url().includes('/myServerURL') && resp.status() === 200);
await page.locator('list1','file1').click();
const response = await responsePromise;
But i want to make sure that I only get one request to ‘/myServerURL’ on selecting the item, and not more than one.
Is this possible in Playwright
2
Answers
This is certainly possible:
But I don’t think this is a good idea. It’s cumbersome to code (although you can write a helper function), and strikes me as overly paranoid. If other requests on the page to the same endpoint happen to trigger at the same time, you may see a false positive.
I’d focus more on the user experience than on network activity. There’s probably a better way to verify this given more context. For example, to verify the app isn’t frozen after the button click, try clicking something else and see if it takes effect.
That said, I suppose if you have an isolated case, you could use this once-off to help avoid regressions.
Note that since you haven’t provided a reproducible case of this "button firing request multiple times" bug, I’ve made a best guess, and you’ll likely need to adapt my code to match your actual application.
My feeling is the the logic of the test is to capture the failure when waiting on the 2nd fetch.
To this end
expect(promise).rejects.toThrow()
looks like it takes the right direction, but I’m working to improve the timing.Here’s the test
My sample web page is