简体   繁体   English

Javascript在Rails Rspec Capybara测试中停止工作

[英]Javascript stopped working in Rails Rspec Capybara tests

I have the following setup in my system.rb helper file: 我的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

I am using Rails 5.2, selenium-webdriver 3.141.0, capybara 2.18, chromedriver-helper 2.1.0. 我正在使用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. js在测试过程中不会触发,从而导致测试失败。 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. 所以我不知道为什么javascript在测试中不会触发。 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 将水豚更新为3.26删除了chromedriver-helper安装的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. 但是,在测试中触发js的问题仍然存在。 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. 浏览器打开页面加载,但是当单击按钮或应该触发某些js的元素时,没有任何反应。 The code works as I've done this manually. 代码就像我手动完成的那样工作。 I still can't figure out why the js won't fire in the test. 我仍然不知道为什么js在测试中不会触发。

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. 我对未触发的JS进行了一些检查,并意识到在页面完全加载了Turbolink之后,我还没有加载JS文件。

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. 虽然这不是问题的原因,但是建议我保持最新状态是合理的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM