水豚中page和page.body的含义是什么 [英] What's the meaning of page and page.body in Capybara
问题描述
我是新手,尝试使用Capybara测试我的Rails项目,但是当我尝试从div中检测某些字符串时,我对page和page.body的含义感到困惑:(in:js => true模式)
I'm a newbie try to test my Rails project using Capybara, but I'm confused with the meaning of page and page.body, when I try to detect some string from my div: (in :js=>true mode)
<div>"some content"</div>
我的一些考试会通过
page.should have_content "some content"
有些人会通过
page.body.should have_content "some content"
我尝试放入内容,但是只有"page.body"会给我一些有价值的信息,"page"本身不会给我任何东西,而且我找不到关于page和page.body所做的任何可靠解释. .谁能帮我吗?
I try to puts the content but only "page.body" will give me some valuable information, the "page" itself will show me nothing, and I can't find any solid explanation about what page and page.body did. Can anyone help me?
推荐答案
页面是当前的Capybara会话-调用#find/#first/#visit/etc与调用page.find(...),页面相同.first(...),等等.
page is the current Capybara session - calling #find/#first/#visit/etc is the same as calling page.find(...), page.first(...), etc.
page.body返回页面的html源.
page.body returns the html source of the page.
大多数情况下,您不想在page.body上调用匹配器,因此99.9%的时间应该使用
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(...)
或等效的Expect语法.这是因为在page.body上调用匹配器实际上是通过解析器运行返回的字符串并对其进行查询,而不是在您要测试的浏览器中进行查询.
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.
这篇关于水豚中page和page.body的含义是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!