Rayrun
← Back to Discord Forum

wait for specific request to finish

automobil'
automobil'

i want to go away from hard waiting. i perform element.click() and know that specific request works after that and how can i wait for exactly this request is finished?

This thread is trying to answer question "How can I wait for a specific request to finish after clicking an element without using hard waiting?"

3 replies
rui.figueira
rui.figueira

you can use a similar approach to this:

rui.figueira
rui.figueira

just create route rules to intersect requests and use a promise to resolve it asynchronously and then wait for it

Answer

Waiting for a Request After a Click Action in Playwright

Playwright automatically waits for elements to be ready before executing actions. This includes checks to ensure the element is attached to the DOM, visible, stable, enabled, and not obscured by other elements.

Here's how you can wait for a specific request to finish after a click action:

import { chromium } from 'playwright';

async function waitForRequestAfterClick() {
  const browser = await chromium.launch();
  const context = await browser.newContext();
  const page = await context.newPage();

  // Perform element click with custom options
  await page.click('button', { timeout: 5000, noWaitAfter: true });

  // Wait for a specific request to finish
  await page.waitForRequest('https://ray.run/api/data');

  await browser.close();
}

waitForRequestAfterClick();

In this code, we first launch a Chromium browser and create a new page. We then perform a click action on a button with a timeout of 5000 milliseconds (5 seconds) and set noWaitAfter to true. This tells Playwright to perform actionability checks but not to wait for any navigations after the click.

We then use page.waitForRequest() to wait for a specific request to finish. Replace 'button' and 'https://ray.run/api/data' with your actual element selector and request URL.

By using these options, you can avoid hard waiting techniques and ensure your tests run smoothly and efficiently.

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.