I am writing test cases and would like to test that all the links in the navigation bars work. How can I go about testing them?
When I run:
visit welcome_index_path
page.find("li", text: "Companies").click
I get an error saying that two elements matching css "li" with text "Companies"? How can I break it down further to find the elements.
I am assuming you are using Capybara based on the syntax, in which case you would wrap your call in a within block. For example..
visit welcome_index_path
within("#id_of_whichever_menu_youre_testing") do
page.find("li", text: "Companies").click
end
There are more examples on the capybara readme https://github.com/jnicklas/capybara
Related
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)
I have a link with a specific attribute on my page. Using Rspec + Capybara how can I check for the existence of this link?
Foo
Does not work:
page.find(:css, 'a[data-content="This is a discrete bar chart."]')
Works:
page.find(:css, 'a#test')
I have the same query, but the quotes are different.
find("a[data-method='delete']").click
and works for me.
Here are some different ways:
expect(page).to have_selector("a[href='#'][data-content='This is a discrete bar chart.']")
page.has_selector?("a[href='#'][data-content='This is a discrete bar chart.']")
page.find("a[href='#'][data-content='This is a discrete bar chart.']") # returns the node found
If you don't have access to page but have access to rendered, then
expect(Capybara.string(rendered)).to have_selector("a[href='#'][data-content='This is a discrete bar chart.']")
It could be an issue of Capybara finding too many results. Maybe change 'find' to 'all' and see if you get an array of results that you could select from. Best of luck.
This worked for me when searching for a link's title attribute (tooltip):
Then(/^I should see tooltip "(.*?)" on link "(.*?)"$/) do |tip, link|
within('#results') do
link = find(:link, link)
expect(link['title']).to eql(tip)
end
end
NOTE: the within is optional and useful for reducing chances of ambiguity.
HTML Source code:
<a title="Owner: lfco Fire Department" href="/water_supplies/597df82566e76cfcae000018">Hydrant LFCO One</a>
I have two About Us links in the homepage (one in the header and one in the footer of the page). I need to test that both links point to the same About Us page.
I have written test in rspec like this inside the describe block:
it { should have_link('About Us', href: aboutus_path) }
However this test does not give error if there is at least one link is ok in the page. I need something that checks for both links and it should give error if any one of the links has been changed.
Thanks in advance for the help Guys!
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.