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.
Thu, 06/16/2022 - 10:55

Authored by

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.
Tags