如何使用 Capybara 测试响应代码 +硒
我有以下规范:
it "deletes post", :js => true do
...
...
page.status_code.should = '404'
end
page.status_code
行给我这个错误:
Capybara::NotSupportedByDriverError
如何检查页面的状态代码?
I have the following spec:
it "deletes post", :js => true do
...
...
page.status_code.should = '404'
end
The page.status_code
line is giving me this error:
Capybara::NotSupportedByDriverError
How do I check the page's status code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
作为旁白。这行
应该是
这对我来说与 capybara-webkit 一起工作。
As an aside. This line
Should be
This worked for me with capybara-webkit.
Selenium 驱动程序当前不支持
status_code
。您将需要编写不同的测试来检查响应状态代码。status_code
is not currently supported by the Selenium driver. You will need to write a different test to check the response status code.Selenium Web 驱动程序不实现 status_code,并且没有直接方法使用 selenium 测试 response_code(开发人员的选择)。
为了测试它,我在我的layout/application.html.erb中添加:
然后在我的测试中:
Selenium web driver doest not implement status_code and there is no direct way to test response_code with selenium (developer's choice).
To test it I added in my layout/application.html.erb:
And then in my test:
要么切换到另一个驱动程序(如rack-test)进行该测试,要么测试显示的页面是否为404页面(h1中应包含“未找到”内容)。
正如 @eugen 所说,Selenium 不支持状态代码。
Either switch to another driver (like
rack-test
) for that test, or test that the displayed page is the 404 page (should have content 'Not Found' in h1).As @eugen said, Selenium doesn't support status codes.
尝试一下
Try it out
使用js发起请求并获取状态如下:
检查 https://gist.github.com/yovasx2/1c767114f2e003474a546c89ab4f90db了解更多详情
Use js to make a request and get status as below:
Check https://gist.github.com/yovasx2/1c767114f2e003474a546c89ab4f90db for more details
是 Ajay 的 回答,如果您更喜欢状态符号而不是代码值,以提高可读性。
参考文献:
对于所有可用的状态代码/符号:
is an acceptable variation to Ajay's answer, if you prefer the status symbol over the code value, for readability.
References:
For all available status codes / symbols: