cucumbercapybararspec-expectations

New line characters in Cucumber Capybara


I'm using Cucumber with Capybara. I've noticed that suddenly, after a bundle install, that I'm getting failed tests now where new line characters are appearing in strings as part of the text.

Example error:

RSpec::Expectations::ExpectationNotMetError: expected to find text "Longview Road Clase Swansea SA6 7JL" in "Skip to main content\nGOV.UK\nDigital tachograph card\n......."

In the past, those new line characters weren't there. I'm struggling to track down which gem is causing this.

Is there a way to stop this without needing to do a strip on every string of text I extract from the webpage?

Some gem versions:

Capybara - 2.18 Rspec-expectations - 3.7.0 Cucumber - 2.4.0


Solution

  • Are you sure you're on Capybara 2.18? This is a behavior that changed in Capybara 3 where drivers are now expected to return text closer to what is displayed to the user - https://github.com/teamcapybara/capybara/blob/master/UPGRADING.md#node. If you really aren't using Capybara 3.x then you probably updated whatever driver you're using with Capybara and it is no longer supporting Capybara 2.x behavior.

    If you are now on Capybara 3.x then you'll either need to change your tests to check for what is actually displayed (which is a more correct checking of things on the page) or you can use Capybara 3.5+ release which adds a normalize_ws option to the text/content matchers so you can write

    expect(element).to have_text('Longview Road Clase Swansea SA6 7JL', normalize_ws: true)
    

    to replicate more 2.x like behavior