【问题标题】:LoadError when using Selenium: no such file to load -- spec使用 Selenium 时的 LoadError:没有要加载的文件 -- 规范
【发布时间】:2011-03-16 19:43:57
【问题描述】:

基本上我刚开始学习在 Rails 3 上使用 Selenium,我从 Selenium IDE 开始并生成了 RSpec 格式的脚本

在运行此脚本之前,我已经为 selenium-clientSeleniumselenium-railsselenium-webdriver 安装了 gems

但是当我使用 rspec 命令运行这个脚本时,我得到了

C:/Ruby192/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:58:in `require': no such file to load -- spec (LoadError)
    from C:/Ruby192/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:58:in `rescue in require'
    from C:/Ruby192/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/selenium-client-1.2.18/lib/selenium/rspec/spec_helper.rb:2:in `<top (required)>'
    from C:/Ruby192/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from C:/Ruby192/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from C:/Users/qsang/Desktop/Code/NextBigThing/spec/Selenium/create_new_user.rb:5:in `<top (required)>'
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `load'
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `block in load_spec_files'
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `map'
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/configuration.rb:386:in `load_spec_files'
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/command_line.rb:18:in `run'
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:55:in `run_in_process'
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:46:in `run'
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rspec-core-2.5.1/lib/rspec/core/runner.rb:10:in `block in autorun'

我已经尝试寻找答案,我发现的唯一一个与我接近的案例是Selenium Can't Find 'spec' Folder,目前还没有人回答。

谁能帮帮我,在此先感谢。

【问题讨论】:

    标签: ruby-on-rails-3 selenium rspec


    【解决方案1】:

    问题在于您的 Rspec 版本。 Selenium gems 正在寻找 Rspec 1.2.8 或更高版本,但 Rspec 在 2.0 中对其类结构进行了剧烈更改。 Selenium 正在寻找 Rspec 2 中不再存在的文件、类和方法。如果您卸载当前版本的 Rspec 然后:

    gem install rspec -v 1.3.1
    

    它应该可以解决您的问题。如果您将 Rspec 用于自动化中的其他内容,那么这可能不是一个可行的选择。

    【讨论】:

      【解决方案2】:

      我也遇到了这个问题。但是,我正在尝试独立运行,而不是在 rails3 项目中运行。然而,我正在尝试使用 ruby​​ 1.9.2。

      我实际上是在尝试测试一个我们希望逐步转换为 Rails 的 IIS/ASP 网站。第一步是采用我们完美的“Selenese”测试套件集,将它们导出为 RSpec 并使用 ruby​​ 等运行它们。我是 ruby​​/rails 世界的新手,我觉得这非常令人沮丧。我以为几个小时的工作变成了几天......

      我采用了与您类似的策略。但是,我只是删除了所有 gem 和 require 语句,require "selenium/client" 除外。我还将append_after 重命名为afterappend_after 方法在 selenium/rspec/spec_helper 文件中定义(我检查了 lib)。

      我花了好几个小时在网上搜索,但我找不到关于为什么 require "selenium/rspec/spec_helper" 语句失败的答案 - 似乎有一个叫做 spec 的东西(我认为 RSpec 曾经被调用)丢失。

      我认为它可以工作,但它缺少一些功能。 spec_helper 似乎可以处理捕获系统状态和其他有用的功能。

      我很想知道您在 您的 spec_helper 文件中添加了什么。可以发一下吗?

      【讨论】:

      • 我已经安装了 Rspec,并通过 rails generate rspec:install 在我的项目文件夹下生成了一个 spec 文件夹。 spec_helper 文件包含在这个文件中,我把它放在我的答案stackoverflow.com/questions/5330867/…
      • 感谢 spec_helper 的澄清。虽然我打算使用 cucumber 和 RSpec 来进一步开发这个混合(rails/IIS)站点。我决定采用低技术并使用测试/单元,因为 Selenium 可以转换为测试/单元以及 RSpec。这很好,因为现有的 Selenium 测试从一开始就不是很“类似 RSpec”——相反,它们更像是“测试/统一”;-) 这样我就可以完全独立地运行这些测试,只需 ruby​​ 或将它们放入由 rake 运行的 rails 应用程序结构。
      【解决方案3】:

      首先我发现Cucumber + Webrat + Selenium guide 真的很有用

      其次,我删除了

      require "selenium/rspec/spec_helper"
      require "spec/test/unit"
      

      并添加了

      require 'spec_helper'
      

      其中spec_helper 包含在规范文件夹中

      我还删除了append_after的所有方法

      现在基本上可以运行测试用例了,这不是最好的解决方案,但这是我迄今为止所做的。

      P.S:需要从http://seleniumhq.org/download/下载Selenium服务器并用java -jar selenium-server-standalone-2.0b2.jar运行服务器


      spec_helper

      # This file is copied to spec/ when you run 'rails generate rspec:install'
      ENV["RAILS_ENV"] ||= 'test'
      require File.expand_path("../../config/environment", __FILE__)
      require 'rspec/rails'
      
      # Requires supporting ruby files with custom matchers and macros, etc,
      # in spec/support/ and its subdirectories.
      Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
      
      RSpec.configure do |config|
        # == Mock Framework
        #
        # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
        #
        # config.mock_with :mocha
        # config.mock_with :flexmock
        # config.mock_with :rr
        config.mock_with :rspec
      
        # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
        config.fixture_path = "#{::Rails.root}/spec/fixtures"
      
        # If you're not using ActiveRecord, or you'd prefer not to run each of your
        # examples within a transaction, remove the following line or assign false
        # instead of true.
        config.use_transactional_fixtures = true
      end
      

      【讨论】:

        猜你喜欢
        • 2014-01-16
        • 2011-02-23
        • 2013-12-14
        • 2011-12-19
        • 2011-11-30
        • 2016-05-09
        • 2023-03-03
        • 2014-04-15
        相关资源
        最近更新 更多