Posted almost 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 ($('body').attr('data-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

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).

Owner of this card:

Henning Koch
Last edit:
over 1 year ago
by Henning Koch
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