I'm writing a suite of tests in Playwright. These tests will be run against multiple environments, including production. However, production has analytics and I'm concerned that running the suite on a regular basis could skew the numbers.
Is there any way to identify Playwright test traffic so that it can be filtered from the analytics reports?
Yes, I haven't worked much with Playwright, preferring Selenium and Puppeteer for this kind of automation, however, the way we do it there is typically by indicating the fact that this is a bot in the useragent. This snippet should work for Playwright pretty well:
const {webkit} = require('playwright');
(async () => {
const browser = await webkit.launch();
// Create a new incognito browser context with a proper user agent
const context = await browser.newContext({
userAgent: 'Playwright Bot for internal QA automation'
});
// Now the page will have the user agent
const page = await context.newPage('https://example.com');
console.log(await page.evaluate(() => navigator.userAgent));
})();
Took it from their git repo issues. However, they have a bug there that they won't fix. But it looks like that's only when you're using proxies, which, I imagine, you won't. And even if you do, there's a workaround, so we should be good relying on the user agent.
It is good, however, to reach out to your analytics department and let them know that you're gonna send multiple hits to them through the testing, indicating how they can filter out your traffic. They may need time to create rules in their tag management systems to re-route your testing traffic tracking to the lower environment analytics database based on the user agent. Don't forget to tell them that the tests are planned to be run in prod though since it's a rare occasion and they should prepare.