我目前正在使用
硒网络驱动程序3.141.0chromedriver助手2.1.0
gem‘rails-assets-sweeteart2’,来源:https://rails-assets.org'宝石"甜蜜警报2-rails">
带轨道5.2
我的水豚设置:
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
require "capybara-screenshot/rspec"
#Use the following to set the screen size for tests
Capybara.register_driver :selenium_chrome_headless do |app|
options = Selenium::WebDriver::Chrome::Options.new
[
"headless",
"window-size=1280x1280",
"disable-gpu" # https://developers.google.com/web/updates/2017/04/headless-chrome
].each { |arg| options.add_argument(arg) }
Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end
我运行以下测试:
require 'rails_helper'
RSpec.describe 'deleting a proofread document using ajax', js: true do
let(:job) { create(:proofreading_job, title: 'Internal Job') }
let(:user) { job.proofreader.user }
it 'can delete a proofread document' 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 'Dashboard'
click_on 'Proofreading Jobs'
click_on 'Current'
click_on 'Internal Job'
click_on 'Upload Proofread Document'
attach_file(I18n.t('proofreader.proofread_document.upload'), Rails.root + 'spec/test_documents/proofread_document/1.docx' , make_visible: true)
accept_alert do
find_button('Upload', disabled: false).click
end
expect(page).to_not have_button('Delete')
end
end
end
然而,测试失败,Rspec通知我:
Capybara::ModalNotFound:
Unable to find modal dialog
然而,我已经手动使用了网页,模态确实显示和工作正常。
如何让Capybara Selenium Chrome无头驱动程序在测试中打开模态?
accept_alert
用于处理系统模式(浏览器在调用window.alert
时默认创建的模式,这些模式实际上不会向页面添加元素(。Sweetalert2是一个JS库,它在页面中插入元素以创建更时尚的"模式"。您不需要将accept_alert
与它们一起使用,您只需与它们交互,就好像它们是页面上的任何其他HTML元素一样。这将意味着类似的东西
....
attach_file(...)
click_button('Upload', disabled: false) # Not sure why you're passing `disabled: false` here since that's the default
within('.swal2-actions') { click_button('the text of the button to accept the "modal"') }
expect(page)....
更新:正如在评论中发现的那样,这个问题的另一个原因是OP设置中没有编译资产,所以JS根本没有启动。当在非无头模式下运行并看到从未显示任何"模态"时,这一点将立即清楚。解决方法取决于正在使用的资产管道及其配置方式,这超出了本问题的范围。