How to test for a target="_blank" attribute with Capybara? - ruby-on-rails

I'm trying something which I thought would be simple. I need to modify a link to have the target="_blank" attribute and I would like to have a test to confirm that it's actually on the page. I've tried a ton of variations, the closest I can get is:
it { should have_link('change', href: 'url', target: '_blank') }
but that just throws an error saying that 'target:' is an invalid key. Is there a way to test for this attribute? Moreover, is there a general method for testing for HTML element attributes? The information I've found isn't clear.
Using Capybara 2.1.0, Rails 4.0.5.
Thanks

You can do what you want, or test for any attribute value, using have_selector and the CSS selector syntax for specifying attribute values, like this:
it { should have_selector('a[href="url"][target="_blank"]') }
It can't be done with have_link, since have_link
doesn't take any HTML attribute as an option (it takes the options that have_selector takes, plus :href), and
doesn't let you specify the entire selector.

Related

How do I display the text of a html span tag in a capybara test

I am running the following integration test on ruby on rails capybara.
test "displays correct date" do
assert page.has_content? #item.begining_date.strftime("%B %d")
end
I get false expected truthy. To try and debug this I want to see what is actually being displayed in the page during tests because I get the proper date when I run the website on my local host. I know that the html tag that displays the dates for the items have and id called date1, date2, date3, and so on.
like in the following example.
<span id="date1">Fri, June 12</span>
I want to display on my console/terminal what is in the span tags with id = "date1", id = "date2",...etc. So that when I run the tests I know what is actually being displayed instead of the expected content.
If you change your rspec assertion to
assert page.has_selector? '#date1', text: 'what you expect the text to be'
Then the error message when the test fails will indicate that the selector '#date1' was found but instead of the text you expected, it contains 'some other text'. This way you will know what was actually on the page.
The root cause of your issue is that you should never use raw assert with Capybara methods. If you instead use the assertions provided by Capybara you'll get error messages that give you useful information instead of "expected true got false"
assert_content #item.begining_date.strftime("%B %d")
will output what the content of the page is if it doesn't match
assert_selector '#date1', text: #item.begining_date.strftime("%B %d")
will output the contents of the element with id 'date1' if it doesn't match the provided text option.
If you're ever writing assert <any_capybara_provided_predicate>?... you're doing something wrong.

Rails (/Rspec) (/Capibara): Test Icon is visible?

I'm currently using Rails, Rspec and Capybara.
How would I test that an Icon is actually visible and coming through on the client side? Something like this:
<i class="ionicons ion-man"></i>
I've searched stack overflow for "Rspec Icons" and "Rails Test Icons" and haven't found an existing question that can answer this.
edit:
I don't want to show that the css is present and visible on the page, but whether the icon is coming down the asset pipeline. I just tested this solution and a passing test after 'breaking' the asset pipeline so that the icons come through as boxes. Perhaps my request is out of the ability of capybara?
You can use the have_css selector.
expect(page).to have_css('i.ion-man')
You can check if a particular node (in your case li) with a specific class (in your case ionicons) exist and is also visible:
page.has_css?('li.ionicons', :visible => true)

Rails + Cucumber: How to test for link with certain text?

I have a link < a href = "http://www.yahoo.com">hello< / a >. How would I use cucumber to test that the link is there? page.should have_selector doesn't seem to have the option to specify what the text of the link is. I'm looking for the assertion version of click_link('hello'). I can also search by href value as well.
page.should have_link('hello')
See https://github.com/jnicklas/capybara/blob/master/lib/capybara/spec/session/has_link_spec.rb for some examples

Why is Rspec/Capybara giving me a expected css ... to return something error?

The test I am using is:
it { should have_link('Logout', href: logout_url) }
I am just testing for the existence of a <a> tag with Logout as the text, and the href to be logout_url path. Should I be using another syntax for this?
Gems:
rails 3.2.6
rspec-rails 2.10.1
capybara 1.1.2
There are a number of ways you can do this. I'd recommend (if you haven't already) familiarizing yourself with the overall capybara dsl as well as some of the specific sections such as the capybara matchers.
I believe your test is fine written as:
it { should have_link('Logout', href: logout_url) }
as long as the link text really is 'Logout' and the href is specified correctly (see below).
According to the capybara docs, the first parameter to have_link is what's called the locator. In your case it's 'Logout'. This can be either the text in the link itself, or it can be the #id of the dom element. So in your case you need to have the text 'Logout' in the link that has the logout_url link.
Note that locaters in capybara are case sensitive, so make sure you match on case.
You might also want to consider whether you should be using logout_url and not logout_path. By default, rails doesn't always generate the full url for most links. It just generates paths. Here's the difference:
users_url: http://localhost/users
users_path: /users
Check your page to see which of these types of url's are being generated by your app.
Probably it should be logout_path.
Also you can try to use save_and_open_page to understand what's going wrong.

How do you check to see if there is a specific piece of text in a span tag using Rspec/Capybara?

I'm currently using the following piece to code to check if my page has a specific piece of text:
it { should have_selector('test_container', text: 'hiya') }
All I'm checking is a span on my page:
<span id="test_container">hiya</span>
How do I do I test for the contents of a specific span tag like this? I don't want to check the entire page for the text 'hiya' but a specific span tag like the above.
Thanks!
Capybara gives you two possibilities here, you can either use the find method:
find('#text_container').should have_content('hiya')
or you can scoping to restrict a block to an html container:
within '#text_container' do
page.should have_content 'hiya'
end
Note that although you're saying page.should in the block, it is restricted to the scoped element.

Resources