How to click first link in list of items after upgrading to Capybara 2.0?
You can just use:
first('.item').click_link('Agree')
or
first('.item > a').click
(if your default selector is :css)
Code in your question doesn't work as:
within ".item" do
first(:link, "Agree").click
end
is equivalent to:
find('.item').first(:link, "Agree").click
Capybara finds several .item
's so it raises an exception. I consider this behavior of Capybara 2 very good.
how to click one item in list of items with capybara
Ideally you would add a unique id as @juan-manuel-rodulfo-salcedo suggested. However, if you can't do that then you could find all the divs with class=search-result
. Below is an example of how to click the second div on the page:
page.all('.search-result')[1].click
For more examples of how to select multiple elements, see this SO answer.
Rails capybara click label with link
When you tell Capybara to click on an element it clicks in the middle of that element which in this case is where you have the conditions
link, so that takes the click before the label element. One option would be to specifically click on the abbr element, which is in the label but away from the link
find('label[for="influencer_agree_terms"] abbr').click
Another option would be to take advantage of the fact that allow_label_click supports specifying offsets for the click location
check('influencer[agree_terms]', allow_label_click: { x: 10, y: 10 })
Depending on the setting of Capybara.w3c_click_offset
that offset could be from the center or from the top left corner of the label element so adjust the values as necessary. Technically you can do the same thing when calling click
find('label[for="influencer_agree_terms"]').click(x: 10, y: 10)
but it's generally better to use check
when dealing with a checkbox
Clicking link using capybara
You can do it like
page.find('.x-round-button').click
Href link click using capybara
page.all(:link,"Graduation") returns Ambiguous match, found 2 elements matching link "Graduation"
That means page contains multiple graduation text link.so if you want to click the first one you can write like this
page.all(:link,"Graduation")[0].click
It will click the first link and the below one click the second link.You can use anyone as your needs.
page.all(:link,"Graduation")[1].click
am sure this will work for you
click the specific link using capybara
If your plant names are unique enough you can do
find('tr', text: plant_name).click_link('Select')
This will work because the :text options matches a substring of the visible text in the tr element, and then the click_link is scoped inside the found tr. It could fail if you have any plant name that is a substring of another plants name, or included into another plants description, etc - but that can handled for testing by selecting plant names/details in your test set wisely. If you really need to test with plant names that are substrings of other plant names/details the initial find can be rewritten using an xpath and the ancestor axis.
How to click a span in capybara tests?
The data-testid
attribute is on the input
element not on the span, so find("span[data-testid='prepay-enrollment-modal-agreement-checkbox']")
won't work. If you put the data-testid attribute on the span rather than the input then it would work, however there's a better way. Since the checkbox has an associated label element you can just tell Capybara it's allowed to click on the label element if necessary. First, since you're using test ids, set
Capybara.test_id = 'data-testid'
in your global config, and then in your test just do
page.check('prepay-enrollment-modal-agreement-checkbox', allow_label_click: true)
Note: rather than expect(page).to have_selector("[data-testid='review-step-container']")
you should use expect(page).to have_css("[data-testid='review-step-container']")
to make it clearer that you know you're passing CSS. Also for test speed reasons you may want to combine all of the have_content
calls into one using a regex
Capybara::Ambiguous match, found 2 elements matching xpath - Find 1st link
Since one of the links is in the title h2 you can use that to scope the find and remove the ambiguity
find(".blog-post-title > a[href=`#{post_path(post)}`]").click # always better to use post_path than hardcode the id
You could also do first(:link, href: post_path(post)).click
but first
(like all
) has the disadvantage of not having waiting/retrying behavior so unless you're sure the page is fully loaded when called it's best to avoid it (or enable waiting/retrying on it by specifying one of the count options first(:link, href: post_path(post), minimum: 1).click
).
If you need to click blog title links a lot you could also create a custom selector with something like
Capybara.add_selector(:title_link) do
css do |post|
".blog-post-title > a[href=`#{post_path(post)}`]"
end
end
which would then allow you to do
find(:title_link, post).click
Related Topics
What Does ::Myclass Ruby Scope Operator Do
Ruby-Debug19 on Ruby-1.9.3-Preview1
Problems Setting a Custom Primary Key in a Rails 4 Migration
Deleting a Modified Object from a Set in a No-Op
Jekyll Serve Dependency Error - Could Not Open 'Lib Curl'
In Ruby Why Does Nil.Id Return 4
In Ruby, Can You Perform String Interpolation on Data Read from a File
Ruby Mocha: Is There an Equivalent to Rspec-Mocks' #And_Call_Original
301 Moved Permanently After S3 Uploading
How to Identify Aliased Methods in Ruby
Undefined Method with "_Path" While Using Rails Form_For
Returning Data from Forked Processes
Query Records Through Its Belongs_To Relation in Rails
How to Get Indexes of All Occurrences of a Pattern in a String