31

Does anybody know how to find if a page has a text more than once?

I know I can use

expect(page).to have_content("my-text")

to check if text appears once. However, I need something like

expect(page).to have_content("my-text").twice

(which doesn't work).

I'm using capybara v2.1.0.

Tonatiuh
  • 2,205
  • 1
  • 21
  • 22

2 Answers2

47
expect(page).to have_content("my-text", count: 2)

will do what you want in modern versions of Capybara, not sure if that will work in 2.1 but worth trying (2.1 is over 2 years old now)

Update: In Capybara 3.19+ this can also now be written

expect(page).to have_content("my-text").twice
Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78
5

When you want the text to show at least 2 times (or 2 times and more) you have to use minimum instead of count.

expect(page).to have_text(/my-text/, minimum: 2)

Panos Angel
  • 136
  • 1
  • 5
  • 4
    Yes, and if you want it at least 2 but <= 5 you can do `expect(page).to have_text(/my-text/, minimum: 2, maximum: 5)` or `expect(page).to have_text(/my-text/, between: (2..5)) – Thomas Walpole Jun 01 '17 at 19:25