Testing more than one same link rspec capybara - ruby-on-rails

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!

Related

Selecting links in side nav and menu bar?

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

Re-use scenarios on multiple pages

I am new to Specflow and need a way to reuse scenarios in multiple feature files.
I have a web application consisting of multiple pages that each share a many items such as footer content. Say my footer contains 3 links:
Help | Feedback | FAQ
I have a scenario to test the "Help" link functionality:
Scenario: Help link
Given I am on page1
When I click on the link containing text "Help"
Then I see the help popup
As the "Given" statement specifies which page to open, the scenario is tied to that page. In order to test the same help link functionality on page2, page3, page4, etc., I would need to:
1) Copy the scenario into another feature file
2) Change the given statement to reference the correct page
3) Repeat for all pages containing the help link!
This seems like an awful lot of duplication and there must be a better way to do this.
You can use a Scenario Outline, which is basically a data driven test:
Scenario Outline: Help link
Given I am on <Page>
When I click on the link containing text "Help"
Then I see the help popup
Examples:
| Page |
| Home |
| Contact Us |
| About Us |
You can't test everything when you work at this level. In fact you can only test a fraction of whats possible. So what you have to do is think about what benefit you get for testing the footer on each other page, and see if this is worth the cost of
Writing the tests
Running the tests
Secondly you don't need to repeat all the tests for each page. If the links work on one page then they will work on every other page, so you could test the links once, and then subsequently check that the footer appears on other pages e.g.
Feature: Footer checks
Scenario: Footer appears on pages
Given a selection of pages
When each page is visited
Then each page should have the footer
and have your selection of pages be a random small sample from all the pages.

the page content is incomplete after visit a page

I am new to Capybara. I try to use "visit root_path" and then check if the spicified tag is rendered correctly. Firstly there is "ElementNotFound" error. And i use "puts page.html" to see the response page content. And i found that only the static parts of the page are rendered. And all the other parts dynamically created by rails(<%= %>) are NOT. BTW, i put this test in requests folder. All Capybara config are default.
Did anyone come with similar problem like it?
Try to use lanchy gem and save_and_open_page in your tests. Check this post

Using Capybara w/ Selenium, how would I click a table row?

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.

Using click_link with Cucumber Scenario

I have the following Feature file which is attempting to visit a page, select a link then edit user data.
Scenario: Edit an existing member of the club
Given I go to the member list page
And I choose the first item
And I fill in the "First Name" with "Bloggs
When I press "Update"
Then I should be on the member list page
And I should see "Bloggs"
and the member_step.rb file contains
Given /^I choose the first item$/ do
#save_and_open_page
visit members_path
click_link "1"
end
My routes.rb file has
Gfs::Application.routes.draw do
resources :members
end
The problem is that I can't get the click_link "1" step to work. The intention is that it will
Navigate to the '/members' index page
The index page displays a list of users each prefixed with a number, that is the link.
Select the link
What I know is that the index page does display a list, my route works ('/members), but my Cucumber is failing and I'm not basking in the glory of green.
The save_and_open_page diagnostic shows only the title of the index page and not the details, as though the controller is not accessing the data
Suggestions are most welcome.
Is there a Background where you create the members data? If not, it may be that the data does not exist.
I'd also suggest adding a step like
And I should not see "Bloggs"
before you update the record.
If I understood your cuke correctly, you really won't see the "1" link because:
Given I go to the member list page <-- first step
And I choose the first item <-- second step
...which expands to:
Given I go to the member list page <-- first step
visit members_path
click_link "1"
why are you visiting the member's path after you just visited the member list page? you should be checking that you ARE in the member's path. Something like:
Given I have existing users(generate members here using factory or whatever)
And I go to the member list page( go to member page)
When I follow "1" (click the first member)
Then I should be on the "1"'s member page
When I fill in...etc you get the point
Hope that helps!
I'm going to suggest that IFF the "1" is a true html anchor link, then you might consider using
And I follow "1"
You could easily wrap this in your own syntax to make it more aesthetically pleasing but the And I follow... step is one that you get out of the box in web_steps.rb.
Another gain in using that one is that you can use a selector to make the click more specific:
And I follow "1" within ".my-kickbutt-div"
Or something along those lines.
Another thing to check into. If you have #javascript enabled you might be seeing incorrect data in your save_and_open_page. My app is javascript/ajax heavy and I have to open the output in a browser with javascript turned off to see what is really visible there :P This might not be your issue, but just in case...

Resources