Reputation:
I want to use RSpec and Capybara to simulate clicking on an image, but I cannot do it.
My error is this:
Failure/Error: click_on "icon_red_heart.png"
Capybara::ElementNotFound:
Unable to find visible css "#image-button"
spec/features/posts_spec.rb
scenario "push an image" do
visit posts_path
expect(page).to have_current_path(posts_path)
find('#image-button').click
end
likes/_likes.html.erb
<%= button_to post_like_path(post, like), id: "image-button",method: :delete, remote: true do %>
<%= image_tag("icon_red_heart.png")%>
I don't know how to how to specify the image.
Please teach me.
Upvotes: 0
Views: 516
Reputation: 5213
As @jonrsharpe indicated, you are not referencing the button properly. The most resilient way to reference an element is to give it an id and reference it by that id.
Also, the button created by button_to
may have no content, in which case you'll need to tell Capybara that the button is not visible.
Change the button_to
line to this:
<%= button_to post_like_path(post, like), id: "image-button", remote: true do %>
Then change your test to read as follows:
scenario "push an image" do
visit posts_path # Use the name of your path here
find('#image-button', visible: false).click
end
Incidentally, using method: :delete
in your button_to
link doesn't do what you would expect. The method is automatically set to POST
, which is presumably what you want.
Upvotes: 1