Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
has_no_link search is unexpectedly broad
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
mcbsys  
View profile  
 More options Sep 14 2012, 8:22 pm
From: mcbsys <mcbsyst...@gmail.com>
Date: Fri, 14 Sep 2012 17:22:09 -0700 (PDT)
Local: Fri, Sep 14 2012 8:22 pm
Subject: has_no_link search is unexpectedly broad

Hi,

According to the doc<http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Mat...>,
has_no_link "Checks if the page or current node has no link with the given
text or id."

After extended time with the debugger, I found out that the reason my test
was failing is because has_no_link also searches for the text in the title
and img (alt) attributes (source<https://github.com/jnicklas/xpath/blob/master/lib/xpath/html.rb#L14>).
So this test:

   it { should_not have_link("Number 3, Contact") }

became this xpath:

   .//a[./@href][(((./@id = 'Number 3, Contact'
     or normalize-space(string(.)) = 'Number 3, Contact')
     or ./@title = 'Number 3, Contact')
     or .//img[./@alt = 'Number 3, Contact'])

I wanted to make sure that my page does not have this link:

   <a href="/contacts/5">Number 3, Contact</a>

But because it *does* have this Delete link, it found the text in title,
and the test failed:

  <a href="/contacts/5" data-confirm="Are you sure?" data-method="delete"
   rel="nofollow" title="Delete Number 3, Contact">Delete</a>

