ruby - Capybara 302 重定向得到空白页面
问题描述
我正在使用 Capybara 3.11.0 编写测试。我正在使用重定向访问页面,并且我想在重定向后检查页面的内容。Capybara 默认遵循重定向,但我得到一个空结果。
page.driver.post("basket/add/?params=")
page.driver.status_code.should eql 302
page.should have_css(".item-form")
最后一行失败,找不到内容。如果我尝试打印内容,在状态码检查之前我得到
page.driver.status_code
=> 302
print page.html
=> nil
page.body.size
=> 0
但是有一个站点从控制器重定向到结果。
请问,我怎样才能得到内容?
感谢帮助
解决方案
The post
method doesn’t follow redirects, so there is no content to check. Additionally using any page.driver.xxxx
method is usually an indicator that you’re doing something you shouldn’t be doing with Capybara. Capybara is designed to be used in feature/system tests which replicate a users behavior. Users don’t care about specific status codes they only care about what’s shown on the page so you should just be using the normal page.xxx
methods to visit a page, interact with it and check content. If you really want to test POST
S to URLs and verify status codes you should be writing request tests (which don’t use Capybara)
推荐阅读
- algorithm - 在 BX=0 的情况下最大化 AX 的算法
- android - 尝试在android中捕获签名时获取`this.refs.signatureCanvas.takeSnapshotAsync is not a function`
- elasticsearch - 如何在elasticsearch中以排序方式查找条目数
- docker - 试图覆盖 ENTRYPOINT 的参数
- javascript - 使用 JavaScript 通过 Min.Io 上传的文件已损坏
- javascript - 如何过滤对象数组中的记录
- reactjs - Redux Saga 测试计划 - 为提供的调用存根不同的响应
- visual-studio-2008 - 什么是 Visual Studio 2008 接受的用于调试经典 ASP 的 web.config 文件?
- python - 使用 keras 构建变分自动编码器 - 错误:您必须使用 dtype float 和 shape 为占位符张量“encoder_input”提供一个值
- javascript - 如何在反应打字稿中循环遍历数组?