I need to validate the presence of some text in a specific part of the page.
expect(page).to
will output too much text, it is especially annoying for the page which hold the terms of agreement, which are always so long.
I'd like to transform expect(page).to(have_text(
into something like expect('#id').to(have_text(...)
I cannot figure out how to select the part of the page referenced by ID. Should I write my expect(page)
inside a within('#id')
?
EDIT : if possible, a solution that works against nested tags
<div id="id">
<span class="nested">...</span>
Text I want to check with have_text()
</div>`
You have multiple choices here, either
section = find(:css, '#id') #the :css may be optional depending on your Capybara.default_selector setting
# or - section = find_by_id('id')
expect(section).to have_text(...)
or
expect(page).to have_css('#id', text: '...')
or
expect(page).to have_selector(:id, 'id', text: '...')
depending on whether or not you need the element for other things.
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