Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if url/links exist in page in Capybara?

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 an id or text 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

like image 585
Þaw Avatar asked Aug 20 '15 07:08

Þaw


2 Answers

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.

like image 138
ndnenkov Avatar answered Oct 31 '22 21:10

ndnenkov


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')
like image 35
Vishnu Atrai Avatar answered Oct 31 '22 21:10

Vishnu Atrai