Detect the current Rails environment from JavaScript or CSS

Detecting if a Javascript is running under Selenium WebDriver is super-painful Show archive.org snapshot . It's much easier to detect the current Rails environment instead.

You might be better of checking against the name of the current Rails environment. To do this, store the environment name in a data-environment of your <html>. E.g., in your application layout:

<html data-environment=<%= Rails.env %>>

Now you can say in a piece of Javascript:

if (document.documentElement.dataset.environment == 'test') {
  // Code that should happen in Selenium tests
} else {
  // Code that should happen for other environments
}

Or in your CSS / Sass:

html[data-environment="test"] {
  * { text-transform: none !important; }
}

See also

Henning Koch Over 12 years ago