2011-10-26 34 views
28

我有以下規格:如何與水豚+ Selenium測試響應代碼

it "deletes post", :js => true do 
... 
... 
page.status_code.should = '404' 

end 

page.status_code線給我這個錯誤:

Capybara::NotSupportedByDriverError 

如何檢查頁面的狀態碼?

+0

相關:https://groups.google.com/forum/?fromgroups=#!topic/ruby-capybara/KdMvhjcjm8E – tokland

回答

15

status_code目前Selenium驅動程序不支持。您需要編寫一個不同的測試來檢查響應狀態代碼。

3

或者切換到另一個驅動程序(如rack-test)進行該測試,或者測試顯示的頁面是404頁面(在h1中應該有內容'Not Found')。

正如@eugen所說,Selenium不支持狀態碼。

40

順便說一句。這條線

page.status_code.should = '404' 

應該

page.status_code.should == 404 

這與水豚,WebKit的工作對我來說。

+0

另一個水豚,WebKit的用戶是在這裏,但'希望( page.status_code)。(200)'返回'Capybara :: NotSupportedByDriverError'。你有什麼想法,爲什麼? – scaryguy

2

硒web驅動程序doest沒有實現status_code,並沒有直接的方式來測試selenium response_code(開發人員的選擇)。

<html code="<%= response.try(:code) if defined?(response) %>">[...]</html> 

然後在我的測試:

def no_error? 
    response_code = page.first('html')[:code] 
    assert (response_code == '200'), "Response code should be 200 : got #{response_code}" 
end 
3

試試吧

expect(page).to have_http_status(200) 
+5

嗨Ajay;你的代碼可能是正確的,但是在某些情況下,它會作出更好的回答;例如,您可以解釋如何以及爲什麼這個提議的變更可以解決提問者的問題,或許還包括指向相關文檔的鏈接。這將使它對他們更有用,而且對尋求類似問題解決方案的其他網站讀者也更有用。 –

1

使用

爲了測試我在佈局/ application.html.erb添加js提出請求並獲得如下狀態:

js_script = <<JSS 
xhr = new XMLHttpRequest(); 
xhr.open('GET', '#{src}', true); 
xhr.send(); 
JSS 
actual.execute_script(js_script) 
status = actual.evaluate_script('xhr.status') # get js variable value 

檢查https://gist.github.com/yovasx2/1c767114f2e003474a546c89ab4f90db更多細節