🌍 Playwright i18n Nightmare: Are your international users seeing gibberish?
Testing internationalized (i18n) web applications with Playwright goes beyond simple text. You need to ensure content, numeric, and date formats are perfect across every locale.
📌 Problem Statement
• Global applications demand accurate localization.
• Basic text assertions often miss critical formatting errors for numbers, dates, and currencies.
❌ Relying solely on default browser settings can lead to inconsistent i18n tests.
❌ Manual verification across numerous locales is slow, expensive, and error-prone.
💡 Solution & Code Walkthrough
Playwright empowers you to simulate specific locale environments effortlessly.
• Browser Context Locale: Configure the browser's language preferences via browser.newContext({ locale: 'xx-YY' }). This influences how client-side APIs (e.g., JavaScript Intl) format data.
• Accept-Language Header: For server-side i18n, set the Accept-Language HTTP header using page.setExtraHTTPHeaders(). This signals the server which localized content to serve.
import { test, expect } from '@playwright/test';
test.describe('Internationalization (i18n) Testing', () => {
test('validates en-US locale content and formatting', async ({ browser }) => {
// 1. Create a new context with 'en-US' locale
const context = await browser.newContext({ locale: 'en-US' });
const page = await context.newPage();
// 2. Set 'Accept-Language' header for server-side i18n
await page.setExtraHTTPHeaders({ 'Accept-Language': 'en-US' });
await page.goto('https://your-i18n-app.com/product'); // Replace with your app URL
// 3. Assert localized content, numbers, and dates
await expect(page.locator('#product-title')).toHaveText('Product Name');
await expect(page.locator('#product-price')).toHaveText('$1,234.56'); // US number format
await expect(page.locator('#last-updated')).toHaveText('January 1, 2023'); // US date format
await context.close();
});
// ✅ Extend this pattern: Create more tests for other locales (e.g., 'de-DE', 'fr-FR')
// by changing `locale` and `Accept-Language`, and asserting expected localizations.
});
🔑 Key Takeaways
• browser.newContext({ locale: 'xx-YY' }) configures the browser's language.
• page.setExtraHTTPHeaders({ 'Accept-Language': 'xx-YY' }) ensures correct server-side content delivery.
• Validate not just text, but specific locale-formatted numbers, dates, and currencies.
✅ Automated i18n tests guarantee a consistent global user experience.
❓ Quick Summary Q&A
Q: Why use locale in newContext?
A: Influences client-side JS APIs for number/date formatting.
Q: What does Accept-Language do?
A: Informs the server which language content to return.
Q: What should i18n tests assert?
A: Localized text, numeric formats (e.g., decimal/thousands separators), and date/time formats.
TAGS: playwright, i18n, testing, automation, localization, typescript
────────────────────────────────────────
Ready to supercharge your testing skills? Download our app today!
────────────────────────────────────────
📲 𝐅𝐑𝐄𝐄 𝐌𝐎𝐁𝐈𝐋𝐄 𝐀𝐏𝐏 — 𝟔𝟎𝟎+ 𝐒𝐃𝐄𝐓 𝐐&𝐀𝐬
Practice real-world interview scenarios offline on the free QA Automation & SDET Prep app:
🤖 𝐆𝐨𝐨𝐠𝐥𝐞 𝐏𝐥𝐚𝐲 (𝐀𝐧𝐝𝐫𝐨𝐢𝐝):
https://play.google.com/store/apps/details?id=com.app.seleniuminterviewquestions&referrer=utm_source%3Ddevto%26utm_medium%3Darticle%26utm_campaign%3Dselenium_20261125
🍎 𝐀𝐩𝐩 𝐒𝐭𝐨𝐫𝐞 (𝐢𝐎𝐒):
https://apps.apple.com/app/id6786760948?pt=128640464&ct=devto_selenium_20261125&mt=8
────────────────────────────────────────
────────────────────────────────────────
Top comments (0)