3

I have an app that's designed to still be functional when JavaScript is disabled, so I wanted to write some specs that covered those cases.

I'm using Selenium (Firefox) with Capybara and I'm registering an new driver with JavaScript disabled (via Selenium's javascript.enabled property)

# spec/rails_helper.rb
Capybara.configure do |config|
  config.ignore_hidden_elements = true
  config.default_driver = :selenium
end

Capybara.register_driver :disable_js do |app|
  profile = Selenium::WebDriver::Firefox::Profile.new
  profile["javascript.enabled"] = false
  Capybara::Selenium::Driver.new(app, profile: profile)
end


# spec/features/siging_in_spec.rb
context "JavaScript disabled", driver: :disable_js do
  it "user can still sign in" do
    # ...
    # ...
  end
end

The feature specs are failing to actually disable JavaScript. When the browser window pops up during testing and I pause it with binding.pry, I can definitely click around on items I know require JavaScript and see them working.

Side note: If I actually go to my Firefox settings and disable JavaScript, the test passes. So it appears it's inheriting whatever configuration I set in my browser, and not actually using the configuration specified when registering the driver.

Is this the correct approach here, or is there something I missed?

Thanks!

2
  • As an alternative option, try the noscript extension - worked for me long ago. Commented Jun 18, 2016 at 4:44
  • please check this answer Commented Aug 4, 2018 at 1:09

2 Answers 2

4
+50

It's not possible to change the javascript.enabled setting when registering the driver because selenium freezes it at true - https://github.com/SeleniumHQ/selenium/blob/master/javascript/firefox-driver/webdriver.json#L35 - This was done because of issues with trying to use selenium and firefox with JS disabled https://github.com/SeleniumHQ/selenium/issues/635 - and is unlikely to be changed. Can you just run those specific tests with the rack_test driver? or does it not provide enough functionality?

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for identifying the issue! I was wondering why it kept overriding my setting. As for rack-test - I also test both desktop and mobile resolutions for responsiveness tests (so all 4 combinations of JS enabled/disabled and desktop/mobile). For the specific cases where JS is disabled on mobile resolution, rack-test would't suffice because it has no concept of window width since it's just rendering HTML. I would consider Poltergeist or Capybara-webkit, but I believe those two also have similar hurdles in disabling JS
3

Unfortunately, setting profile["javascript.enabled"] = false no longer works.

An alternative is to install a Firefox addon that disables JavaScript. This worked for me with Firefox 45 ESR, selenium-webdriver (2.53.4), and capybara (2.8.1):

profile.add_extension(File.expand_path('../quickjava-2.1.0-fx.xpi', __FILE__))

# Configure the extension to disable JavaScript by default.
profile['extensions.thatoneguydotnet.QuickJava.startupStatus.JavaScript'] = 2

# Disable loading the extension's first-run tab.
profile['extensions.thatoneguydotnet.QuickJava.curVersion'] = '2.1.0'

I evaluated a few different addons, including NoScript and QuickJs, but decided to find a very simple addon that could disable JavaScript by default—QuickJava did the trick. You can download the XPI file here (use Firefox, right click and Save As instead of installing directly): https://addons.mozilla.org/en-US/firefox/addon/quickjava/versions/

You can also see all of the addon's pref settings in the source.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.