Capybara::Webkit::InvalidResponseError: Javascript failed to execute

Capybara::Webkit::InvalidResponseError: Javascript failed to execute

我将使用 cancan gem 的应用程序从 Rails 3.2 转换为 4.2。谷歌搜索并检查他们的 github 说只需将 gem cancan 替换为 gem cancancan 而不更改任何内容就足够了。这似乎不起作用。我的测试失败:

Failures:

  1) Redactable fields Manual redaction Redacting selected text in body
     Failure/Error:
           page.execute_script <<-EOS
             document.getElementById('notice_#{@name}').focus();
             document.getElementById('notice_#{@name}').select();
           EOS
     
     Capybara::Webkit::InvalidResponseError:
       Javascript failed to execute
     # /Users/siaw23/.rvm/gems/ruby-2.1.9/gems/capybara-webkit-1.11.1/lib/capybara/webkit/browser.rb:305:in `check'
     # /Users/siaw23/.rvm/gems/ruby-2.1.9/gems/capybara-webkit-1.11.1/lib/capybara/webkit/browser.rb:211:in `command'
     # /Users/siaw23/.rvm/gems/ruby-2.1.9/gems/capybara-webkit-1.11.1/lib/capybara/webkit/browser.rb:232:in `execute_script'
     # /Users/siaw23/.rvm/gems/ruby-2.1.9/gems/capybara-webkit-1.11.1/lib/capybara/webkit/driver.rb:80:in `execute_script'
     # /Users/siaw23/.rvm/gems/ruby-2.1.9/gems/capybara-2.7.1/lib/capybara/session.rb:524:in `execute_script'
     # ./spec/support/page_objects/redactable_field_on_page.rb:15:in `select'
     # ./spec/support/page_objects/redactable_field_on_page.rb:22:in `select_and_redact'
     # ./spec/integration/redaction_spec.rb:37:in `block (4 levels) in <top (required)>'

  2) Redactable fields Manual redaction Restoring body from original
     Failure/Error: page.find("#notice_#{@name}").click
     
     Capybara::ElementNotFound:
       Unable to find css "#notice_body"
     # /Users/siaw23/.rvm/gems/ruby-2.1.9/gems/capybara-2.7.1/lib/capybara/node/finders.rb:44:in `block in find'
     # /Users/siaw23/.rvm/gems/ruby-2.1.9/gems/capybara-2.7.1/lib/capybara/node/base.rb:85:in `synchronize'
     # /Users/siaw23/.rvm/gems/ruby-2.1.9/gems/capybara-2.7.1/lib/capybara/node/finders.rb:33:in `find'
     # /Users/siaw23/.rvm/gems/ruby-2.1.9/gems/capybara-2.7.1/lib/capybara/session.rb:699:in `block (2 levels) in <class:Session>'
     # ./spec/support/page_objects/redactable_field_on_page.rb:9:in `unredact'
     # ./spec/integration/redaction_spec.rb:46:in `block (4 levels) in <top (required)>'

登录后可以手动访问正在搜索的元素。这意味着 cancancancancan 都没有成功地将 :admin 权限应用到我在测试中创建的用户。我使用 You are not authorized to access this page. 并收到“您无权访问此页面”。

规范本身如下所示:

require 'rails_helper'
require 'support/notice_actions'

      #lots of irrelevant code

      private

      def visit_redact_notice
        user = create(:user, :admin)
        visit '/users/sign_in'
        fill_in "Email", with: user.email
        fill_in "Password", with: user.password
        click_on "Log in"

        notice = create(:dmca, :redactable)

        visit "/admin/notice/#{notice.id}/redact_notice"

        notice
      end
    end
  end
end

上述规范中的 visit_redact_notice 方法是一切发生的地方。我需要帮助 :)。如果可能有帮助,这是我的回购协议:https://github.com/siaw23/lumendatabase

ability.rb

class Ability
  include CanCan::Ability

  def initialize(user)
    return unless user

    if user.has_role?(Role.submitter)
      can :submit, Notice
    end

    if user.has_role?(Role.redactor)
      grant_admin_access
      grant_redact
    end

    if user.has_role?(Role.publisher)
      grant_admin_access
      grant_redact

      can :publish, Notice
    end

    if user.has_role?(Role.admin)
      grant_admin_access
      grant_redact

      can :edit, :all
      cannot :edit, [User, Role]

      can :publish, Notice
      can :rescind, Notice

      can :pdf_requests, :all
    end

    if user.has_role?(Role.super_admin)
      can :manage, :all
    end
    
    if user.has_role?(Role.researcher)
      can :read, Notice
    end
  end

  def grant_admin_access
    can :read, :all
    can :access, :rails_admin
    can :dashboard
    can :search, Entity
    can :access, :original_files
  end

  def grant_redact
    can :edit, Notice
    can :redact_notice, Notice
    can :redact_queue, Notice
  end
end

您可能想查看的几个问题...

  1. 有一个新版本的设计出来,允许通过功能/集成测试进行身份验证。我总是花费数小时调试实际让分步登录功能正常工作。在 Devise 4.2 中,现在支持 logging in via integration tests。您必须对 Devise 的 RSpec 配置进行一些更改,如上面文档中所述,sign_in 方法才能正常工作。
  2. 如果此时以管理员身份登录仍然无效,我会检查您的 RSpec 配置是否将 use_transactional_fixtures 设置为 true。如果是这样,我强烈建议您让 database_cleaner gem 管理您的测试数据库,并将 use_transactional_fixtures 设置为 false

其中一个或两个应该可以解决您的身份验证问题。我的 猜测 很可能是交易固定装置的问题,因为 Rails 可能会在您完全验证之前清理交易(因此我怀疑 #以上 1 行不通)。