Playwright Testing for EU Cookie Directive and IAB TCF Compliance
Test IAB TCF 2.2 consent string generation, vendor consent management, and legitimate interest claims in EU programmatic advertising platforms.
playwright-v1-49-matrix
Playwright Testing for EU Cookie Directive and IAB TCF Compliance
Region Focus: This guide includes examples specifically relevant to EU-based applications.
Test IAB TCF 2.2 consent string generation, vendor consent management, and legitimate interest claims in EU programmatic advertising platforms. This guide provides step-by-step Playwright patterns to handle this scenario reliably.
Introduction
Robust automation requires handling domain-specific patterns effectively. This article covers playwright testing for eu cookie directive and iab tcf compliance with production-ready Playwright code.
Architecture Overview
graph TD
CMP["CMP Banner"] --> User["User Consent"]
User --> TCF["TCF String"]
TCF --> Cookie["euconsent-v2"]
Cookie --> Vendors["Vendor Consent"]Implementation Guide
test('IAB TCF consent string is stored after user consent', async ({ page }) => {
await page.goto('/');
await page.getByRole('button', { name: 'Accept All' }).click();
const tcString = await page.evaluate(() => {
return document.cookie.split(';')
.find(c => c.trim().startsWith('euconsent-v2='))?.split('=')[1];
});
expect(tcString).toBeTruthy();
expect(tcString?.length).toBeGreaterThan(50); // Valid TC string is long
});Run Your Tests
npx playwright test --grep "Playwright Testing for"
npx playwright test --uiReference Table
| TCF Event | Expected Behavior | Test Method |
|---|---|---|
| Accept All | All vendors consented | Cookie check |
| Reject All | No vendor consent | Cookie value |
| Custom | Selected vendors | String decode |
Best Practices
getByRole() and getByLabel() for resilient locators that survive UI changes.await expect(locator).toBeVisible() instead of waitForTimeout()Common Mistakes
| Anti-Pattern | Solution |
| Hardcoded wait times | Use auto-waiting assertions |
| Shared mutable test data | Create unique data per test |
| Testing 3rd party UIs directly | Mock external services |
Frequently Asked Questions
What is IAB TCF?
IAB Transparency and Consent Framework standardizes how websites collect and signal user consent to advertisers.
How to decode a TC string in Playwright?
Use the iabtcf-core library in page.evaluate() to decode the euconsent-v2 cookie value.
How to test legitimate interest claims?
Verify vendors using legitimate interest don't require explicit consent and can be objected to separately.
Can Playwright test the CMP UI vendor list?
Open the CMP vendor list panel and verify all IAB TCF registered vendors appear with correct descriptions.
How to test CMP for Google's Additional Consent Mode?
Verify the addtl_consent cookie is set with correct Google vendor IDs after user consent.
Summary
Test IAB TCF 2.2 consent string generation, vendor consent management, and legitimate interest claims in EU programmatic advertising platforms. Apply these patterns to build a reliable, maintainable automation suite for your specific use case.
Related Articles
About The Author
PlaywrightPad Editorial reports on Chromium engines, E2E test optimizations, and AI integration specifications.
Newsletter
Get weekly browser reports sent directly to your inbox.