Capybara :: Webkit :: InvalidResponseError:Javascript无法执行

时间:2016-08-03 15:24:40

标签: ruby-on-rails capybara cancan capybara-webkit cancancan

我将应用程序从Rails 3.2转换为使用cancan gem的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 个答案:

答案 0 :(得分:1)

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

  1. 有一个新版本的Devise out允许通过功能/集成测试进行身份验证。我总是花费数小时调试实际上逐步登录功能。使用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
  3. 其中一个或两个应该可以解决您的身份验证问题。我的猜测是它最有可能成为交易固定装置的问题,因为Rails可能会在您完全通过身份验证之前清理交易(因此我怀疑为什么上面的#1没有工作)。