Sub topic 15
Automated Accessibility Checks in Widget Tests
Assume you know basic Dart, Flutter layouts, and widget tests. Automated checks do not replace TalkBack, VoiceOver, keyboard, or large-text testing. They provide a fast safety net that catches common regressions whenever you run the test suite.
What accessibility tests can check
Flutter's flutter_test package includes guidelines for common accessibility requirements:
androidTapTargetGuidelinechecks that interactive targets are large enough for Android.iOSTapTargetGuidelinechecks the corresponding iOS target size.labeledTapTargetGuidelinechecks that tappable controls have accessible labels.textContrastGuidelinechecks whether text has enough contrast against its background.
These tests inspect the rendered widget tree and its semantics. They can catch an unlabeled icon button or a button whose hit area became too small during a layout change.
A complete widget test
The following test defines a small page and checks four guidelines.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () {},
child: const Text('Continue'),
),
),
);
}
}
void main() {
testWidgets('home page meets accessibility guidelines', (tester) async {
final semanticsHandle = tester.ensureSemantics();
await tester.pumpWidget(
const MaterialApp(
home: HomePage(),
),
);
await expectLater(
tester,
meetsGuideline(androidTapTargetGuideline),
);
await expectLater(
tester,
meetsGuideline(iOSTapTargetGuideline),
);
await expectLater(
tester,
meetsGuideline(labeledTapTargetGuideline),
);
await expectLater(
tester,
meetsGuideline(textContrastGuideline),
);
semanticsHandle.dispose();
});
}ensureSemantics() enables the semantics tree during the test. Without it, semantics-dependent checks may not inspect the accessibility information you expect. Always dispose of the returned handle when the test finishes.
Standard widgets usually provide useful semantics automatically. For example, ElevatedButton exposes a button role and uses its child text as the label. A bare GestureDetector around an icon does not provide the same information unless you add a Semantics widget.
Interpreting failures
A failing guideline identifies a symptom, not always the complete cause. Inspect the widget that failed and ask:
- Does the control have a visible or semantic label?
- Is the entire intended area interactive?
- Did a fixed height become too small?
- Does the text color contrast with its actual background?
- Is a custom gesture missing a semantic role or action?
Run these checks in continuous integration so every change receives the same baseline review. Keep manual testing in the release process because automated guidelines cannot judge reading order, wording quality, gesture discoverability, or whether a screen reader announcement makes sense.
Exercise
Why should a widget test call tester.ensureSemantics() before running accessibility guidelines?
Show answer
It enables the semantics tree so the test can inspect labels, roles, actions, and other accessibility information exposed by the widgets.