ruby capybara to check a text inside a dev - ruby

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

Related

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

Getting text from bootstrap alert

I am attempting to test my rails app's alert messages. I have the following code that is produced inside of Twitter Bootstrap:
<div class="alert fade in alert-success">
<button class="close" data-dismiss="alert">×</button>
Signed out successfully.
</div>
What I am trying to do is pull out just the "Signed out successfully."
At the moment I have:
def alert_text
#page.find('div.alert').text
end
But that gives me "× Signed in successfully."
In my test I am doing:
assert_equal "Signed in successfully.", my_page.alert_text
If I change the assertion to include the ×, I am getting a invalid multibyte char (US-ASCII) error. Is there an easy way to pull out the text and ignore the button? I would wrap the text in a separate div, but it is generated in the gem so I can't.
Since its hard to see what else is going on, I would add a custom element with id="signedOut" or something around the specific text, and access it by id. It won't mess with your boostrap styling.
Alternatively, try being more specific in the find. You may be using two classes that start with alert.
def alert_text
#page.find('div.alert fade in alert-success').text
end
If you have two div's using this class, you can add on to the end of it or just go into the class definition in your bootstrap folder and add one more layer to it. (Bootstrap uses js and css just like the rest of us, although it helps get things off the ground pretty quickly).

find a particular text is present or not inside an element using xpath or ruby capybara code

Iam working with test automation .for that am using ruby capybara to write 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.
the following xpath should return true if the text contains the given string
//h3[contains(., "Your order is placed")]=true()

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