2

我正在使用这里提到的 webdriver-user-agent – http://watirwebdriver.com/mobile-devices/
这是我在试用这个 gem
浏览器时使用的代码:FF/Chrome Ruby:1.9.3 / Selenium:2.30。 0 / 瓦提尔:4.0.2

http_client = Selenium::WebDriver::Remote::Http::Default.new
http_client.timeout = HTTP_TIMEOUT
profile = Selenium::WebDriver::Firefox::Profile.new
device = ENV["DEVICE"]
orientation = ENV["ORIENTATION"]
driver = UserAgent.driver(:browser => :firefox, :agent =>device, :orientation=>orientation)
devices = UserAgent.resolution_for(device,orientation)
UserAgent.resize_inner_window(driver,devices[0],devices[1])
Watir::Browser.new driver

现在执行最后一条语句时,出现以下错误

(STEP) Launching FIREFOX (using web driver user agent)……
browser:
#

undefined method `to_sym’ for # (NoMethodError)
/Users/user/.rvm/gems/ruby-1.9.3-p194/gems/watir-4.0.2/lib/watir/loader.rb:42:in `load_driver_for’
/Users/user/.rvm/gems/ruby-1.9.3-p194/gems/watir-4.0.2/lib/watir/loader.rb:8:in `new’

根据一些调查,问题出现在下面突出显示的行,因为它试图在 selenium webdriver 对象上使用 .to_sym。

def load_driver_for(browser)

if browser && browser.to_sym != :ie && Watir.driver == :classic
Watir.driver = :webdriver
end
Watir.load_driver
end

但是,如果我们添加如下所示的行,则该 gem 将按预期工作。

def load_driver_for(browser)

if “#{ENV["BROWSER"]}”.eql?(“chrome_useragent”)||”#{ENV["BROWSER"]}”.eql?(“firefox_useragent”)
Watir.driver = :webdriver
else
if browser && browser.to_sym != :ie && Watir.driver == :classic
Watir.driver = :webdriver
end
Watir.load_driver
end
end

由于这是我们框架之外的 watir 代码,这不是正确的方法,关于如何避免这种情况的任何建议?

4

1 回答 1

2

当您这样做时,问题是可重现的:

require 'watir'
require 'webdriver-user-agent'
driver = Webdriver::UserAgent.driver(:browser => :chrome, :agent => :iphone, :orientation => :landscape)
browser = Watir::Browser.new driver
browser.goto 'tiffany.com'
browser.url.should == 'http://m.tiffany.com/International.aspx'

您可以通过直接要求 watir-webdriver 而不是通过 watir metagem 来解决此问题。将第一行更改为:

require 'watir-webdriver'
于 2013-03-01T14:32:38.313 回答