使用 Capybara + Selenium 时禁用 JavaScript [英] Disabling JavaScript when using Capybara + Selenium

查看:22
本文介绍了使用 Capybara + Selenium 时禁用 JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序设计为在禁用 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 设置,因为 selenium 将其冻结为 true - https://github.com/SeleniumHQ/selenium/blob/master/javascript/firefox-driver/webdriver.json#L35 - 这样做是因为在禁用 JS 的情况下尝试使用 selenium 和 firefox 时出现问题 https://github.com/SeleniumHQ/selenium/issues/635 - 不太可能改变.你能用 rack_test 驱动程序运行那些特定的测试吗?还是没有提供足够的功能?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