My workaround is to write the xpath myself:

   it { should_not have_xpath("//a[contains(text(), 'Number 3,
Contact')]/@href") }

When I read "has_link", I think of specifying the actual, underlined, blue *
link*. I would rather specify qualifiers (id, title, img-alt, href) as
additional options. Probably too late for that discussion, but it would
help if the docs listed *all* the elements that the locator includes in its
searches.

And I would love it if there was a way to turn on "extra info" for failing
tests so that, for example,

  expected link "Number 3, Contact" not to return anything

could become

  expected link "Number 3, Contact" not to return anything, but found a
matching link at this xpath:
  /html/body/div/section/div/div[2]/div[4]/table/tbody/tr[5]/td[3]/a[2]

Digging that out of a console-based debugger is painful!

Thanks,

Mark


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jo Liss  
View profile  
 More options Sep 14 2012, 8:40 pm
From: Jo Liss <jolis...@gmail.com>
Date: Sat, 15 Sep 2012 02:40:54 +0200
Local: Fri, Sep 14 2012 8:40 pm
Subject: Re: [Capybara] has_no_link search is unexpectedly broad

On Sat, Sep 15, 2012 at 2:22 AM, mcbsys <mcbsyst...@gmail.com> wrote:
> Probably too late for that discussion, but it would help
> if the docs listed all the elements that the locator includes in its
> searches.

Hm, seems like the documentation is indeed off, at least here:
http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Fin...

Do you want to send a pull request?

>   expected link "Number 3, Contact" not to return anything

> could become

>   expected link "Number 3, Contact" not to return anything, but found a
> matching link at this xpath:
>   /html/body/div/section/div/div[2]/div[4]/table/tbody/tr[5]/td[3]/a[2]

On master, it's

     expected not to find link "Number 3, Contact", but there was 1 match

which is marginally better. I don't see a reason why we couldn't also
provide an xpath, but it gets a little tricky if there's more than one
match.

Jo

--
Jo Liss
http://www.opinionatedprogrammer.com/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
mcbsys  
View profile  
 More options Sep 15 2012, 8:46 pm
From: mcbsys <mcbsyst...@gmail.com>
Date: Sat, 15 Sep 2012 17:46:40 -0700 (PDT)
Local: Sat, Sep 15 2012 8:46 pm
Subject: Re: [Capybara] has_no_link search is unexpectedly broad

Hi Jo,

Re. the documentation, I could probably fix *has_no_link?* with moderate
confidence, but the whole set of matchers needs review, and I'm still
pretty lost when I try to read through the code. I picked the random
example of has_select, where the doc

https://github.com/jnicklas/capybara/blob/master/lib/capybara/node/ma...

says it searches by label, name, or id, but where what I would guess is the
corresponding xpath builder

https://github.com/jnicklas/xpath/blob/master/lib/xpath/html.rb#L80

calls locate_field

https://github.com/jnicklas/xpath/blob/master/lib/xpath/html.rb#L169

which matches on what to me is a pretty opaque combination of id, name,
placeholder, label, for, label (again), and disabled.

Do you want me to do a pull for the *has_no_link?* line? I think that
should be:

"Checks if the page or current node has no link with the given text, id,
title, or img alt attribute."

Probably *has_link?* would be:

"Checks if the page or current node has a link with the given text, id,
title, or img alt attribute."

> I don't see a reason why we couldn't also provide an xpath, but it gets
> a little tricky if there's more than one match.

Thanks for considering this! Hopefully you have the array of xpaths that
you could dump? e.g.

   expected not to find link "Number 3, Contact", but there were 2 matches
with xpaths
   /html/body/div/section/div/div[2]/div[4]/table/tbody/tr[5]/td[3]/a[2]
   /html/body/div/section/div/div[2]/div[4]/table/tbody/tr[6]/td[3]/a[2]

Mark


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
mcbsys  
View profile  
 More options Sep 15 2012, 9:10 pm
From: mcbsys <mcbsyst...@gmail.com>
Date: Sat, 15 Sep 2012 18:10:19 -0700 (PDT)
Local: Sat, Sep 15 2012 9:10 pm
Subject: Re: has_no_link search is unexpectedly broad

So to search on exact link text, optionally with an exact href, I added
this to my spec_helper.rb:

    def has_exact_link?(locator, options={})
      if options[:href]
        xpath = "//a[text()='#{locator}' and @href='#{options[:href]}']"
        options.delete :href
      else
        xpath = "//a[text()='#{locator}']/@href"
      end
      has_xpath?(xpath, options)
    end

Then I can call:

  it { should have_exact_link("Number 3, Contact") } # check exact text only
  it { should have_exact_link("Number 3, Contact", href: "/accounts/5") } #
also check href

To my surprise it seems to work! Is that the best way to write it? The only
drawback is that by deleting :href from options, the test output shows
options = {}. But I wanted to be sure that options did not include the href
that I handled in the xpath.

I thought about using

  it { should have_selector('a', text: "Number 3, Contact", href:
"/accounts/5" }

but it seems that silently discards the href option. (Here's where I wish I
could inspect the generated xpath query!)

Thanks,

Mark


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
mcbsys  
View profile  
 More options Sep 19 2012, 7:36 pm
From: mcbsys <mcbsyst...@gmail.com>
Date: Wed, 19 Sep 2012 16:36:34 -0700 (PDT)
Local: Wed, Sep 19 2012 7:36 pm
Subject: Re: has_no_link search is unexpectedly broad

I've updated has_exact_link and posted it here:  
http://stackoverflow.com/a/12469159/550712.

Just finished updating a test suite where 40 passing tests used have_link.
After replacing have_link with have_exact_link, 20 tests failed. Some of
those were due to the substring matching (now I have to search for "Edit
contact" not just "Edit"). But many were actually succeeding by finding the
wrong link due to have_link's broad search criteria.

Mark


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jonas Nicklas  
View profile  
 More options Sep 20 2012, 3:03 am
From: Jonas Nicklas <jonas.nick...@gmail.com>
Date: Thu, 20 Sep 2012 09:02:43 +0200
Local: Thurs, Sep 20 2012 3:02 am
Subject: Re: [Capybara] Re: has_no_link search is unexpectedly broad
This is exactly why we will raise an error in the future if more than
one match is found.

/Jonas


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
mcbsys  
View profile  
 More options Sep 20 2012, 1:07 pm
From: mcbsys <mcbsyst...@gmail.com>
Date: Thu, 20 Sep 2012 10:07:27 -0700 (PDT)
Local: Thurs, Sep 20 2012 1:07 pm
Subject: Re: [Capybara] Re: has_no_link search is unexpectedly broad

That should help... So is that basically going to apply a ":count => 1"
option for has_link and ":count => 0" for has_no_link?

Mark


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »