Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capybara expect page.selector to_have

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>`
like image 866
Cyril Duchon-Doris Avatar asked Jun 26 '16 21:06

Cyril Duchon-Doris


Video Answer


1 Answers

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.

like image 172
Thomas Walpole Avatar answered Oct 24 '22 14:10

Thomas Walpole