Capybara not able to recognize ajax loaded elements.? - ruby-on-rails

In my Rails 3 application, I have a "image" on my HTML page, which creates a "div" element in html page, when clicked.
I have to test the creation of this new "div" through RSpec (and i am using Capybara for views based testing).
I have written the following code :
it "clicks the extended details button" do
Capybara.default_wait_time = 5
within('.table_expand') do
find("#img_dealer_code_04039").click
should have_selector('#extended_details_04039')
end
end
yeah i have already added, :js=>true in corresponding describe.
I thought the problem would be of Ajax time , so i added Capybara.default_wait_time = 5 for it to load properly. (But it didnt work)
Also i tried putting :visible=>true in line should have_selector('#extended_details_04039') , but with no success..
Is there anything that i am missing..??
Please help me...I am stuck with this from quite a long time..!!

https://github.com/rspec/rspec-rails/issues/478
are you sure that you don't have duplicated ID on site?
Try to save it to file and check the source for duplications.
To see what's going on the page try
page.save_and_open_page method

Is img part of your ID or is it the tag type? I believe you shoul have
find("img#dealer_code_0409").click

Related

Why is my Ruby on Rails link_to sending the action twice?

I have a link_to method in my Ruby on Rails application in one of the views, and when I click on it, the controller is set to do a whole bunch of things... But for some reason I'm seeing this GET request twice even when I click on the link one time.
Here's what the link looks like:
<%= link_to image_tag("excel.png"), spreadsheet_technical_report_path(report) %>
Which goes to /technical_report/id/spreadsheet and here's what it looks like in the controller:
def spreadsheet
spreadsheet = GenerateSpreadsheet.generate(params[:id])
send_file spreadsheet
end
I've even replaced the contents of that function with a binding.pry, and it hits it twice! This is so confusing. my whole GenerateSpreadsheet model does a variety of things and takes approximately a minute, and this second request does nothing but double that time.
Can someone please tell me what I'm missing here? I don't have a view set up for this since I want it to just send the user a download prompt (which it's doing) and not necessarily go to a view. I don't even know if not having a view is even relevant here.
JS
To add to the comments, the main issue here would likely be that you've bound some javascript to the page's a elements.
With an absence of remote: true and other hooks, the only thing which would likely cause a double-fire from your link_to is if Javascript is sending an ajax request too.
You mention that you...
removed //= require tree . from my application.js
... whilst good that this fixed the issue, you have to remember that nothing happens with computers without them being told to do it. IE your "link" wouldn't just double-click for the sake of it.
If your JS works when you remove the //require_tree ., you'll want to look at the other JS files you have. There will be one where you're binding to the $("a").on("click" event, which is likely leading to the double-firing of your link.
Thanks to chaitanya saraf's comment, I just removed the "//= require tree ." from my application.js to get this fixed. Once I got rid of this, I added this to my config/initializers/asset.rb file
Rails.application.config.assets.precompile += [/.*\.js/,/.*\.css/]
Got rid of this annoying problem nice and easily.

How to set an initial value for my object in ruby on rails

I am still pretty new to Rails and am working on a basic app and have run into a problem, which I can't seem to get around, sorry if this question has already been answered, but searching didn't yield me any results.
Basically I am creating an app that catalogues all of someones's clothes, Now the main page is the index page of everything they have logged, and each picture is linked to the show page where more information is revealed. I want to use AJAX to bring that show page to the side of the main index page. I am working off of another tutorial to figure this out but have run into a problem. On the initial load of my index page, I need to define a variable for that show page so that it can be rendered using a partial, but I can't use find(params[:id]) since the user hasn't selected anything yet. So my question is how do I set that initial value to either just the first item in the catalogue or not have anything appear, and then when someone does click the link, the sidebar shows the more detailed description of the piece of clothing.
Thanks for any help.
#object = params[:id] ? MyModel.find(params[:id]) : MyModel.first
But I think there's some problem with design of application.
You might have some luck working with the ruby gem 'PJAX'. Here is a screen cast showing you how to get that AJAX sidebar you want. Good luck
It sounds like you can just print the container element as normal, but leave it empty when the page is generated. Optionally, hide it via CSS. Then, when you load its content with AJAX, set it to visible or just populate it as normal.
Alternatively, if you really want to set it to the first item in the catalog (or in any ActiveRecord) you can use .first e.g. Products.first, and use that value to populate its initial contents.

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.

visual_effect after replace_html not working in rjs

In learning ruby on rails I've created a blog site. When the user adds a post via AJAX, the following rjs gets called:
page.replace_html 'posts', :partial => #posts
page.visual_effect :Highlight, 'post_' + #post.id.to_s
However, the highlight isn't happening.. neither is any sort of effect even a hide.
Clues:
It works if I just do an insert_html
for just the new post (but I want
to update the whole list of posts
at this time)
If I hard code the id to the next id in the sequence, it doesn't work on the post, but it does work on the next post. { ex.. hardcode post_100... no highlight on submit 100, highlight 100 on submit 101 }
Alert shows that 'post_' + #post.id.to_s is what is expected ( post_100, etc )
Any ideas or debugging suggestions?
Thanks,
Orlando
Can you alert the innerHTML of the $("post_#{#post.id}") before the visual_effect.
Does firebug give you an error when it gets to the visual_effect?
Can you do something else, like an alert after the visual_effect line?
Have you got the required js files included?
It's not really an answer to the problem, but I have since done away with reliance on rjs. Instead I'm following the pattern outlined here
http://blog.solnic.eu/2007/10/30/why-javascript-helpers-in-rails-are-evil
And now all effects are working as expected. Note that I did get the effect working when comments were added using effectively the same code that should have been working here, so I'm fairly convinced there was just some sort of weird operator error going on.

How do I tell ActiveScaffold to always show the search form in a list view?

The ActiveScaffold list view has a search form that is loaded via ajax when a user click the search link. I'd prefer to have the form show by default when a user opens a list page.
I've figured out a way to trigger the ajax call when the page loads, but I'm wondering if there's a way to get ActiveScaffold to render the form automatically. Is there a template or a method I can override? I've had a look through the code but there's nothing obvious, at least to me.
Update:
srboisvert's answer inspired me to have a better look.
The trick is to use Template overrides to refactor the following: list.rhtml, _list_header.rhtml, _search.rhtml so that the search form partial renders inline.
There is a way to get it rendered automatically:
active_scaffold :model do |config|
config.list.always_show_search = true
end
I don't currently have an active scaffold project handy but here is how I would figure it out.
I'd use firefox with firebug installed and take a look at what is called when the link is clicked. Then I would go look at that javascript and what it is generating. Then I would search the source for any part of the code or combination that would be fairly unique to the search box ajax. After that it should be easy to cut and past it in without the ajaxyness.
The option
config.list.always_show_search = true
works fine, but only on concrete controller. It throws an exception when used in AS set_default block. Somebody know better solution then to include it in every controller (apart from overriding the template which is handy but complicates version updates)

Resources