B S*_*ven 15 ruby tdd rspec ruby-on-rails capybara
以下按预期工作:
within('h2', text: 'foo') do
should have_content 'bar'
end
Run Code Online (Sandbox Code Playgroud)
我试图在父元素内检查,使用 find(:xpath, '..')
找到元素后,如何应用.find(:xpath, '..'),然后检查该元素中的某些内容?
And*_*lov 19
当你在里面使用XPath定位器时,within它应该以.(如果不是从. 搜索开始不是在.myclass整个文档内但在整个文档内)开始.
例如:
within('.myclass') do
find(:xpath, './div')
end
Run Code Online (Sandbox Code Playgroud)
要么:
find('.myclass').find(:xpath, './div')
Run Code Online (Sandbox Code Playgroud)
来自@ BSeven的答案的代码可以写成一行:
expect(find("//h2[text()='foo']/..")).to have_text('bar')
Run Code Online (Sandbox Code Playgroud)
要么
expect(page).to have_xpath("//h2[.='foo']/..", text: 'bar')
Run Code Online (Sandbox Code Playgroud)
在2.11之后使用Rspec的新语法,应该是;
within('h2', text: 'foo') do |h|
expect(h).to have_content 'bar'
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12551 次 |
| 最近记录: |