ByAltText

If you need to write a test and get or interact with an image, you can easily get the <img> element with getByAltText().

Using this function encourages appropriate alt text, which is good for accessibility.

(Remember decorative images should use empty alt="" so they're ignored by assistive tech.)

Lesson covers:

findAllByAltText(), findByAltText(), getAllByAltText(), getByAltText(), queryAllByAltText(), queryByAltText()

In this lesson we will go over simple ways to use it, and why you probably won't need it for most tests when finding images.

Loading full lesson content & code...

Ready to try running tests for this lesson?