1

Ruby on Rails フレームワークから Watir テスト コードを実行する必要があります。私の ruby​​ on rails hello world は完璧に動作し、Watir テストも同様です (irb から実行する場合と、個別の ruby​​ スクリプトとして実行する場合)。 Watir エラーを読み込めません。

この要件は非常に緊急であり、エラーの内容がわかりません。

注: Rails アプリケーションは netbeans 6.9 IDE で実行され、システムでネイティブの Ruby インタープリターを使用します (デフォルトの jruby ではありません)。

私は次のバージョンを持っています: ruby​​: 1.9.3 watir: 3.0.0 Rails: 3.2.8

LoadError (cannot load such file -- Watir):
  app/controllers/watir_controller.rb:4:in `<class:WatirController>'
  app/controllers/watir_controller.rb:1:in `<top (required)>'

これは、watir スクリプトを呼び出すコントローラー クラスです。

class WatirController < ApplicationController

## the Watir controller
require 'rubygems'
require 'watir'
# set a variable
test_site = "http://www.google.com"
# open the IE browser
ie = Watir::IE.new
# print some comments
puts "Beginning of test: Google search."
puts " Step 1: go to the test site: " + test_site
ie.goto test_site
puts " Step 2: enter 'pickaxe' in the search text field."
ie.text_field(:name, "q").set "pickaxe" # "q" is the name of the search field
#puts " Step 3: click the 'Google Search' button."
ie.button(:name, "btnG").click # "btnG" is the name of the Search button
puts " Expected Result:"
puts "  A Google page with results should be shown. 'Programming Ruby' should be high on the list."
puts " Actual Result:"
if ie.text.include? "Programming Ruby"
  puts "  Test Passed. Found the test string: 'Programming Ruby'. Actual Results match Expected Results."
else
  puts "  Test Failed! Could not find: 'Programming Ruby'."
end
puts "End of test: Google search."
end
4

2 に答える 2

0

Railsコントローラー内でWatirを使用する必要がある理由がわかりません。

Watirは、ブラウザの自動化でテストを行うためのものです。つまり、これをtest / specディレクトリに配置し、watirをGemfileの開発およびテストグループに次のように配置する必要があります。

group :development, :test do
  gem "watir"
end

Railsを使用していて、おそらくRailsで記述されたアプリケーションをテストするので、watir-railsを使用することもお勧めします。また、RSpecを使用している場合はwatir-rspecを使用します。これは、Gemfileが次のようになることを意味します。

group :development, :test do
  gem "watir-rails"
  gem "watir-rspec"
end
于 2012-09-28T11:05:39.627 に答える
-1

gem ファイルに含める必要があります。

Gemfile

gem 'watir'

コントローラ

class MyApp::WebCrawlerController
    require "watir"
    require "nokogiri"

    def index
      browser = Watir::Browser.new
      browser.goto 'http://www.google.com'
      doc = Nokogiri::HTML.parse(browser.html)
    end
end
于 2016-07-20T01:56:18.167 に答える