Capybara: has_content? and new lines

Created on 10 Mar 2010  Â·  9Comments  Â·  Source: teamcapybara/capybara

BTW, http://github.com/keithpitt actually posted this, but I forgot to log out of the work account as I wrote this :D

Take the following html:

<div>
<strong>Keith</strong>
 is writing a bug report
</div>

The following cucumber test fails.

And I should see "Keith is writing a bug report"

I've nailed it down to the new line "n". The original webrat cucumber tests solve this problem by doing something like this:

html.inner_text.gsub(/\s+/, ' ').include?("Keith is writing a bug report"")

Where are the capybara cucumber steps use XPath - which from what I can see - screw up when there is a new line.

I've solved it by doing this:

module Capybara
  class Session

    # The Capybara definition of has_content uses XPath, which works for
    # the most part, but it falls over when the content you are testing
    # has a line break in it. Here I'm reimplementing the has_content?
    # methods to go directly to the browser text, and I'm also stripping
    # out new lines.
    def has_content?(content)
      if driver.kind_of?(Capybara::Driver::Culerity)
        driver.browser.text.gsub(/\s+/, ' ').include?(content)
      else
        driver.html.inner_text.gsub(/\s+/, ' ').include?(content)
      end
    end

    def has_no_content?(content)
      !has_content?(content)
    end

  end
end

Its not a great solution, but its fixed for my local version.

All 9 comments

I acknowledge that this is a problem, but I don't think your solution is the right workaround. With the current implementation of scopes, this is actually a bit tricky. The current implementation has weaknesses any way, and I might get around to fixing it at some point, but for the moment, I'm reluctant about changing anything here. I'm closing this issue for now. If you can provide a good patch, please reopen the ticket and I'll consider merging it.

Aww, shut down. I know my solution was crappy. I was hoping to open a discussion for other alternatives fixes because the majority of my tests are failing. I want to move to Capybara but this bug is a deal breaker for me.

I'm happy to submit another, correct patch, but before I do I'll need you to point me in the right direction.

I actually just took a look at the XPath spec and found the beautiful gem that is the normalize-space function. Turns out this was actually fairly easy to implement!

Share? ;)

I was hoping for GitHub to add the link for me, but it seems like it doesn't do that if the issue is already closed. Here you go: http://github.com/jnicklas/capybara/commit/dbe7d8ad2251049088f0a4e24398abb7c8584b19

Yay! Cheers for looking into that.

@jnicklas this fix is great but it doesn't work to me!

 And I should see "FOOBAR ver perfil 30,00 €"                     # features/step_definitions/web_steps.rb:238
      expected to find text "FOOBAR ver perfil 30,00 €" in "Salir  \nBuscar:\nImporte de la puja Fecha (hora de España)\nFOOBAR ver perfil\n30,00 €\nviernes, 11/05/18"
bundle list capybara
/home/davidm/.rvm/gems/ruby-2.2.2@project/gems/capybara-3.1.0

@linuxonrails This issue is from 7 years ago -- please ask howto type questions in the mailing list as requested in the README or on stackoverflow

Was this page helpful?
0 / 5 - 0 ratings