Search code examples
cucumbercapybara

How to use expect in text with line breaks?


I'm doing some automated tests with capybara, cucumber and webdriver.

And I have an objective to know if a text was loaded on a page, however there is a css property in it white-space that breaks the text and the expect cannot find.

My code is like this.

HTML

<p style="white-space: pre-wrap;">
    Some 
    cool 
    text
</p>

Cucumber.Feature

expect(page).to have_selector('p', text: 'Some')
expect(page).to have_selector('p', text: 'cool')
expect(page).to have_selector('p', text: 'text')

I'm looking for something similar to:

#This text will failure in teste
expect(page).to have_selector('p', text: 'Some cool text')

Solution

  • Capybara tries to treat text the way the browser displays it. If the browser is displaying the text on multiple lines then try

    expect(page).to have_selector('p', text: "Some\ncool\ntext")
    

    If that doesn't work you can always use a regex along the lines of

    expect(page).to have_selector('p', text: /Some\s+cool\s+text/)