How to check if url/links exist in page in Capybara?
Solution 1
The following two should work:
expect(page).to have_link('', href: 'http://project/guides/basics/')
expect(page).to have_selector("a[href='http://project/guides/basics/']")
I was really hoping there is a better looking way, but unfortunately, I can't find one.
Solution 2
Capybara API provides method like below -
page.should have_link("Foo")
page.should have_link("Foo", :href=>"googl.com")
page.should have_no_link("Foo", :href=>"google.com")
For your specific sample you can find all elements and then check if a
is there with specific div and image. like below -
page.all("a[href='http://project/guides/basics/'] div img")[0]['src'].should be('/images/basic_img.png')
Þaw
Web Developer PHP CakePHP CodeIgniter jQuery / AJAX Ruby On Rails Tags: Please avoid extended discussions in comments. Would you like to automatically move this discussion to chat?
Updated on June 20, 2022Comments
-
Þaw almost 2 years
I've been searching for so long already but haven't found any solution.
I would like to check if a particular URL exists in the page. Rspec Capybara
for example: I'd like to check if the url http://project/guides/basics/ is in the page.
capybara has
has_link
function but only accepts anid
ortext
as a parameter so for this example,<a href='http://project/guides/basics/'> <div class='image-button-container'> <img src='/images/basic_img.png'/> </div> </a>
how should I do the
expect()
in Rspec using Capybara? Thanks -
Þaw over 8 yearsHi @Vishnu thanks for answering. based on my example above? how would I apply your answer? thanks
-
Vishnu Atrai over 8 yearsUpdated answer with your sample html
-
Þaw over 8 yearsHi @ndn! been working this for days :) thank you very much! I didnt know that first param of
have_link()
can be an empty string. I used the first one.