In the Rails Tutorial, section 5.3 (Layout links) we add some tests for the Contact page:
describe "Contact page" do
it "should have the content 'Contact'" do
visit '/static_pages/contact'
expect(page).to have_content('Contact')
end
it "should have the title 'Contact'" do
visit '/static_pages/contact'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Contact")
end
end
After which we comment out the Contact link in the footer code:
<li><%#= link_to "Contact", '#' %></li>
According to the text, "To ensure that both of the tests in Listing 5.17 fail, we need to comment out the “Contact” link in the footer", but that doesn't make sense to me. There is no test for the Contact link, only tests that the Contact page has valid title and content. If the Contact page is properly defined the tests should still pass whether there's a link in the footer or not.
Or am I missing something here? It behaves as I expect it to, but I don't want to just continue on and miss something I should be understanding.
Thanks
If we don't comment out the link, the first test will pass before we create our Contact page. That's because RSpec will search for the word "Contact" anywhere on the page (that's how 'have_content' works), so it will find it in the footer, which means the first test will pass thanks to our generic footer, not thanks to us actually creating a Contact page.
Related
I application.html.erb I add <%= yield %>. I want to show registration page (/users/sign_up) with header and footer in application.html.erb. As I understand contant from /device/registration/new.html.erb must contain in yield, but I get registration page withot templates(header and footer).
How I could show registration form in yiled?
I use RoR 3.2.12. Sorry for my English. Thanks.
If you get the registration page without the layout, then chances are you have a :layout => false triggered somewhere upstream.
I have a test that fills out some fields and then is supposed to click a button. This is all done after loading up a modal window. However, it seems that it just skips over it and doesn't click the button. I have tried debugging it manually and calling it myself and it'll work fine but when I run the test by itself it doesn't click it.
Given /^I login with "(.*?)" and "(.*?)"$/ do |email, password|
within "#signin_fields" do
fill_in("custom_fields_email", :with => email)
fill_in("custom_fields_password", :with => password)
end
click_button("Sign In") if page.should have_selector(".btn-signin")
end
I even added a check to make sure it was on the page but since the removal of wait_until, I'm not sure how I'm supposed to let the page load and then make sure it clicks the button properly. Any ideas would heavily appreciated.
This post may help: How do you get rspec to output what it encountered rather than it "didn't find what it expected"? Basically, for debugging, you can add the line save and open page and inspect what the spec is finding and whether it differs from the results of your own debugging.
The following row from your code is incorrect as have_selector is a RSpec matcher:
click_button("Sign In") if page.should have_selector(".btn-signin")
Instead you should use any of Capybara::Node::Matchers like:
click_button("Sign In") if page.has_selector?(".btn-signin")
page.should have_selector(".btn-signin") isn't going to return true or false and therefor the button will never be clicked. Just do this:
click_button(".btn-signin")
If it should be there then the test will fail if it doesn't appear on the page.
In Rails 3.2 I'm using rspec (2.11.0) and capybara 1.1.2 (and have not installed webrat) and it's doing something strange when I click a link on my web page.
I'm trying to click on link on my home page to go to another page. Below is the basic rspec test.
it "should go to agents page" do
visit 'home'
#puts page.body
#links = page.all('a')
#links.each do |l|
# puts l.text
#end
page.find_by_id('menu_agents').click
puts page.body
#<various content asserts on page currently commented out>
end
I basically go to a page, grab a link and click
The link exists. As you can see from the commented out statements I've confirmed by looking at the page in the console and also looking at the list of links. I've tried clicking not only with the link above, but also using click_link and referencing the id. Looking at both the code and the html of the page when I run the code I see the id of the element.
In all cases I get the following result:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
When running the software with rails s clicking the link does load the correct page. Likewise when I visit the home page in the test it loads correctly as well, since when I print the body to the console I see the right html.
It seems like something is going wrong with the page load when I click the link and the page load has some type of failure (although the test itself does not fail). Any ideas?
Please try this:
Page load will take some time. Use sleep function. It makes some time to wait.
Then use page.find("#menu_agents").click instead of page.find_by_id('menu_agents').click
Verify menu_agents id name is not present any other places in result page.
Note: make sure menu_agents id is unique.
it "should go to agents page" do
visit 'home'
sleep 5
page.find("#menu_agents").click
puts page.body
#<various content asserts on page currently commented out
end
I'm trying to get my first capybara tests going. I following Ryan Bate's philosophy and putting some functional tests into my controller spec files.
describe UsersController do
render_views
it "can get the home page" do
get 'home'
response.body.should include("Login")
end
it "should log in" do
get 'home'
puts response.body
click_link('Login')
response.body.should include("Email")
response.body.should include("Password")
end
end
In it should login I ran into problems so began with just trying to make sure I can find and click the link. No luck. In the test as above I'm just trying to make sure the link exists
The puts response.body produces the following output
...
<div id="user_nav">
Register or Login
</div>
...
and I also see the element on the actual page. It seems only my test can't find it. The first test does pass.
1) UsersController should log in
Failure/Error: click_link('Login')
Capybara::ElementNotFound:
no link with title, id or text 'Login' found
# (eval):2:in `click_link'
# ./spec/controllers/users_controller_spec.rb:14:in `block (2 levels) in <top (required)>'
I'm using Rails 3.2, Rpsec 2.11 and Capybara 1.1.2.
(I've already checked the other questions on stackoverflow as well as a few tutorials and screencasts. I can't see any reason it can't find an element given an id tag, but I'm probably missing something obvious.
I think the reason it's not working is that you're using get for what is essentially an integration test. See this post: http://blog.plataformatec.com.br/2012/06/improving-the-integration-between-capybara-and-rspec/
If my understanding is correct, you need to use visit in order to use click_link on the page:
it "should log in" do
visit home_path
click_link('Login')
page.should ...
end
See also this answer on SO: Rspec and capybara, difference between visit and get methods, with regards to the current_path object
I have seen the following page:
http://activeadmin.info/docs/9-custom-pages.html
It doesn´t have a lot of information on how to create a custom page.
What I need to do is to add a custom action to the index of an entity that redirects me to another page i.e. /admin/mycustompage . I want to render my new page from a partial. It has to look similar to a view or edit page (with a breadcrumb and the layout).
The example in the docs is too simple:
ActiveAdmin.register_page "My Page" do
content do
para "Hello World"
end
end
How can I render a partial within the content?
How can I render the breadcrumb?
How is the url for this new page specified?
Thanks.
Sample page, rendering /app/views/admin/password/_index.html.haml partial:
ActiveAdmin.register_page "Password" do
menu label: I18n.t("menu.change_password")
content do
render "index"
end
end
Default url for this page is /admin/password (you can check it by calling 'rake routes').
If your page title contains spaces, you have to use aa version from github, because it was impossible until this commit - https://github.com/gregbell/active_admin/commit/30b19c86eef3c504fe71c2e39e072620169b80c2