我需要从 Ruby on Rails 框架运行 Watir 测试代码。我的 ruby on rails hello world 运行良好,我的 Watir 测试也是如此(当我从 irb 运行并作为单独的 ruby 脚本运行时),但我的要求是从 rails 控制器中调用 watir 代码,这给出了一个无法加载 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