Search code examples
rspeccapybara

Capybara Rspec has content twice


I'm a novice hobbyist, trying to write my first rspec ever for a 2017 rails project that use capybara 2.10.1 and rspec.

I got launchy, did save_and_open_page, ctrl-shift-c, right-clicked a link to find the xpath for some deep random bootstrap navbar log-in link:

scenario "valid with correct credentials", focus: true do
    expect(page).to have_link "LOG IN"
    find('#myNavbar > ul.nav.navbar-nav.navbar-right.signlogin > li:nth-child(2) > a').click
    expect(page).to have_content("Sign in").twice
    fill_in "Email", with: @fan.email
    fill_in "Password", with: @fan.password
    click_button "Sign in"

    find('.dropdown-toggle').click
    expect(page).to have_link "Logout"
    expect(page).to have_current_path root_path
  end

but, assuming because it's capybara 2.10.1, have_content("").twice gives me

Capybara::Ambiguous:
       Ambiguous match, found 2 elements matching xpath "/html"

although i tried updating capybara but with the same result.

I could find something else unique to expect on the page, but have been unsuccessfully trying to solve it with google (a capybara apparently is twice the size of a beaver and is the largest rodent in the world).

any way to solve this?


Solution

  • .twice support was released in Capybara 3.19.0 version.

    With older version of capybara 2.10.1, you can use following.

    expect(page).to have_content("Sign in", count: 2)