2

I have been trying to use Capybara to match actual image file name with expected file name.

I have images that are stored in app/assets/images, e.g. image01.png.

The file name of these images are stored in the image_file_name field of one or more tables.

They are being rendered using <%= @object.image_file_name %> in one of more html.erb file. However, the actual file name rendered has a series of alphanumeric characters at the right hand side, e.g. assets/image01-d4f7d162a9ecb8877e69bc96a8ca03bd7531c8faf7be7c65ba60c14e7c6fe530.png'.

I've tried the following expectations and they work:

expect(page).to have_css("img[src*='image01-d4f7d162a9ecb8877e69bc96a8ca03bd7531c8faf7be7c65ba60c14e7c6fe530.png']")
expect(page).to have_css("img[src*='image01']")

May I know is there a way to validate that the image file name starts with image01 and ends with .png regardless of what are in between? If that couldn't be achieved with hav_css, how about have_selector or have_xpath?

Waihon Yew
  • 65
  • 6

1 Answers1

2

You can apply multiple CSS attribute selectors to the same field so

expect(page).to have_css(“img[src^=‘image01’][src$=‘.png’]”)

should do what you want

Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78
  • Since the ```src``` actually contains the directory name ```assets/```, I have adapted the suggested answer as follows, and it works: ```expect(page).to have_css("img[src^='assets/image01'][src$='.png']")``` – Waihon Yew Dec 15 '19 at 05:59