Clicking a link using rspec and capybara doesn't load the page - ruby-on-rails-3

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

Related

ViewStateException - Validation of viewstate MAC failed - Sitefinity ASP.NET MVC with Jquery Mobile

I am getting this error most of the time when I submit my form. I am using Sitefinity 6.2 with ASP.NET MVC 4.0 and JQuery Mobile.
As I have Sitefinity in Hybred mode I am using the #Html.BeginFormSitefinity() command to create the form. On the Controller I have my action with the [HttpPost] attribute. The code always hits my default action on the controller with no problem. No matter what I put in the form when I submit I only get an error message on the page...never hits the HttpPost action.
I've looked around and there are many pages with fixes for the MAC failed issue, but none are working for me. I have a machine key in the web.config and I am NOT going to set enableViewStateMac to false as that is a security hole.
OK I tried working with both of the below solutions but they are both really bad. Here is what I am doing now, which is still not great, but I have Sitefinity, MVC, and JQuery Mobile all on the same page and forms are not giving me View State Exceptions anymore.
First thing is that adding data-ajax="false" is not enough, for this to work you need to disable Ajax before JQuery Mobile starts. So, to do this you need to add in this script BEFORE the JQuery Mobile File loads but after the JQuery file loads.
$(document).bind("mobileinit", function () { $.mobile.ajaxEnabled = false; });
After doing this I then do not use the Sitefinity Begin Form, I just JQuery to change the form on the main page to have the correct action.
<script>
$("#aspnetForm").attr("action", "Home/Login");
</script>
Together this means that there is a complete page load for each page change, and form posts use the form declared in my WebForms Master Page.
-Old Answer -
Actually...what I have below is not working. What I am
currently doing is really ugly but is usually working.
As long as the user enters the site from the home page then the home
page is the Jquery Mobile first page. The view state errors that I
was getting was because it saw the current page as the first page and
the form submit was to the active page. What if the controller for
the home page was just set to handle ALL HTTPPost calls? I have
removed the #Html.BeginFormSitefinity() from all the views with forms
and am just using the form on my top level masterpage. Then I add in
code on the view to change the action of this form to point to the
main page controller. ex
<script>
$("#aspnetForm").attr("action", "Home/Login");
</script>
Once I made this change the forms are not throwing view state
exceptions...as long as the home page is the Jquery Mobile first page.
If the user comes in from a different page then all is scrambled.
Don't have an answer for that yet.
Really Old Answer -
OK, think I have found it. I read somewhere, lost the link now, a
list of issues that can cause the error message. One of them is the
form being submitted from a different page.
I looked at the error message I was getting with Fiddler and noticed
that the Referer was my home page but the URL of the form post was the
URL for the page with my form. In stead of browsing through my site
to the page with the form I typed the URL in the address bar. I tried
submitting my form again and now it works!
So, this is an issue of Sitefinity and JQuery Mobile fighting it out.
When asp.net MVC is run in Hybred mode in Sitefinity it is actually
run in a Web.Forms master page that contains a form. When you use the
#Html.BeginFormSitefinity() to add a form to the view it is actually
just adding a div and then using AJAX to submit the form on the
Web.Forms master page.
JQuery Mobile loads up the first page that you visit, but later pages
are just injected into the existing page. So, there are multiple
data-role="page" divs loaded up in the DOM, inside of the Sitefinity
Web.Forms Master Page.
This all together is causing the form to post with the URL of the
active data-role="page" but the server sees that it is being refered
from the original page I loaded up. So, if I went to the page with
the form first all would work, start at any other page it does not
work.
Now that I know this I can put in data-ajax="false" on the link to the
page with the form and all looks to be working. This will cause
JQuery Mobile to not inject the target page into the current page but
will load all fresh with the target.
data-ajax="false" is the answer!

Flattr button only shown after page reload

I've followed the instructions in here to add an embedded flattr button to my site but it's only being shown after reloading the page. This is the page: http://nipanipa.com/en/donations/new. Actually, if you visit that url directly, the button will be shown but if you visit it throught the "Donate" link in the header it will not! I get this behaviour in both Firefox and Chromium.
What am I missing? Probably this has something to do with my poor knowledge of javascript... :S
Thanks a lot!!
Your website (or framework if you are using one) is bundling all javascripts in to one neat package that is only loaded on the first page load. The flattr javascript is automatically triggered onLoad, but unless you are actually on the donate page during the initial page load, there will be no flattr button to initialize.
One way of solving this is to call FlattrLoader.setup() somewhere on the Donation page.
But since you are only interested in having one Flattr button for one url there is a much easier way to add it.
Remove all Flattr related code from your source code and then use the embed tool to generate the button specific html/javascript and paste that code where you want the button to appear.
Hope this helps :)

Confused why a test in section 5.3 should fail

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.

Selenium IDE - Assert that JavaScript redirect worked after clicking Ajax button

I have a button that executes an Ajax request and then it successfully redirects to another page.
How do I assert that the redirected page was successfully reached?
I have a clickAndWait on the button. But after that..?
you can use verifyTextPresent command to verify a unique lable or text in the redirected page.by that way you can fix you have successfully reached the redirected page.
try like this
command : verifyTextPresent
Target : some unique text in the redirected page.
i think your problem can be fixed by this.
IDE has many assert commands. You can use any of the one and you can achieve the test scope(here the page is navigated or not).
Example:
command : assertTitle
Target : check the title of the page.
In the above he used verifyTextPresent it will check weather the text is present or not in the page and it will continue the next step. If you use assert commands it will proceed the next step when the assert step is passed. Otherwise it fails.
One thing you keep in mind, selenium won't wait for Ajax kind of loading, it will wait for the Page loading. So, you have to put Wait commands explicitly to finish the Ajax loading.
You can get moreassertions when you convert the selenese code into the preferred language and testing framework. You can see that option in the Option tab in the IDE.
For more info on AssertCommands in SIDE

Render a custom page in ActiveAdmin using Rails 3

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