Test image alt attributes
All non-text content must have a text alternative that serves the equivalent purpose, except for decorative images which should have null alt text.
Alt text comparison
Hero image on travel website
Product image in e-commerce
Data visualization
Decorative separator
Do's and don'ts
When to use what
Is the image purely decorative?
├── YES → alt="" (empty string)
└── NO → Does it contain text?
├── YES → Include that text in alt
└── NO → Is it a link or button?
├── YES → Describe the action/destination
└── NO → Is it a complex image (chart, diagram)?
├── YES → Summarize + provide detailed description elsewhere
└── NO → Describe the content and purpose conciselyTesting hints
import AxeBuilder from '@axe-core/playwright';
// Check for missing alt text
const results = await new AxeBuilder({ page })
.withRules([
'image-alt', // All images have alt
'image-redundant-alt', // No "image of"
'input-image-alt', // Input type="image"
'role-img-alt' // role="img" elements
])
.analyze();
// Find images with poor alt text
const images = await page.$$('img');
for (const img of images) {
const alt = await img.getAttribute('alt');
const src = await img.getAttribute('src');
// Check for common issues
if (alt === null) {
console.log('Missing alt:', src);
} else if (/\.(jpg|png|gif)$/i.test(alt)) {
console.log('Filename as alt:', alt);
} else if (/^(image|photo|picture)/i.test(alt)) {
console.log('Redundant prefix:', alt);
}
}// Get all images for review
const imageData = await page.$$eval('img', imgs =>
imgs.map(img => ({
src: img.src,
alt: img.alt,
role: img.role,
isDecorative: img.alt === '' ||
img.role === 'presentation',
inLink: img.closest('a') !== null,
dimensions: {
width: img.width,
height: img.height
}
}))
);
// Flag images needing review
const needsReview = imageData.filter(img =>
!img.isDecorative &&
(!img.alt || img.alt.length < 5)
);
console.log('Images needing review:',
needsReview
);Automation hints
image-alt, image-redundant-altimg.getAttribute('alt') !== nullexpect(img).toHaveAttribute('alt', '')