Localization testing practice
Practice locating elements in a page that ships in two languages. Switch the checkout between English and Deutsch: text and accessible names change, test ids and roles do not. The switch is a link parameter (?lang=de), so everyone sees the same page. Open the shop checkout
Checkout in two languagesUse the Language switch above. The same build serves both.
Built well
Checkout
Total: $149.00
Built to break
Checkout
Total: $149.00
Tasks: write a locator, get a grade
How to locate Localized text
Text locators only speak one language
A locator that matches on visible text, such as //button[text()='Place order'] or cy.contains('Place order'), is a locator for the English page. Run the same suite against German and it finds nothing. Role locators with a name have the same problem, because the accessible name is translated too: getByRole('button', { name: 'Place order' }) matches only in English.
Numbers and dates move as well. $149.00 becomes 149,00 €, so an assertion on the exact total string breaks even when the amount is right.
Many sites pick the language from the browser, which makes failures depend on the machine running the test. This page uses a link parameter instead, so a failing run can be reproduced exactly.
What a stable locator looks like
Anchor on what translation leaves alone: test ids, roles, and stable attributes such as name or type. Keep text assertions, but read the expected strings from the same translation files the app uses, and pin the test’s locale instead of inheriting the browser’s.
// Test ids survive the switch; text and names do not
for (const lang of ['en', 'de']) {
await page.goto(`/practice/localized-text?lang=${lang}`);
await page.getByTestId('l10n-email').fill('[email protected]');
await page.getByTestId('l10n-place-order').click();
await expect(page.getByTestId('l10n-result')).not.toBeEmpty();
}In a real workflow
The shop checkout is English only, but its Place order button keeps the same test id, role and name on every release. Write the checkout test with test ids and roles first, so a translated build needs new strings, not new locators.
COR Lens generates locators for shadow DOM, iframes and tables, and grades each one. Click the localized text above with it and compare its pick with yours.
Add to Chrome