Does this seem right? Would like to test with any number of different locale. Was thinking could update the playwright.config.ts and add a project entry adding the locale in the 'use' like:
projects: [ { name: "Desktop Chrome-EN", use: { ...devices["Desktop Chrome"], baseURL: ENV.URL, locale: "en-US" } }, { name: "Desktop Chrome-ES", use: { ...devices["Desktop Chrome"], baseURL: ENV.URL, locale: "es-ES" } } ,
Would think this should work, what i'm missing is when the tests run pulling out what the current locale that should be used?
This thread is trying to answer question "Does updating the playwright.config.ts and adding a project entry with the locale in the 'use' allow for testing different browser locales, and how can the current locale be pulled out when the tests run?"
Related Ask AI answer for "How do I set up and verify different browser locales for testing in Playwright?".
You can set different browser locales for testing in Playwright by updating the playwright.config.ts
file and adding a project entry with the locale in the 'use' option. Here's how you can set a default locale:
import { defineConfig } from '@playwright/test';
export default defineConfig({
use: {
locale: 'en-GB',
},
});
You can also override this setting for specific projects or tests. For a specific project:
import { defineConfig } from '@playwright/test';
export default defineConfig({
projects: [
{
name: 'chromium',
use: {
...devices['Desktop Chrome'],
locale: 'de-DE',
},
},
],
});
For a specific test:
import { test } from '@playwright/test';
test.use({
locale: 'fr-FR'
});
test('example', async ({ page }) => {
// Test code here
});
To verify the current browser locale during test execution, you can use page.evaluate()
and JavaScript's navigator.language
property:
import { test, expect } from '@playwright/test';
test('get current locale', async ({ page }) => {
const currentLocale = await page.evaluate(() => navigator.language);
expect(currentLocale).toBe('en-GB');
});
This way, you can easily configure different browser locales for testing purposes and retrieve the current browser locale during test execution. Happy testing with Playwright!
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 [email protected].