2011-08-30 127 views
2

我試圖設置一個Jquery移動應用程序的黃瓜測試,但測試點擊是拋出一個意外的錯誤。下面是這種情況:點擊鏈接使用水豚/黃瓜

problems.feature

Scenario: Clicking problem when not signed in 
    When I go to the wall page for The Beast 
    Then I should see "Blue" 
    When I click the problem "Blue" 
    Then I go to the sign up page 

和實施,以捕捉到倒數第二行的相關步驟如下:

steps.rb

When /^I click the problem "(.*)"$/ do |problem_name| 
    find(:xpath, "//h3[text() = '#{problem_name}']/parent::a").click 
end 

一切直到這一點才能通過。當它到達點擊這個問題,下面的錯誤被拋出:

When I click the problem "Blue" 
    You have a nil object when you didn't expect it! 
    You might have expected an instance of Array. 
    The error occurred while evaluating nil.each (ActionView::Template::Error) 

我也曾嘗試立即查找之前,包括一個調試器()點擊steps.rb調用。當我再手動運行find命令,它確實返回以下內容:

(rdb:1) p find(:xpath, "//h3[text() = 'Blue']/parent::a") 
    #<Capybara::Element tag="a" path="/html/body/div/div[2]/ul/li[2]/a"> 

然後在調試器就完了「單擊」運行產生如下:

(rdb:1) p find(:xpath, "//h3[text() = '#{problem_name}']/parent::a").click 
    ActionView::Template::Error Exception: You have a nil object when you didn't expect it! 
    You might have expected an instance of Array. 
    The error occurred while evaluating nil.each 
(rdb:1) p find(:xpath, "//h3[text() = '#{problem_name}']/parent::a").click 
    nil 

所以似乎水豚可以找到正確的元素,但當試圖點擊它設置爲零?我想知道如何正確執行我正在使用Capybara執行的點擊。

+0

已嘗試使用:「然後顯示頁面」步驟來調試問題。 –

回答

2

我不認爲水豚有問題。現在我有同樣的問題,我認爲問題在於,您需要使用驅動程序來測試諸如硒的JavaScript操作,並使用此驅動程序運行測試。

我認爲你的按鈕正在執行動作並刷新頁面而沒有它需要的所有對象。這不會發生如果您使用JavaScript

+0

你的想法正確 - 特別是你需要在每個場景上方的行中添加'@javascript'。 – IanWhalen