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.
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.
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With