getByAltText<E extends Element> function
Null safety
ByAltText
Queries
- Node container,
- dynamic text,
- {bool exact = true,
- NormalizerFn normalizer( )?}
Returns a single ImageElement, InputElement or AreaElement with the given text as the value of
the alt attribute, defaulting to an exact match.
Throws if no element is found within the provided container.
Use queryByAltText if a RTE is not expected.
Prefer using ByRole queries
for ImageElements and ByLabelText queries
for InputElements and AreaElements when possible in order to query for elements
in a way that most reflects how the user would interact with them.
Related: getAllByAltText
Example
The example below demonstrates the usage of the
getByAltTextquery. However, the example is also relevant forgetAllByAltText,queryByAltText,queryAllByAltText,findByAltTextandfindAllByAltText.Read more about the different types of queries to gain more clarity on which one suits your use-cases best.
<img alt="Incredibles 2 Poster" src="/incredibles-2.png" />
import 'package:react/react.dart' as react;
import 'package:react_testing_library/matchers.dart' show isInTheDocument;
import 'package:react_testing_library/react_testing_library.dart' as rtl;
import 'package:test/test.dart';
main() {
test('', () {
// Render the DOM shown in the example snippet above
final view = rtl.render(
react.img({'alt': 'Incredibles 2 Poster', 'src': '/incredibles-2.png'}),
);
expect(view.getByAltText(RegExp(r'incredibles.*? poster')), isInTheDocument);
});
}
NOTE:
render()supports React vDom elements / custom components created using either the react or over_react packages.The examples shown here use the
reactpackage since thereact_testing_librarydoes not have a direct dependency onover_react- but both libraries are fully supported.
Options
text
This argument can be set to a String, RegExp, or a Function which returns true for a match and false for a mismatch.
See the JS TextMatch docs for more details
and examples.
exact
Defaults to true; matches full strings, case-sensitive. When false, matches substrings and is
not case-sensitive. It has no effect on regex or function arguments. In most cases using a regex
instead of a string gives you more control over fuzzy matching and should be preferred over exact: false.
normalizer
An optional function which overrides normalization behavior.
Before running any matching logic against text in the DOM, DOM Testing Library automatically normalizes that text. By default, normalization consists of trimming whitespace from the start and end of text, and collapsing multiple adjacent whitespace characters into a single space.
If you want to prevent that normalization, or provide alternative normalization
(e.g. to remove Unicode control characters), you can provide a normalizer function.
This function will be given a string and is expected to return a normalized version of that string.
NOTE: Specifying a value for
normalizerreplaces the built-in normalization, but you can call getDefaultNormalizer to obtain a built-in normalizer, either to adjust that normalization or to call it from your own normalizer.
See the JS TextMatch precision and
JS TextMatch normalization docs
for more details and examples.
Implementation
E getByAltText<E extends Element>(
Node container,
/*TextMatch*/ dynamic text, {
bool exact = true,
NormalizerFn Function([NormalizerOptions?])? normalizer,
}) =>
within(container).getByAltText(text, exact: exact, normalizer: normalizer);