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

Detect the current Rails environment from JavaScript

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 }

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.

Author of this card:

Henning Koch
Last edit:
6 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