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
Related
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.
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.
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.
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.
Would simply like to know if there's an alternative to click_link / click_button that can be used with any element as some of my clickable elements are not standard html clickables such as tr elements, but they still contain href attributes.
Javascript enabled.
Use Javascript then:
page.execute_script("$('whatever_you_want').click()");
I had the same situation with a html month view, I had to choose a day of month. I kept it as simple as I could and this is only one way of doing this.
# Choose July 22 (at this point in time)
assert page.has_css? '#calendar'
within '#calendar' do
find('td', :text => '22').click
end
Are you using cucumber? Not sure if it's any use to you, but here's a cucumber step definition for clicking anything with selenium:
Then /^I click "(.+)"$/ do |locator|
selenium.click locator
end
I have tried the javascript solution in past, it works in the sense that the button do gets clicked, but cucumber fails to recognise it as a completed step.