Rayrun
← Back to Discord Forum

Assert 3rd party calls

The following assertion exceeds the 30000ms threshold. Does it look like I am structuring the assertion correctly?

test.beforeEach(async ({ page }) => {
  await page.goto('https://my-site.com/my-page')
})

test('comscore', async ({ page }) => {
  const responsePromise = page.waitForResponse(/https:\/\/sb\.scorecardresearch\.com\/b.*/)
  const response = await responsePromise

  expect(response.ok()).toBe(true)
})

This thread is trying to answer question "How can I structure Playwright tests to assert that multiple third-party scripts load correctly without causing too much network strain or having the test timeout if one script fails to load?"

8 replies

Assert 3rd party calls

technically not but don't you miss a click or some other action which would trigger this route /https://sb.scorecardresearch.com/b.*/?

because now it looks like you're asserting a response which never happens

I am trying to assert 3rd party scripts that load simply because they are included in the head of the web page. There is no interaction that triggers the loading of the script. Is there a better way to structure such a test?

In that case you need to put your response promise before go to

Thanks.

That works well. But, I want to test a handful of third parties loading. If I put all the responsePromises in the same test, if the first one times out, none of the other responsePromises resolve. I want to use the same goto for triggering all the responsePromises though. I do not want to put an undue amount of network strain on the app running so many gotos in succession, which is what would happen if each third party was in its own test with its own goto. Here is my test following my most recent refactor.

import { test, expect } from '@playwright/test'

test('homepage third parties', async ({ page }) => {
  const responsePromises = {
    comscore: page.waitForResponse(/https:\/\/sb\.scorecardresearch\.com\/b.*/), // If this promise never resolves, the whole test times out.
    ga4: page.waitForResponse(/https:\/\/analytics\.google\.com\/g\/collect.*/),
    chartbeat: page.waitForResponse(/https:\/\/ping\.chartbeat\.net\/ping.*/),
    gam: page.waitForResponse(/https:\/\/securepubads\.g\.doubleclick\.net\/gampad\/ads.*/),
    permutive: page.waitForResponse(/https:\/\/api\.permutive\.com\/v2\.0\/batch\/events.*/)
  }

  await page.goto('https://my-site.com')

  for (const [key, responsePromise] of Object.entries(responsePromises)) {
    console.log(`homepage: ${key}`)
    const response = await responsePromise
    expect(response.ok() || response.status() === 302).toBe(true)
  }
})

I understand that a "partial failure" for a test makes no sense. So it seems to me each third party should have its own test, but I want to avoid too much network noise from so many gotos.

Related Discord Threads

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 [email protected].