The following works as expected:
within('h2', text: 'foo') do
should have_content 'bar'
end
I am trying to check within the parent element, using find(:xpath, '..')
Once you find an element, how to apply .find(:xpath, '..')
, and then check for something within that element?
When you use XPath locator inside within
it should start with .
(if it doesn't start with .
the search is done not within .myclass
but within the whole document).
E.g.:
within('.myclass') do
find(:xpath, './div')
end
or:
find('.myclass').find(:xpath, './div')
Code from @BSeven's answer can be written in one line:
expect(find("//h2[text()='foo']/..")).to have_text('bar')
or
expect(page).to have_xpath("//h2[.='foo']/..", text: 'bar')
With the new syntax of Rspec after 2.11, it should be;
within('h2', text: 'foo') do |h|
expect(h).to have_content 'bar'
end
The following is one approach:
within('h2', text: 'foo') do
within(:xpath, '..') do
should have_content 'bar'
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With