Capybara: Finding a second checkbox which is identical to the first - ruby-on-rails

I am writing tests for view files. I have a page with two checkboxes which allows the user to Select All items in two different lists. However, the checkboxes are part of a partial so are identical. I have managed to check the first one using:
first(:checkbox, "Select all").click
But I am unable to check the second. I have tried replacing first with last and with second but to no avail.
I think I may need to use the find selector but am struggling with that also. Any help much appreciated.
Thanks
UPDATE
It appears that this in fact another issue. I think that any checkbox that impacts on other checkboxes does not work as expected when "checked" by Capybara. So the current answers below work in the sense that they do check the checkbox, but the expected behaviour does not occur i.e. the checkboxes linked to them do not get "checked". I may be wrong though.

try this
# find the second checkbox
find('input[type="checkbox"]:nth-child(2)').click

You can use find all
all('input[type="checkbox"]', :text => 'Select all')[1].click

Whoops. Turns out it was a JS issue and not Capybara macthers! Thanks for the help though!

Related

Not able to click on POST button with xpath code

driver.findElement(By.xpath("//*[#id=\"ember558\"]")).click();
I am doing testing for linkedin post. I entered this code for POST button but that does not work. Can you please suggest about this?
It seems that #id attribute value is dynamic so, it will not be able to interact with the Element. You can try with dynamic X-path techniques. Generally, a set of data keeps on changing and rest data remains same.
e.g. <button "id":"emns1233">
here starting part of id will remain same every time but last part that is 1233 will change (may be each time). So, one can try to write X-path as
driver.findElement(By.xpath("//button[starts-with(#id,'emn')])).
Hope it helps. Thanks

Rails I18n not cascading

I would like to use the I18n cascading module as described here, but I cannot get the cascading to working.
I've a YAML file as follows:
follows:
every_x_day: "value here"
...
main:
...
In follows/main view, I'm calling the t helper for the key '.every_x_day', which is just short for 'follows.main.every_x_day'. No such key is of course found, so I would expect the cascading to look next for 'follows.every_x_day', but this does not happen.
I've seen the question and the answer here: I've added the
I18n.backend.class.send(:include, I18n::Backend::Cascade)
to application.rb. But when I call the translation helper with cascade: true, it simply does not cascade. The page just shows that the translation is missing (key name on magenta background).
I've also checked it with this i18n patch. It also shows that no cascading search happens. It just looks for the most specific key, doesn't find anything, and stops searching.
Any ideas what is causing this behaviour?
Ok, so I assume that you have something like this in your code:
<%= t('.every_x_day', cascade: true) %>
Are you completely sure, that '.every_x_day' starts looking for the key in 'follows.main.every_x_day' The way I describe it in the answer in my original question is the normal behaviour for the I18n and the lazy-lookup in views. So, the first thing I would try is to pass the full key into the helper:
<%= t('follows.main.every_x_day', cascade: true) %>
Then you can at least be sure, that your original lookup-path is correct.
If that doesn't work, then the cascading module isn't loaded.
Did you put the code inside the config-options? (see my updated_answer).
If it still doesn't work, come back at me and we will try to figure it out together.
Turns out the issue was caused by the I18n-monkeypatch (see the original question). After disabling the monkeypatch, the cascading took place as it should. This was carelessness on my part, apparently I never tested the app without the monkeypatch.
Anyway, thanks for taking the time to help klaffenboeck.

ListGrid: Need to show icons in an editable field for a many to one relationship

My problem seems simple. But have not been able to solve till now. Any help would be appreciated.
I have a listgrid showing certain records from a datasource. One of the fields is a many to one. When I try to edit any record, I get a dropdown with all the possible values that the record can have. All fine thus far. The issue is that all the dropdown values are displayed as simple text. I wish them to be displayed as shown in this link.
The requirement is to have "A SelectItem with icons" on the listgrid.
Regards
Use ListGridField.setEditorType() to customize the editor shown for a field. This API takes a FormItem, so pass a SelectItem configured similarly to the sample you linked to.

How to click on the second link with the same text using Capybara in Rails 3?

In a view file I have:
= link_to 'View', post
= link_to 'View', comment
In a spec file (I'm using Capybara):
click_on 'View'
It clicks on the first link, but I want it to click on the second one. How can I do it?
You could try to find all entries and deal with an array:
page.all('a')[1].click
Would help to have a class or use within to scope your search ;)
There's probably a few ways but I usually scope something like this.
within(".comment") do
click_on("View")
end
There's quite possibly/probably alternatives as well. I usually do my acceptance testing from cucumber, so my steps typically look like
When I follow "View" within the comment element
Where I have a step that translates within the comment element to a scoped call to the step itself (which I think is built into the latest capybara web_steps)
The worst thing about "the second" link is that it can become the third or the first or even the twenty fifth someday. So, scoping with a within block is the best way. Example:
within(".comment") do
click_on("View")
end
But if it is difficult to specify the link with a within scope (which sometimes it really is), I guess the way to click the second link with a certain text is:
find(:xpath, "(//a[text()='View'])[2]").click
In later versions of capybara (2.0.2, for example) both click_on 'View' and click_link 'View' will raise an ambiguous match error:
Failure/Error: click_on 'View'
Capybara::Ambiguous:
Ambiguous match, found 2 elements matching link or button "View"
So this won't do even if you want to click the first link (or if any link would be ok, which is my case).
As far as I understand this is made to force people write more specific tests where particular links are clicked.
It definitely could be tricky to debug the code if you accidentally placed two or more links with identical text and try to see what is happening. It's good to rely on something that is unlikely to change and specifying a link with a within block is a nice way to do this.
There are many ways for solving this type of problems.
Do it like this
if(page.find("a")[:href] == "comment")
click_on("View")
or
page.find("a:eq(2)").click
Remember javascript indexing starts with 0 while In Capybara, indexing starts with 1. So use a:eq(2) here for second href.
For capybara 2 solution:
within(".comment") do
click_on("View")
end
would not help if you have a few .comment. So simple use: page.first(:link, "View").click
This works for me if you have several rows of identical classes and you want to find the second row. Like a previous author mentioned, capybara indexing starts at 1.
within all(".trip-row")[2] do
assert page.has_content?("content")
end
If you use capybara-ui you could define the widget, or reusable DOM reference, for each widget.
# define your widget. in this case,
# we're defining it in a role
widget :view_post, ['.post', text: 'View']
widget :view_comment, ['.comment', text: 'View']
# then click that widget in the test
role.click :view_post
role.click :view_comment

auto_complete_for: prevent the first item from being auto-selected

The auto_complete_for dealio from script.aculo.us is great an all, but is there a way for me to selectively disable the fact that it always auto-selects the first item in the list?
The problem is that, if I want to type my own entry that is new, and novel, I don't want the first item in the list to be auto-selected. The reason is because when I TAB out of the field, it selects, and fills the text box with that first item.
I got around that, somewhat, by making the first item in the list the same as what I'm typing, but that's not perfect either, because the auto_complete list doesn't always update with every keystroke, depending on how fast I type. I've tried setting the list refresh rate to the lowest value (1 millisecond) but no go.
What I really want is an option in "auto_complete_for" that doesn't select that first item at all - the same way that Google Instant doesn't automatically select the first suggested search phrase - you have to arrow-down to select one.
Maybe I can do this via an HTML option that I'm missing?
Looking at the source, there doesn't appear to be an option for that, but I bet if you changed line 284 of controls.js to this.index = -1; it would do what you want.
Otherwise, it might be time to look for a different autocomplete widget.
If your requirements are too far away from the available plugin, then I guess there is no point in tinkering around. Its best to write your own JS code.
You might want to consider this: https://github.com/laktek/jQuery-Smart-Auto-Complete
or this : https://github.com/reinh/jquery-autocomplete
I'll add another alternative that works great with Rails 3:
http://github.com/crowdint/rails3-jquery-autocomplete
I recently implemented auto complete for more than a field for Rails 2.0.2.
The plugin I used is:- https://github.com/david-kerins/auto_complete . Not sure if it supports Rails 3.
I have also encountered issues on implementing the above scenario and have posted questions( Implementing auto complete for more than one field in Rails ; Implementing a OnClick kind of functionality and formatting wrt Rails Partial-Views ) on stackoverflow for the same, I have been lucky on getting things working for me based on my requirement.
Kindly refer to these questions, they might have relevance to your requirement.

Resources