[英]Javascript stopped working in Rails Rspec Capybara tests
我的system.rb帮助文件中有以下设置:
require "capybara-screenshot/rspec"
RSpec.configure do |config|
config.before(:each, type: :system) do
driven_by :rack_test
end
config.before(:each, type: :system, js: true) do
driven_by :selenium_chrome_headless
end
end
Capybara.register_driver :selenium_chrome_headless do |app|
options = Selenium::WebDriver::Chrome::Options.new
[
"headless",
"window-size=1280x1280",
"--enable-features=NetworkService,NetworkServiceInProcess",
"disable-gpu"
].each { |arg| options.add_argument(arg) }
Capybara::Selenium::Driver.new(app, browser: :chrome, options: options, clear_local_storage: true, clear_session_storage: true)
end
Capybara::Screenshot.register_filename_prefix_formatter(:rspec) do |example|
"screenshot"
end
Capybara::Screenshot.webkit_options = { width: 1586, height: 768 }
Capybara.javascript_driver = :webkit
Capybara.default_max_wait_time = 4
Capybara::Webkit.configure do |config|
config.allow_url("https://cdnjs.cloudflare.com/ajax/libs/core-js/2.4.1/core.js")
end
我正在使用Rails 5.2,selenium-webdriver 3.141.0,capybara 2.18,chromedriver-helper 2.1.0。
这是一个典型的测试:
require "rails_helper"
RSpec.describe "Queues the Error Accounting Job ", type: :system, js: true do
include ActiveJob::TestHelper
let!(:job) { create(:proofreading_job, :with_start_end_dates, title: "Internal Job", status: 'queued', document: create(:document))}
let!(:user) { job.proofreader_user }
it 'queues the WordCountsJob' do
visit root_path
click_on "Login"
fill_in("Email", with: user.email)
fill_in("Password", with: user.password)
click_on "Sign In"
click_on "Jobs"
page.find(:css, ".clickable-row").click()
click_on "Upload Proofread Document"
attach_file('customFile','/Users/mitchellgould/RailsProjects/ProvenWordNew/spec/test_documents/proofread_document/1.docx', make_visible: true)
find_button('Upload', disabled: false).click
sleep 2.seconds
expect(ErrorAccountingJob).to have_been_enqueued
end
end
js在测试过程中不会触发,从而导致测试失败。 几周前,该测试仍在进行,但我还没有涉及设置。 所以我不知道为什么javascript在测试中不会触发。 当我在应用程序中尝试此功能时,该功能将起作用,因此代码没有任何问题。
任何帮助表示赞赏。
更新:
根据评论中的建议,我更改了以下内容:
将水豚更新为3.26删除了chromedriver-helper安装的webdrivers
重构我的系统帮助文件,如下所示:
require "capybara-screenshot/rspec"
require 'webdrivers'
RSpec.configure do |config|
config.before(:each, type: :system) do
driven_by :rack_test
end
config.before(:each, type: :system, js: true) do
driven_by :selenium_chrome
end
end
Capybara.default_max_wait_time = 4
Capybara::Screenshot.register_filename_prefix_formatter(:rspec) do |example|
"screenshot"
end
Capybara::Screenshot.webkit_options = { width: 1586, height: 768 }
但是,在测试中触发js的问题仍然存在。 浏览器打开页面加载,但是当单击按钮或应该触发某些js的元素时,没有任何反应。 代码就像我手动完成的那样工作。 我仍然不知道为什么js在测试中不会触发。
我对未触发的JS进行了一些检查,并意识到在页面完全加载了Turbolink之后,我还没有加载JS文件。
将代码放在以下内容中进行修复:
$(document).on 'turbolinks:load', ->
$('.clickable-row').on 'click', (e) ->
unless $(e.target).hasClass('btn')
window.location = $(this).data('href'
根据注释中的建议完成了对水豚的更新。 虽然这不是问题的原因,但是建议我保持最新状态是合理的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.