gpt4 book ai didi

ruby-on-rails - 如何在 Capybara/Poltergeist 中测试 JavaScript 错误

转载 作者:行者123 更新时间:2023-12-04 12:47:20 26 4
gpt4 key购买 nike

我需要检查页面是否存在 JavaScript 错误。

capybara-webkit的解决方案
http://blog.55minutes.com/2013/10/test-javascript-with-capybara-webkit/

require 'spec_helper'feature 'Home' do  it 'should not have JavaScript errors', :js => true do    visit(root_path)    expect(page).not_to have_errors  endend

How to make one look the same as for Poltergeist?

spec_helper.rb

...
require 'capybara/rails'
require 'capybara/selenium/driver'
...


selenium_hub_host = "selenium"
selenium_hub_port = "100"
selenium_url = "http://#{selenium_hub_host}:#{selenium_hub_port}/wd/hub"

...
Capybara.register_driver :selenium_remote do |app|
options = {}
options[:browser] = :remote
capabilities = Selenium::WebDriver::Remote::Capabilities.firefox
capabilities[:platform] = :any
capabilities[:takes_screenshot] = true
options[:url] = selenium_url
options[:desired_capabilities] = capabilities
Capybara::Selenium::Driver.new(app, options)
end

Capybara.javascript_driver = :selenium_remote
Capybara.default_max_wait_time = 30

Capybara.server do |app, port|
require 'rack/handler/thin'
Rack::Handler::Thin.run(app, :Host => '0.0.0.0', :Port => port)
end

...

最佳答案

试试这个配置:

Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new( app, {
debug: true, # turn on poltergeist debug mode
js_errors: true, # turn on javascript errors on page
timeout: 10000,
phantomjs_options: ['--load-images=yes', '--ignore-ssl-errors=yes', '--ssl-protocol=any']
})
end

Capybara.javascript_driver = :poltergeist
Capybara.current_driver = :poltergeist
Capybara.default_wait_time = 5
Capybara.server_port = '3000'
Capybara.app_host = "http://127.0.0.1:3000"

如果不需要,请关闭加载图像和 poltergeist Debug模式。

顺便说一句,Capybara 不包括 have_errors匹配器。要使用该匹配器,您需要使用 capybara-webkit gem/driver 而不是 selenium

如果你使用 PhantomJS/Poltergeist 和 Capybara 来运行你的测试,它会导致测试失败并输出任何错误(包括 JS 错误)......

除此之外,它还将输出 JS 警告,这不会使测试失败,但仍然可以让您看到站点中的困惑……

如果 JS 错误对您正在测试的产品很重要,我建议将它与 teaspoon 一起使用...

关于 Selenium WD,鉴于有特定的工具可以做到这一点,因此在页面上监视 JS 错误有点超出范围...

关于ruby-on-rails - 如何在 Capybara/Poltergeist 中测试 JavaScript 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29111566/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com