What's the meaning of page and page.body in Capybara

醉酒当歌 提交于 2019-12-01 22:02:30

page is the current Capybara session - calling #find/#first/#visit/etc is the same as calling page.find(...), page.first(...), etc.

page.body returns the html source of the page.

Most of the time you would not want to be calling matchers on page.body, so 99.9% of the time you should be using

page.should have_content(...) 

or the equivalent expect syntax. This is because calling matchers on page.body actually runs the returned string through a parser and queries against that, rather than querying in the browser you're testing in.

I believe this syntax is deprecated and you should change to:

expect(page).to have_content "some content"

This will check the rendered page for the content you specify. If you are unsure of whether you are rendering the page you expect and want to debug your test, you can use puts page.body to do so.

You can learn more about should and expect here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!