使用Capybara + Selenium时禁用JavaScript [英] Disabling JavaScript when using Capybara + Selenium
问题描述
我有一个设计为在禁用JavaScript时仍能正常运行的应用程序,因此我想编写一些涵盖这些情况的规范.
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.
我在Capybara中使用Selenium(Firefox),并且正在注册一个禁用JavaScript的新驱动程序(通过Selenium的javascript.enabled
属性)
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
功能规格无法实际禁用JavaScript.在测试期间弹出浏览器窗口并用binding.pry
暂停时,我可以肯定地单击我知道需要JavaScript的项目并看到它们正常工作.
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.
侧面说明:如果我实际上进入Firefox设置并禁用了JavaScript,则测试通过.因此,它似乎继承了我在浏览器中设置的任何配置,而不实际使用注册驱动程序时指定的配置.
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?
谢谢!
推荐答案
注册驱动程序时无法更改javascript.enabled
设置,因为硒将其冻结为true-
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?
这篇关于使用Capybara + Selenium时禁用JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!