首页 > 解决方案 > 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

但是有一个站点从控制器重定向到结果。

请问,我怎样才能得到内容?

感谢帮助

标签: rubytestingcapybara

解决方案


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 POSTS to URLs and verify status codes you should be writing request tests (which don’t use Capybara)


推荐阅读