Posted almost 7 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:

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

Now you can say in a piece of Javascript:

Copy
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:

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

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

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Author of this card:

Avatar
Henning Koch
Last edit:
3 months 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