Posted over 8 years ago. Visible to the public. Repeats.

Detect the current Rails environment from JavaScript or CSS

Detecting if a Javascript is running under Selenium WebDriver is super-painful. 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 <body>. E.g., in your application layout:

%body{'data-environment' => Rails.env}

Now you can say in a piece of Javascript:

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

Or in your CSS / Sass:

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

Similar sounding but completely different card: Cucumber: Detect if the current Capybara driver supports Javascript

Once an application no longer requires constant development, it needs periodic maintenance for stable and secure operation. makandra offers monthly maintenance contracts that let you focus on your business while we make sure the lights stay on.

Owner of this card:

Henning Koch
Last edit:
3 months ago
by Thomas Eisenbarth
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more