Disabling JavaScript when using Capybara + Selenium

回眸只為那壹抹淺笑 提交于 2020-01-29 17:45:26

问题


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!


回答1:


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?




回答2:


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.



来源:https://stackoverflow.com/questions/37769915/disabling-javascript-when-using-capybara-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!