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

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

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.

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')

Related

click on a image link with Capybara

I am trying to click on a image link with a Capybara / Rspec test. I am having very little success at the moment.
I am trying to select the link with href "/post/3", (knowing that they are other links before). I have tried many combinations of xpath without success. The only combination working was
page.first(:xpath, //a).click
however when I have changed the file and added more links above my Capybara test is broken.
<div class='row'>
<img id="imagen3" src="/system/posts/images/000/000/003/original/frankie-mannings-102nd-birthday-5160522641047552-hp.gif?1464448829" alt="Frankie mannings 102nd birthday 5160522641047552 hp" />
<p>caption</p>
</div>
How can I select that link, and click it?
ok I got it:
find(:xpath, "//a[contains(#href,'posts/3}')]").click
//a[contains(#href, 'posts/3’)]

Cannot access href from a element

I cannot select an href with Mechanize. I have done so successfully in the past with the following methods, but for this website it does not seem to work. Does anyone have any ideas?
Here is a snippet of the output of just the selector:
<div class="component-user-name component-user-name-15">
<a class="name-page-link" data-reco-action="view"
href="/profile/name">Name</a>
These are my attempts:
agent.page.search(".selector a").map {|link| link["href"]}
agent.page.search(".selector a["href"]")
agent.page.search(".selector a")[0]["href"]
agent.page.search(".selector a").attribute["href"]
Try this (html is different, you cannot do copy&paste from different website ;) ):
agent.page.search('a.name-page-link').attribute('href')
If it really looks like that, then:
agent.page.at("a.name-page-link")["href"]

ruby capybara to check a text inside a dev

Iam working with test automation .for that am using ruby capybara to wite test scripts.Using
Ruby cabybara code i want to check a text is present is not inside dev element
how can i possible?
<div class="modal-header">
<h3 class="orderCompleteNoEmailLabel">
Your order is placed, but one more step is needed to complete it.
</h3>
</div>
Here i want to check the text Your order is placed, but one more step is needed to complete it. is present or not.
Personally I would use this:
page.should have_css('div.modal-header', :text => "Your order is placed, but one more step is needed to complete it.")
you could also use:
page.should have_content('Your order is placed, but one more step is needed to complete it.')
Here is a link to a pretty useful list for capybara methods.
https://gist.github.com/zhengjia/428105

capybara ruby code to check an element is present or not

using capybara how can i check an element is present or not.
i used following code but it is not working
page.should_not have_selector('#confirmation_code')
<div style="width:250px;display:block;float:right;text-align:right;">
<span id="email_text">Order Number:<br></span>
<input id="confirmation_code" type="text">
</div>
i want to check above text box element is present or not?
So going from the last comment from Sush,
The first line in the code in the question is not correct at all. That will verify that the element is not shown.
You could use the following instead:
page.should have_css('#confirmation-code')
or if you wanted the text within do this:
page.find('#confirmation-code[type="text"]')
I will be honest though I am going by what I think the question is, it is not very clear what you are looking to achieve.

Ruby Watir - how to select <a onclick="new Ajax.Request

Hi I'm trying to select an edit button and I am having difficulty selecting it.
<td>
<a onclick="new Ajax.Request('/media/remote/edit_source/3', {asynchronous:true, evalScripts:true}); return false;" href="#">
<img title="Edit" src="/media/images/edit.gif?1258500617" alt="Edit">
</a>
I have the number at the end of ('/media/remote/edit_source/3') the which changes and I have stored it in #rep_id variable.
I can't use xpath because the table changes often. Any suggestions? Any help is greatly appreciated. Below is what I have tried and fails. I am fairly new to watir and love it, but occasionally I run into things like this and get stumped.
browser.a(:text, "/media/remote/edit_source/#{#rep_id}").when_present.click
The line:
browser.a(:text, "/media/remote/edit_source/#{#rep_id}").when_present.click
fails because:
The content you are looking for is in the onclick attribute (rather than the text)
The locator is passed a string for the second parameter. This means that it is looking for something that exactly matches that. Given that you are only using part of the text/attribute, you need to use a regexp.
If you are using watir-webdriver, there is support for locating an element by its :onclick attribute. You can use a regexp to partially match the :onclick attribute.
browser.link(:onclick => /#{Regexp.escape("/media/remote/edit_source/#{#rep_id}")}/).when_present.click
If you are also using watir-classic (for IE testing), the above will not work. Instead, you can check the html of the link. Checking the html also works in watir-webdriver, but could be less robust than using :onclick.
browser.link(:html => /#{Regexp.escape("/media/remote/edit_source/#{#rep_id}")}/).when_present.click
From your example, it looks like you are using the URL from the onclick event handler as a :text locator, which I'd expect to fail unless that text does exist.
You could potentially click on the img. Examples:
browser.image(:title, "Edit").click
browser.image(:src, "/media/images/edit.gif?1258500617").click
browser.image(:src, /edit\.gif\?\d{10}/).click # regex the src
Otherwise, you might need to use the fire_event method to trigger the event handler, which looks like this:
browser.link(:id, "foo").fire_event "onclick"
These are the links to the fire_event docs for watir and watir-webdriver for reference.

Resources