capybara icon indicating copy to clipboard operation
capybara copied to clipboard

has_text? and invisible content

Open jonleighton opened this issue 14 years ago • 37 comments

Capybara master has a new has_text? matcher which excludes invisible content such as <title> elements and display: none elements.

Its implementation uses the Node#text method, so the implication is that the behaviour of Node#text has now changed from returning all text to only returning visible text. This presents two problems:

  • Users will have trouble upgrading if they are using Node#text directly, or has_content? (which is now just an alias of has_text?)
  • There is no API for getting e.g. the text of the <title> element. This could be a valid user requirement and was previously supported by the #text method.

I propose adding a #visible_text method and using that to implement has_text?, leaving the implementations of #text and #has_content? as they are in 1.1. (#has_content? can be deprecated but I believe #text remains useful for checking e.g. <title> text.) WDYT?

Related: #522

cc @mkdynamic

jonleighton avatar Oct 07 '12 18:10 jonleighton

+1 for not being able to query <title> after upgrading to 2.0.

find("title").should have_content("some text") now fails.

ugisozols avatar Nov 21 '12 12:11 ugisozols

+1 for troubles with <title> in 2.0

it { should have_selector('title', text: 'Some Title') }

will fail on <head><title>Some Title</title></head>

demetrios avatar Nov 28 '12 15:11 demetrios

Wouldn't you guys rather have an explicit API for testing the page title, rather than changing the behaviour of #text just for this? That seems much more sensible to me.

jnicklas avatar Nov 28 '12 16:11 jnicklas

OK with me...just point me to it.

demetrios avatar Nov 28 '12 16:11 demetrios

There isn't one, because no one has bothered to build one.

jnicklas avatar Nov 28 '12 16:11 jnicklas

Wish I could help...but I've never worked on a gem and would only cause more problems...

demetrios avatar Nov 28 '12 16:11 demetrios

Maybe there should be a way to get content of every tag, even if it is not visible.

Mic92 avatar Nov 28 '12 16:11 Mic92

I think the problem is that the railstutorial makes heavily use of this feature (check the title) and people tend to copy these examples.

Mic92 avatar Nov 28 '12 16:11 Mic92

Yup, that's my problem. I learned TDD & RSpec from railstutorial.org and as such most of my projects carry with them some basic structure gleaned from there. After upgrading projects to 2.0 my title specs are failing everywhere.

FWIW, it's a little confusing to a relative newb like me when this is happening:

  it { should have_selector('title', text: 'Some Title') } # <= fails
  it { should have_selector('title') }                     # <= passes
  it { should have_text('Some Title') }                    # <= passes

…and according to http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Matchers#has_selector%3F-instance_method we should be able to find elements containing text:. Now maybe those docs are for 1.x…but that's not clear either...to me anyway.

If there's an alternative way to easily test the title in a feature spec let me know and I'll go away…leaving you all to continue your great work!

demetrios avatar Nov 28 '12 17:11 demetrios

