I'm using rails 6.1, ruby 3.1.1 with capybara
I'm learning feature specs and I've never used xpath before. I have a page/view with several images that I'd like to have tests for - to be sure they are present. The following is my first attempt on just one image:
# in my view:
# all images on this view are in 'app/assets/images/bars/'
<% @levels.each do |l| %>
<% image=l.color.downcase + '-bar.png' %>
<p><%= image_tag("bars/#{image}", id: l.color.downcase, :size => "25x50") %></p>
<% end %>
# in my feature specs
scenario "it has the level 1 image" do
# /app/assets/images/bars/white-bar.png is level 1 image
expect(page).to have_xpath("/img[contains( @src, 'white-bar' )]")
end
I've tried a couple other variations on the image path and name, but they didn't work either:
expect(page).to have_xpath("/img[contains( @src, 'white-bar.png' )]")
expect(page).to have_xpath("/img[contains( @src, '/bars/white-bar.png' )]")
expect(page).to have_xpath("/img[contains( @src, '/images/bars/white-bar.png' )]")
From what I've read on xpath, "I believe" it is looking for an image that contains 'white-bar' in the name. All images on the page have unique names, so it should only see one image with white-bar in the name.
NOTE: when I inspect the image using developer-tools I see this:
/assets/bars/white-bar-31c13c54bf0beaccadbdcb342490b30d380f235669ec6c31b316f6ed8100722f.png
/assets/bars/... it doesn't have the /images in the path.
I know the long number is something added in by rails, but I don't know if it is causing any issues in the test.
Any help on explaining how this works and what I'm missing would be greatly appreciated.
Thanks