Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Headless Google Chrome - Change Timezone

Is it possible to launch a headless Google Chrome and programatically set a timezone for testing? It doesn't seem to be present as a shell switch.

I'd like to be able to run tests with varying timezones to confirm that the front end is handling timezone conversion properly.

like image 563
Jamie Counsell Avatar asked Jun 09 '17 17:06

Jamie Counsell


People also ask

How do I simulate different time zones in Chrome?

To change the Chrome time zone for testing, follow the steps below: Open DevTools in Chrome -> Open the Console drawer. Click on the three-dotted menu -> Click on More tools -> Sensors. From the Sensors tab, set the location according to your preference and define the specific timezone.

How do I run Chrome in headless mode?

As we have already seen, you just have to add the flag –headless when you launch the browser to be in headless mode. With CLI (Command Line Interface), just write: chrome \<br> – headless \ # Runs Chrome in headless mode. <br> – disable-gpu \ # Temporarily needed if running on Windows.

What is headless version of Chrome?

Headless mode is a functionality that allows the execution of a full version of the latest Chrome browser while controlling it programmatically. It can be used on servers without dedicated graphics or display, meaning that it runs without its “head”, the Graphical User Interface (GUI).


1 Answers

I recently came across this script that solves this issue. It supports a few configuration options. From the code:

# Change this to the spoof a different timezone. Helpful for testing timezone specific client facing content.
export TZ="America/Los_Angeles"

# Permanent directory to store the user-data directory of your 'fresh'
# Chrome configuration.
fresh_dir="$HOME/.fresh-chrome"

# Temporary directory in which to create new user-data directories for
# temporary Chrome instances.
tmp_dir="/tmp"

Interestingly, the TZ variable is not part of the script. Perhaps Chrome reads the TZ environment variable when it starts? I'm not sure, but this works for now.

like image 128
Jamie Counsell Avatar answered Oct 15 '22 18:10

Jamie Counsell