I have the following setup in my system.rb helper file:
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
I am using Rails 5.2, selenium-webdriver 3.141.0, capybara 2.18, chromedriver-helper 2.1.0.
Here is a typical test:
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
The js does not fire during the test which causes the test to fail. This same test was working a few weeks ago and I have not touched the setup. So I have no idea why the javascript will not fire in the tests. This feature works when I try it in the application so there are no problems with the code.
Any help appreciated.
Update:
As per the suggestions in the comments I have changed the following:
Updated Capybara to 3.26 Removed chromedriver-helper Installed webdrivers
Refactored my System helper file as follows:
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 }
However, the problem getting the js to fire in the tests persists. The browser opens the pages load but when it clicks on a button or on an element that is supposed to fire some js nothing happens. The code works as I've done this manually. I still can't figure out why the js won't fire in the test.
I did some checking of the JS that was not firing and realized that I had not loaded the JS files after the page was fully loaded with turbolinks.
Putting the code inside the following fixed it:
$(document).on 'turbolinks:load', ->
$('.clickable-row').on 'click', (e) ->
unless $(e.target).hasClass('btn')
window.location = $(this).data('href'
Updating the capybara was done on the advise in the comments. While that was not the reason for the issue it was sound advise to keep my gems up to date.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.