Rayrun
โ† Back to Discord Forum

Help to intercept and verify status of network request.

Hello! I need help with the code. I'm trying to reach a page, and I need to verify that the google analytics is getting 204.

I'm trying to understand the documentation, but I cannot make it work. It would be really helpfull if someone can give me the code in order to check it.

I need to verify that the URL that includes collect?v=2&tid=G-TYDS4GPM5T gets 204

If it could be in JS, would be great ๐Ÿ™‚

Thank you!

This thread is trying to answer question "How can I verify that a specific URL in a Google Analytics network request returns a 204 status code using JavaScript?"

4 replies

Here is your answer from chatgpt:

Hello! You can use Playwright to navigate to a webpage and check if a specific URL returns a 204 status code. Here's a JavaScript example of how you can achieve this:

const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch();
  const page = await browser.newPage();

  // Navigate to the webpage you want to test
  await page.goto('https://example.com'); // Replace with the actual URL

  // Wait for the network request to complete
  await page.waitForResponse(response => {
    return response.url().includes('collect?v=2&tid=G-TYDS4GPM5T') && response.status() === 204;
  });

  console.log('Google Analytics request returned a 204 status code.');

  await browser.close();
})();

Make sure to replace 'https://example.com' with the actual URL you want to test. This code launches a browser, navigates to the webpage, and waits for a network request that matches your criteria (URL and status code) before logging a message indicating that the Google Analytics request returned a 204 status code.

refactoreric

Actually the ChatGPT code does have some flaws:

  • It will hang and timeout if something else than 204 is received.
  • It has a race condition: If the goto is too fast, we might miss the 204 response.
  • It assumes that we are writing a script instead of a test.

Improved script version:

const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch();
  const page = await browser.newPage();

  // Start observing Google analytics response.
  const responsePromise =  page.waitForResponse(response => {
    return response.url().includes('collect?v=2&tid=G-TYDS4GPM5T');
  });

  // Navigate to the webpage you want to test
  await page.goto('https://example.com'); // Replace with the actual URL

  const response = await responsePromise;

  assert.strictEqual(response.status(), 204);
  console.log('Google Analytics request returned a 204 status code.');

  await browser.close();
})();```

Improved test version:
```import { test, expect } from '@playwright/test';

test('Google analytics responds with 204', async ({ page }) => {
  // Start observing Google analytics response.
  const responsePromise =  page.waitForResponse(response => {
    return response.url().includes('collect?v=2&tid=G-TYDS4GPM5T');
  });

  // Navigate to the webpage you want to test
  await page.goto('https://example.com'); // Replace with the actual URL

  const response = await responsePromise;
  expect(response).toBe(204);  
});```

thanks @refactoreric ! I had to do a change on your code, on the last line

expect(response.status()).toBe(204)

also thanks @hubspace . My chat gpt answers were not working correctly

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 luc@ray.run.