~ 1 min read

How to Set the Locale Used in Cypress Tests

When running some Cypress tests in Github Actions that were inspecting dates recently, I wanted to be able to set the locale of the browser - specifically for Chrome which I was using with Cypress.

The tests checked the date format of dd/mm/yyyy and ran fine locally in my own locale of Great Britain, but when my Github action workflow executed them they appeared in a mm/dd/yyyy format.

Originally, I’d thought this require a step within setting up the github action environment itself which set my linux box to have the appropriate locale, but this doesn’t change anything - presumably because the Cypress github action runs in a separate step.

Turns out it is possible, but by using the chrome devtools protocol.

// cypress/support/index.js

Cypress.on("test:before:run", () => {
  Cypress.automation("remote:debugger:protocol", {
    command: "Emulation.setLocaleOverride",
    params: {
      locale: "en-GB",
    },
  });
});

Source: https://github.com/cypress-io/cypress/issues/7890

Subscribe for Exclusives

My monthly newsletter shares exclusive articles you won't find elsewhere, tools and code. No spam, unsubscribe any time.