Skip to main content

Playwright Expect Polling

Polling generally means checking status, or in the case of the playwright, it can be for checking Response. Using the expect, we can convert any synchronous expect to be asynchronous expect.poll

So in this article, we will try to get the response status 200 from a site called reqres.

const {test, expect} = require('@playwright/test');

  test('ElementHandle', async ({page})=> 
{

    await expect.poll(async () => {
        const response = await page.request.get('https://reqres.in/api/unknown/2');
        console.log(response);
        return response.status();
      }, {
        message: 'Try Again',
        timeout: 10000,
      }).toBe(200);

});
  • So here, we used the async poll with expect and stored the page's Response in the response object.
  • We are logging in and getting the response status as a return.
  • Within that, we can log some custom error messages and timeout too.

  • Here is the Response from the reqres site.
const {test, expect} = require('@playwright/test');

  test('ElementHandle', async ({page})=> 
{

    await expect.poll(async () => {
        const response = await page.request.get('https://reqres.in/api/unknown/2');
        console.log(response);
        return response.status();
      }, {
        intervals: [1000, 2000, 10000],
        timeout: 10000,
      }).toBe(200);

});
  • We can also specify custom polling intervals of wait time to wait for with gap.
Tags
Submitted by arilio666 on June 16, 2022

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.