How do I test an image src and alt value using capybara?

Viewed 16939

I'm trying to write test the value of alt text + src of an image using capybara and the css selectors.

any idea s to test both in single xpath ?

7 Answers

In 2022, this works perfectly:

find("img[src='https://some-url.com/image.jpg']")

or with embedded ruby

find("img[src='#{@post.image}']")

Here is an example I recently used:

test "Showing a company" do
  visit companies_path
  click_link @company.name
  
  assert_title @company.name
  assert_selector :link, href: @company.url
  assert_selector "h3",  text: @company.title
  assert_selector "p",   text: @company.description

  find("img[src='#{@company.image}']")
end
Related