2011-10-26 33 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更多细节