Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to verify an API request has not been made with Playwright

I have a chat application that I'm writing Playwright tests for. There is a textbox where the user can write their message. When the user presses Enter, the message should be sent. When the user presses Shift+Enter, the message should not be sent, but a line break should be added to the message.

How can I write Playwright tests for this? Here is a start:

test("pressing enter should send message", async ({ page }) => {
  await page.goto("/chat");

  const input = page.getByRole("textbox");

  await input.fill("foo");
  await input.press("Enter");
  await expect(input).toHaveValue("foo");

  // TODO: Expect that the form is submitted.
});

test("pressing shift+enter should create a line break", async ({ page }) => {
  await page.goto("/chat");

  const input = page.getByRole("textbox");

  await input.fill("foo");
  await input.press("Enter");
  await expect(input).toHaveValue("foo\n"); // Can the line break be \r\n?

  // TODO: Expect that the form is not submitted
});
like image 451
Magnar Myrtveit Avatar asked Sep 05 '25 03:09

Magnar Myrtveit


1 Answers

I would verify a certain API request has not been made like below:

    let fooRequestWasMade = false;
 
    page.on('request', request => {
      if (request.url().includes("/foobar/"))
        fooRequestWasMade = true
    })

    await page.waitForLoadState('networkidle');
    expect(fooRequestWasMade).toBe(false);
like image 139
Vishal Aggarwal Avatar answered Sep 07 '25 22:09

Vishal Aggarwal