Rayrun
← Back to Discord Forum

How to set browser language

navaneethanposted in #help-playwright
Open in Discord

I want to test an application for language translations. How to do this in playwright? Playwright has this config "locale" and "timezoneId" . But when I set them and access a site, it is still showing the content in english. Questions:

  1. In general, for any site, Does all browsers show the content in english when accessed from any part of the world?
  2. how can I set my browser in playwright to mimic this realworld scenario?

This thread is trying to answer question "How can I set my browser language in Playwright to test an application's language translations?"

4 replies
cherry_picker_01

Have you tried setting geolocation?

cherry_picker_01

Some websites rely on IP geolocations or user preferences

peritektikum
peritektikum

You can check how the locale in your UserAgent is set by visiting https://gtranslate.io/detect-browser-language

With Playwright is it possible to assert against the above page, whether your locale config for the browser context is being recognized

//detectBrowserLanguage.spec.ts

import { expect, test } from "@playwright/test";

const expectedLocale = "de-DE";
test.use({ locale: expectedLocale });

test("checkMyBrowserLocale", async ({ page }) => {
  await page.goto("https://gtranslate.io/detect-browser-language");

  const tableAcceptLanguageHeader = page
    .getByRole("table")
    .filter({ hasText: "Preference Score" });
  const tableNavigatorLanguages = page
    .getByRole("table")
    .filter({ hasNotText: "Preference Score" });

  const localeAcceptLanguageHeader = await tableAcceptLanguageHeader
    .getByRole("row")
    .filter({ hasNot: page.locator("th") })
    .locator("td")
    .first()
    .innerText();

  const localeNavigatorLanguages = await tableNavigatorLanguages
    .getByRole("row")
    .filter({ hasNot: page.locator("th") })
    .locator("td")
    .first()
    .innerText();

  expect(localeAcceptLanguageHeader).toBe(expectedLocale);
  expect(localeNavigatorLanguages).toBe(expectedLocale);
});

thanks guys, will try these.

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

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 luc@ray.run.