+1 for not being able to query after upgrading to 2.0.</p> </p> <div class="body-meta"> <div class="owner-info float-right"> <img class="owner-avatar" src="https://avatars.githubusercontent.com/u/666401?u=1092f4a30490e308accf5a5eb291e057500a96e9&v=4" alt="anilali avatar" width="48" height="48" title="Published by a pub.dev verified publisher"/> <span class="w3-opacity w3-small"> Dec 02 '12 07:12 </span> <a class="owner-name" href="http://gitmemories.com/anilali"> anilali </a> </div> </div> <div class="clear-both"></div> </div> <div class="issue-body"> <p> <p>Adding a <code>#has_title?</code> matcher sounds like a decent solution to me. I'll tweet about in and see if I can get someone to step up to do the work.</p> </p> <div class="body-meta"> <div class="owner-info float-right"> <img class="owner-avatar" src="https://avatars.githubusercontent.com/u/1979?v=4" alt="jonleighton avatar" width="48" height="48" title="Published by a pub.dev verified publisher"/> <span class="w3-opacity w3-small"> Dec 02 '12 18:12 </span> <a class="owner-name" href="http://gitmemories.com/jonleighton"> jonleighton </a> </div> </div> <div class="clear-both"></div> </div> <div class="issue-body"> <p> <p>IMO the behavior of 2.0 is completely broken. In 1.1.4 <code>page.find("title").text</code> returns the text, in 2.0 it returns an empty string. Returning an empty string is just wrong when the element does contain text.</p> </p> <div class="body-meta"> <div class="owner-info float-right"> <img class="owner-avatar" src="https://avatars.githubusercontent.com/u/175494?v=4" alt="gucki avatar" width="48" height="48" title="Published by a pub.dev verified publisher"/> <span class="w3-opacity w3-small"> Dec 15 '12 11:12 </span> <a class="owner-name" href="http://gitmemories.com/gucki"> gucki </a> </div> </div> <div class="clear-both"></div> </div> <div class="issue-body"> <p> <p>My original reason for wanting the <code>has_text</code> matcher (#522) was to be able to test specifically for 'text visible to the user'.</p> <p>The title tag <strong>is</strong> visible text, it's shown in the browser chrome.</p> <p>Given that, I think it would be ideal if the title text was included.</p> </p> <div class="body-meta"> <div class="owner-info float-right"> <img class="owner-avatar" src="https://avatars.githubusercontent.com/u/4312?v=4" alt="mkdynamic avatar" width="48" height="48" title="Published by a pub.dev verified publisher"/> <span class="w3-opacity w3-small"> Dec 15 '12 20:12 </span> <a class="owner-name" href="http://gitmemories.com/mkdynamic"> mkdynamic </a> </div> </div> <div class="clear-both"></div> </div> <div class="issue-body"> <p> <p>@gucki: the title tag <em>is</em> an element and it is decidedly <em>not</em> visible by default. Try adding this CSS selector to a page:</p> <pre><code class="language-css">head, title { display: block } </code></pre> <p>Boom, there is the title <em>element</em> and it is quite distinct from the title shown in the browser chrome. This element is not visible, and if we follow the logic that the text of invisible elements is not shown, then it makes sense that the title <em>element</em> has no text. I agree that that's a matter of debate and discussion, but I wouldn't go so far as to say that the current behaviour is broken. It's just a different interpretation.</p> <p>I think the solution to this problem is two-fold:</p> <ol> <li>Add some kind of other method which returns the text regardless of if it's visible or not. This could be an option to <code>#text</code> or a separate method.</li> <li>Add an official API for retrieving the title of the page and querying it. Some work on this has already been done on this and I will take a closer look as soon as I have the time.</li> </ol> </p> <div class="body-meta"> <div class="owner-info float-right"> <img class="owner-avatar" src="https://avatars.githubusercontent.com/u/134?v=4" alt="jnicklas avatar" width="48" height="48" title="Published by a pub.dev verified publisher"/> <span class="w3-opacity w3-small"> Dec 15 '12 21:12 </span> <a class="owner-name" href="http://gitmemories.com/jnicklas"> jnicklas </a> </div> </div> <div class="clear-both"></div> </div> <div class="issue-body"> <p> <p>@jnicklas Well, it is visible :) <img src="http://cl.ly/image/0g2E462s3Z3U/Screen%20Shot%202012-12-15%20at%202.34.51%20PM.png" alt=""></p> <p>I do think it makes more semantic sense for <code>has_text</code> to include text from the title element. Since the whole idea behind <code>has_text</code> is to move away from the logical model of XML's notion of text and towards the notion of human readable text.</p> <p>I think your suggested solution for #1 and #2 are a good idea, regardless.</p> </p> <div class="body-meta"> <div class="owner-info float-right"> <img class="owner-avatar" src="https://avatars.githubusercontent.com/u/4312?v=4" alt="mkdynamic avatar" width="48" height="48" title="Published by a pub.dev verified publisher"/> <span class="w3-opacity w3-small"> Dec 15 '12 22:12 </span> <a class="owner-name" href="http://gitmemories.com/mkdynamic"> mkdynamic </a> </div> </div> <div class="clear-both"></div> </div> <div class="issue-body"> <p> <p>@jnicklas As you say, the elemenI itself is not visible - not only its content. So think it'd be more intuitive if <code>page.find(element)</code> would return <code>nil</code> if the element is not visible. But when <code>page.find(element)</code> returns an element, it should properly return its attributes and content as they are - no matter if it's visible or not.</p> </p> <div class="body-meta"> <div class="owner-info float-right"> <img class="owner-avatar" src="https://avatars.githubusercontent.com/u/175494?v=4" alt="gucki avatar" width="48" height="48" title="Published by a pub.dev verified publisher"/> <span class="w3-opacity w3-small"> Dec 16 '12 10:12 </span> <a class="owner-name" href="http://gitmemories.com/gucki"> gucki </a> </div> </div> <div class="clear-both"></div> </div> <div class="issue-body"> <p> <p>It seems a bit unintuitive to me too that page.find("title").text shouldn't work, but if it's architecturally messy to make it work, then we may have to do without it. Jonas is the judge. :)</p> </p> <div class="body-meta"> <div class="owner-info float-right"> <img class="owner-avatar" src="https://avatars.githubusercontent.com/u/524783?v=4" alt="joliss avatar" width="48" height="48" title="Published by a pub.dev verified publisher"/> <span class="w3-opacity w3-small"> Dec 26 '12 16:12 </span> <a class="owner-name" href="http://gitmemories.com/joliss"> joliss </a> </div> </div> <div class="clear-both"></div> </div> <div class="issue-body"> <p> <p>+1 for this problem, and also:</p> <pre><code class="language-html"><html> <head> <title>Title Text</title> </head> <body> <h1>Some heading</h1> <p>Some paragraph</p> </body> </html> </code></pre> <pre><code class="language-ruby">page.has_xpath? "//title" # => true page.has_xpath? "//title", text: "Title Text" # => false page.should have_xpath "//title" #=> true page.should have_xpath "//title", text: "Title Text" #=> Capybara::ExpectationNotMet Exception: expected to find xpath "//title" with text "Title Text." but there were no matches. Also found "", which matched the selector but not all filters. </code></pre> <p>Which indicates the title tag is found, but the text inside it remains invisible.</p> <p>Although a <code>page.title</code> would be a nice shorthand to have, it will only provide support for that one tag. How about meta tags, canonical links, etc.? In my humble opinion, <code>#find</code> etc. can return nothing when content is invisible, but xpaths should return visible and invisible content, so it can be used as a more "advanced" fallback for niche situations..</p> <p>I'd be happy to help code it, once there is agreed on what to do with this btw!</p> <p>Thanks.</p> </p> <div class="body-meta"> <div class="owner-info float-right"> <img class="owner-avatar" src="https://avatars.githubusercontent.com/u/309331?v=4" alt="wearethefoos avatar" width="48" height="48" title="Published by a pub.dev verified publisher"/> <span class="w3-opacity w3-small"> Jan 02 '13 10:01 </span> <a class="owner-name" href="http://gitmemories.com/wearethefoos"> wearethefoos </a> </div> </div> <div class="clear-both"></div> </div> <div class="issue-body"> <p> <p>Would be great to have a method that retrieves the text of an element independent of it's visibility. That method could also be used to retrieve the page title. So in my opinion no need for an extra page-title-method but wouldn't hurt of course.</p> <p>If you keep the behaviour as earlier I would having #text returning always the text and e.g. #visible_text returning only text that is visible.</p> <p>Best regards Tobias</p> </p> <div class="body-meta"> <div class="owner-info float-right"> <img class="owner-avatar" src="https://avatars.githubusercontent.com/u/235900?v=4" alt="agileapplications avatar" width="48" height="48" title="Published by a pub.dev verified publisher"/> <span class="w3-opacity w3-small"> Jan 07 '13 14:01 </span> <a class="owner-name" href="http://gitmemories.com/agileapplications"> agileapplications </a> </div> </div> <div class="clear-both"></div> </div> <div class="issue-body"> <p> <p>+1 can't query title text, but the following works ok:</p> <pre><code><head><title>marflar</title></head> should have_xpath("//title[contains(.,'marflar')]") </code></pre> <p>Is there anything wrong with just doing that?</p> </p> <div class="body-meta"> <div class="owner-info float-right"> <img class="owner-avatar" src="https://avatars.githubusercontent.com/u/56636?v=4" alt="murdoch avatar" width="48" height="48" title="Published by a pub.dev verified publisher"/> <span class="w3-opacity w3-small"> Jan 07 '13 18:01 </span> <a class="owner-name" href="http://gitmemories.com/murdoch"> murdoch </a> </div> </div> <div class="clear-both"></div> </div> <nav> <ul class="pagination"> <li class="page-item disabled" aria-disabled="true"> <span class="page-link">« Previous</span> </li> <li class="page-item"> <a class="page-link" href="http://gitmemories.com/teamcapybara/capybara/issues/844?page=2" rel="next">Next »</a> </li> </ul> </nav> </section> </div> </div> </div> <aside class="detail-info-box"> <h3 class="title pkg-infobox-metadata">Labels</h3> <script type="text/javascript"> atOptions = { 'key' : '6ca19c2b16e3f20a28885886ef99a68e', 'format' : 'iframe', 'height' : 250, 'width' : 300, 'params' : {} }; </script> <script type="text/javascript" src="https://www.highperformanceformat.com/6ca19c2b16e3f20a28885886ef99a68e/invoke.js" ></script> <h3 class="title">Owner</h3> <div class="owner-info flex-items-center d-flex"> <img class="owner-avatar" src="https://avatars.githubusercontent.com/u/1979?v=4" alt="jonleighton avatar" width="48" height="48" title="Published by a pub.dev verified publisher"/> <a class="owner-name" href="http://gitmemories.com/jonleighton"> jonleighton </a> </div> <hr class="color1"> <h3>Other Repo Issues</h3> <div class="packages"> </div> <div style="clear: both"></div> </aside> </div> </div> </main> <footer class="site-footer"> <span>© 2022 Git Memory </span> <a class="link sep" href="http://gitmemories.com/privacy-policy">Policy</a> <a class="link sep" href="http://gitmemories.com/terms">Terms </a> <a class="link sep" href="http://gitmemories.com/contact">Contact </a> <a class="link sep" href="https://exchangetuts.com">Exchangetuts </a> <a class="link sep" href="https://onltools.com/">Onltools </a> <a class="link sep" href="https://serveraide.com/">Serveraide </a> </footer> <script src="http://gitmemories.com/js/app.js?id=a1337f444c840fae34ad"></script> </body> </html>