2013-05-17 43 views
1

我是RubyMine的新手,試圖調試一個我自己沒寫過的項目。RubyMine - NoMethodError:未定義的方法'get'for nil:nilClass - Webdriver UserAgent

運行Project.feature時發生錯誤,並且它停在第一行「由於我訪問本網站」。

"NoMethodError: Undefined method 'get' for nil:nilClass" in filepath/Common_steps_json.rb:14 in '/^I go to this website$' 

要提供的鏈接,它指引我到我的Common_steps_json.rb文件,其中寫的代碼是

Given /^I go to this website$/ do 
@driver.get 'https://www.somewebsite.com/field1/field2/' 
sleep 3 
end 

我相信這是一個使用硒的webdriver導航到一個URL的正確格式。所以這促使我檢查環境文件env.rb以查看驅動程序是如何被調用的。我在那裏找到了。

require "selenium-webdriver" 
require "webdriver-user-agent" 
. 
. 
. 
Before('@driver_iphone_portrait') do 
@driver = UserAgent.driver(:browser => :firefox, :agent => :iphone, :orientation =>  :portrait) 
@base_url = 'https://www.somewebsite.com/' 
@driver.manage.timeouts.implicit_wait = 2 
@verification_errors = [] 
end 

我檢查這個link以確保硒webdriver的用戶代理正確地叫,但我不能肯定它是。我用交互式Ruby來看看我是否能使用UserAgent.Driver格式調用驅動程序,但它遇到了一個錯誤

NameError: uninitialized constant UserAgent 

如果任何人有關於如何正確格式的UserAgent的RubyMine的任何意見,我將不勝感激。謝謝!

回答

2

它看起來像你的代碼試圖運行一些測試使用Firefox與配置文件,使用用戶代理屬性來模擬iPhone。

不幸的是,您的代碼使用的是由Alister Scott編寫的 depreciated gem。你將無法繼續使用這顆寶石,應該尋找解決辦法。

UPDATE阿利斯特·斯科特just announced that the maintenance of the gem has been taken over by Jeff "Cheezy" Morgan,並提供對Github repo


事實上,它看起來像這是一個非常簡單和標準的寶石包裝,你可以聘請沒有所有的含糖包裝善良。其實,你可以輕鬆地在WebDriver Profile setup

嘗試這樣配置此設置更換的UserAgent字符串(並採取了require 'webdriver-user-agent'太行):

profile = Selenium::WebDriver::Firefox::Profile.new 
profile["general.useragent.override"] = "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3" 
@driver = Selenium::WebDriver.for :firefox, :profile => profile 

應會產生@driver對象,並允許您使用get方法使用Firefox瀏覽器打開網站。瀏覽器應該在代碼中指定用戶代理的請求頭中發送,並且您的網頁應該像iPhone正在瀏覽時那樣呈現。

+1

謝謝bgoad!我不知道代碼中使用的寶石是貶值的。該解決方法做了一個輝煌的工作,設置@driver以顯示Iphone會顯示的網頁。 –

相關問題