I am using Capybara with cucumber and ruby
env.rb
Capybara.current_driver = :selenium
I have a web site in many languages and I want to verify following element that should have text with minimum length of 100 characters, otherwise, show a message
<p id="user_text">sometext</p>
The text in “user_text” could be in any language, so
(("#user_text").text.length > 99) == true
Element.should have_content?("text") won't work because I don't know the text in "user_text"
Above method is not work and always passes.
How I would verify the length of text and on failure print a message ?
Any answer will be highly appreciated.
Just a clarification:
When you’re calling
Element.shouldyou are really using RSpec predicate matchers, so:Element.should have_content?("text")really just calls the:
Element.has_content?("text")so it does the same thing, but the second examples uses Capybara node matcher.
Going back to your question
You can also use other RSpec matchers in your tests. And since Capybara uses Nokogiri, your Element is really a Nokogiri::XML::Node here. So you can use the
textmethod to get to the content and verify it, like this:Element.text.should match(/^.{99,}$/)This should do what you need. You can use
textorinner_htmlmethods depending on what you want to check.