Queries topic
Queries are the functions / methods that Testing Library gives you to find elements on the page. There are several types of queries ("get", "find", "query"); the difference between them is whether the query will throw an error if no element is found or if it will return a Future and retry. Depending on what page content you are selecting, different queries may be more or less appropriate. See the priority guide for recommendations on how to make use of semantic queries to test your page in the most accessible way.
Scoping Queries
Scoping a query to a specified container within the DOM can be done in a number of ways:
- By specifying the
containerargument on a "top level" query function. - By using the default container pre-defined for query methods exposed by the class instance returned from:
render(): scoped to the root of the renderedReactElementwithin(<container>): scoped to the provided<container>screen: scoped todocument.body
The function signatures for the queries are identical no matter which place you call them from with the exception of the "top level" query functions - which require that a container be specified as the first argument as shown in the examples below.
Examples
import 'package:react/react.dart' as react;
import 'package:react_testing_library/react_testing_library.dart' as rtl;
import 'package:test/test.dart';
main() {
final exampleVDom = react.nav({},
react.ul({},
react.li({}, 'Item 1'),
react.li({}, 'Item 2'),
react.li({},
'Item 3',
react.ul({},
react.li({}, 'Item 3.1'),
react.li({}, 'Item 3.2'),
),
),
),
);
test('', () {
final view = rtl.render(exampleVDom);
// Succeeds, there is only one in the react tree.
final goodNavQuery = view.getByRole('nav');
// This is identical to the first query, but shows how
// the top level queries must specify a container.
final moreVerboseNavQuery =
rtl.getByRole(view.container, 'nav');
// Fails, there is more than one element with the list role
// in the default scope used by the `RenderResult` instance
// returned from `rtl.render`. If we want to get only the
// nested list, we should use a more tightly scoped query
// like the one shown next.
final badSubListQuery = view.getByRole('list');
// Succeeds, there is only one list nested within the" "Item 3" item.
final goodSubListQuery =
rtl.within(view.getByText('Item 3')).getByRole('list');
// Lets say that our test above also renders an element that displays an
// overlay / "portal" dialog of some kind that renders in its own tree.
// This is where the `screen` queries come in handy - it allows you to
// easily query the entire document for something.
final documentQueryOutsideTheReactTree = rtl.screen.getByRole('dialog');
// If querying the entire document is too broad, you can still easily
// query outside the scope of the react tree rendered in the test
// by specifying a container using `within`:
final scopedQueryOutsideTheReactTree =
rtl.within(someElementWhereYouExpectTheDialog).getByRole('dialog');
});
}
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.
Classes
- RenderResult Queries
-
The model returned from render, which includes all the
ScopedQueriesscoped to the container that the renderedElement was rendered within. - WithinQueries Queries
- Queries scoped to the provided container.
Properties
- screen ↔ ScreenQueries Queries
-
Exposes all the "top-level" queries exposed by the dom-testing-library,
but the scope/container is defaulted to
document.body.read / write
Functions
-
findAllByAltText<
E extends Element> (Node container, dynamic text, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, Duration? timeout, Duration interval = defaultAsyncCallbackCheckInterval, QueryTimeoutFn? onTimeout, MutationObserverOptions mutationObserverOptions = defaultMutationObserverOptions}) → Future< Async ByAltText QueriesList< E> > -
Returns a list of ImageElements, InputElements and/or AreaElements with the given
textas the value of thealtattribute, defaulting to anexactmatch after waiting1000ms(or the specifiedtimeoutduration). -
findAllByDisplayValue<
E extends Element> (Node container, dynamic value, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, Duration? timeout, Duration interval = defaultAsyncCallbackCheckInterval, QueryTimeoutFn? onTimeout, MutationObserverOptions mutationObserverOptions = defaultMutationObserverOptions}) → Future< Async ByDisplayValue QueriesList< E> > -
Returns a list of InputElement, TextAreaElement or SelectElement that has the matching
valuedisplayed, defaulting to anexactmatch after waiting 1000ms (or the providedtimeoutduration). -
findAllByLabelText<
E extends Element> (Node container, dynamic text, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, String? selector, Duration? timeout, Duration interval = defaultAsyncCallbackCheckInterval, QueryTimeoutFn? onTimeout, MutationObserverOptions mutationObserverOptions = defaultMutationObserverOptions}) → Future< Async ByLabelText QueriesList< E> > -
Returns a list of elements that are associated with a LabelElement with the given
text, defaulting to anexactmatch after waiting 1000ms (or the providedtimeoutduration). -
findAllByPlaceholderText<
E extends Element> (Node container, dynamic text, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, Duration? timeout, Duration interval = defaultAsyncCallbackCheckInterval, QueryTimeoutFn? onTimeout, MutationObserverOptions mutationObserverOptions = defaultMutationObserverOptions}) → Future< Async ByPlaceholderText QueriesList< E> > -
Returns a list of elements with the given
textas the value of theplaceholderattribute, defaulting to anexactmatch after waiting1000ms(or the specifiedtimeoutduration). -
findAllByRole<
E extends Element> (Node container, dynamic role, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, dynamic name, bool? selected, bool? checked, bool? pressed, bool? expanded, bool queryFallbacks = false, int? level, Duration? timeout, Duration interval = defaultAsyncCallbackCheckInterval, QueryTimeoutFn? onTimeout, MutationObserverOptions mutationObserverOptions = defaultMutationObserverOptions}) → Future< Async ByRole QueriesList< E> > -
Returns a list of elements with the given
rolevalue, defaulting to anexactmatch after waiting 1000ms (or the providedtimeoutduration). -
findAllByTestId<
E extends Element> (Node container, dynamic testId, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, Duration? timeout, Duration interval = defaultAsyncCallbackCheckInterval, QueryTimeoutFn? onTimeout, MutationObserverOptions mutationObserverOptions = defaultMutationObserverOptions}) → Future< Async ByTestId QueriesList< E> > -
Returns a list of elements with the given
testIdvalue for thedata-test-idattribute, defaulting to anexactmatch after waiting 1000ms (or the providedtimeoutduration). -
findAllByText<
E extends Element> (Node container, dynamic text, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, String? selector, dynamic ignore = 'script', Duration? timeout, Duration interval = defaultAsyncCallbackCheckInterval, QueryTimeoutFn? onTimeout, MutationObserverOptions mutationObserverOptions = defaultMutationObserverOptions}) → Future< Async ByText QueriesList< E> > -
Returns a list of elements with the given
textcontent, defaulting to anexactmatch after waiting 1000ms (or the providedtimeoutduration). -
findAllByTitle<
E extends Element> (Node container, dynamic title, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, Duration? timeout, Duration interval = defaultAsyncCallbackCheckInterval, QueryTimeoutFn? onTimeout, MutationObserverOptions mutationObserverOptions = defaultMutationObserverOptions}) → Future< Async ByTitle QueriesList< E> > -
Returns a list of elements with the given
titleas the value of thetitleattribute, defaulting to anexactmatch after waiting1000ms(or the specifiedtimeoutduration). -
findByAltText<
E extends Element> (Node container, dynamic text, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, Duration? timeout, Duration interval = defaultAsyncCallbackCheckInterval, QueryTimeoutFn? onTimeout, MutationObserverOptions mutationObserverOptions = defaultMutationObserverOptions}) → Future< Async ByAltText QueriesE> -
Returns a future with a single ImageElement, InputElement or AreaElement value with the given
textas the value of thealtattribute, defaulting to anexactmatch after waiting1000ms(or the specifiedtimeoutduration). -
findByDisplayValue<
E extends Element> (Node container, dynamic value, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, Duration? timeout, Duration interval = defaultAsyncCallbackCheckInterval, QueryTimeoutFn? onTimeout, MutationObserverOptions mutationObserverOptions = defaultMutationObserverOptions}) → Future< Async ByDisplayValue QueriesE> -
Returns a future with a single InputElement, TextAreaElement or SelectElement that has the matching
valuedisplayed, defaulting to anexactmatch after waiting 1000ms (or the providedtimeoutduration). -
findByLabelText<
E extends Element> (Node container, dynamic text, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, String? selector, Duration? timeout, Duration interval = defaultAsyncCallbackCheckInterval, QueryTimeoutFn? onTimeout, MutationObserverOptions mutationObserverOptions = defaultMutationObserverOptions}) → Future< Async ByLabelText QueriesE> -
Returns a future with a single element that is associated with a LabelElement with the given
text, defaulting to anexactmatch after waiting 1000ms (or the providedtimeoutduration). -
findByPlaceholderText<
E extends Element> (Node container, dynamic text, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, Duration? timeout, Duration interval = defaultAsyncCallbackCheckInterval, QueryTimeoutFn? onTimeout, MutationObserverOptions mutationObserverOptions = defaultMutationObserverOptions}) → Future< Async ByPlaceholderText QueriesE> -
Returns a future with a single element value with the given
textas the value of theplaceholderattribute, defaulting to anexactmatch after waiting1000ms(or the specifiedtimeoutduration). -
findByRole<
E extends Element> (Node container, dynamic role, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, dynamic name, bool? selected, bool? checked, bool? pressed, bool? expanded, bool queryFallbacks = false, int? level, Duration? timeout, Duration interval = defaultAsyncCallbackCheckInterval, QueryTimeoutFn? onTimeout, MutationObserverOptions mutationObserverOptions = defaultMutationObserverOptions}) → Future< Async ByRole QueriesE> -
Returns a future with a single element value with the given
rolevalue, defaulting to anexactmatch after waiting 1000ms (or the providedtimeoutduration). -
findByTestId<
E extends Element> (Node container, dynamic testId, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, Duration? timeout, Duration interval = defaultAsyncCallbackCheckInterval, QueryTimeoutFn? onTimeout, MutationObserverOptions mutationObserverOptions = defaultMutationObserverOptions}) → Future< Async ByTestId QueriesE> -
Returns a future with a single element value with the given
testIdvalue for thedata-test-idattribute, defaulting to anexactmatch after waiting 1000ms (or the providedtimeoutduration). -
findByText<
E extends Element> (Node container, dynamic text, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, String? selector, dynamic ignore = 'script', Duration? timeout, Duration interval = defaultAsyncCallbackCheckInterval, QueryTimeoutFn? onTimeout, MutationObserverOptions mutationObserverOptions = defaultMutationObserverOptions}) → Future< Async ByText QueriesE> -
Returns a future with a single element value with the given
textcontent, defaulting to anexactmatch after waiting 1000ms (or the providedtimeoutduration). -
findByTitle<
E extends Element> (Node container, dynamic title, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, Duration? timeout, Duration interval = defaultAsyncCallbackCheckInterval, QueryTimeoutFn? onTimeout, MutationObserverOptions mutationObserverOptions = defaultMutationObserverOptions}) → Future< Async ByTitle QueriesE> -
Returns a future with a single element value with the given
titleas the value of thetitleattribute, defaulting to anexactmatch after waiting1000ms(or the specifiedtimeoutduration). -
getAllByAltText<
E extends Element> (Node container, dynamic text, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?}) → List< ByAltText QueriesE> -
Returns a list of ImageElements, InputElements and/or AreaElements with the given
textas the value of thealtattribute, defaulting to anexactmatch. -
getAllByDisplayValue<
E extends Element> (Node container, dynamic value, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?}) → List< ByDisplayValue QueriesE> -
Returns a list of InputElements, TextAreaElements or SelectElements that have the matching
valuedisplayed, defaulting to anexactmatch. -
getAllByLabelText<
E extends Element> (Node container, dynamic text, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, String? selector}) → List< ByLabelText QueriesE> -
Returns a list of elements that are associated with a LabelElement with the given
text, defaulting to anexactmatch. -
getAllByPlaceholderText<
E extends Element> (Node container, dynamic text, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?}) → List< ByPlaceholderText QueriesE> -
Returns a list of elements with the given
textas the value of theplaceholderattribute, defaulting to anexactmatch. -
getAllByRole<
E extends Element> (Node container, dynamic role, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, dynamic name, bool? selected, bool? checked, bool? pressed, bool? expanded, bool queryFallbacks = false, int? level}) → List< ByRole QueriesE> -
Returns a list of elements with the given
rolevalue, defaulting to anexactmatch. -
getAllByTestId<
E extends Element> (Node container, dynamic testId, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?}) → List< ByTestId QueriesE> -
Returns a list of elements with the given
testIdvalue for thedata-test-idattribute, defaulting to anexactmatch. -
getAllByText<
E extends Element> (Node container, dynamic text, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, String? selector, dynamic ignore = 'script'}) → List< ByText QueriesE> -
Returns a list of elements with the given
textcontent, defaulting to anexactmatch. -
getAllByTitle<
E extends Element> (Node container, dynamic title, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?}) → List< ByTitle QueriesE> -
Returns a list of elements with the given
titleas the value of thetitleattribute, defaulting to anexactmatch. -
getByAltText<
E extends Element> (Node container, dynamic text, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?}) → E ByAltText Queries -
Returns a single ImageElement, InputElement or AreaElement with the given
textas the value of thealtattribute, defaulting to anexactmatch. -
getByDisplayValue<
E extends Element> (Node container, dynamic value, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?}) → E ByDisplayValue Queries -
Returns a single InputElement, TextAreaElement or SelectElement that has the matching
valuedisplayed, defaulting to anexactmatch. -
getByLabelText<
E extends Element> (Node container, dynamic text, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, String? selector}) → E ByLabelText Queries -
Returns a single element that is associated with a LabelElement with the given
text, defaulting to anexactmatch. -
getByPlaceholderText<
E extends Element> (Node container, dynamic text, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?}) → E ByPlaceholderText Queries -
Returns a single element with the given
textas the value of theplaceholderattribute, defaulting to anexactmatch. -
getByRole<
E extends Element> (Node container, dynamic role, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, dynamic name, bool? selected, bool? checked, bool? pressed, bool? expanded, bool queryFallbacks = false, int? level}) → E ByRole Queries -
Returns a single element with the given
rolevalue, defaulting to anexactmatch. -
getByTestId<
E extends Element> (Node container, dynamic testId, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?}) → E ByTestId Queries -
Returns a single element with the given
testIdvalue for thedata-test-idattribute, defaulting to anexactmatch. -
getByText<
E extends Element> (Node container, dynamic text, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, String? selector, dynamic ignore = 'script'}) → E ByText Queries -
Returns a single element with the given
textcontent, defaulting to anexactmatch. -
getByTitle<
E extends Element> (Node container, dynamic title, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?}) → E ByTitle Queries -
Returns a single element with the given
titleas the value of thetitleattribute, defaulting to anexactmatch. -
queryAllByAltText<
E extends Element> (Node container, dynamic text, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?}) → List< ByAltText QueriesE> -
Returns a list of ImageElements, InputElements and/or AreaElements with the given
textas the value of thealtattribute, defaulting to anexactmatch. -
queryAllByDisplayValue<
E extends Element> (Node container, dynamic value, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?}) → List< ByDisplayValue QueriesE> -
Returns a list of InputElements, TextAreaElements or SelectElements that have the matching
valuedisplayed, defaulting to anexactmatch. -
queryAllByLabelText<
E extends Element> (Node container, dynamic text, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, String? selector}) → List< ByLabelText QueriesE> -
Returns a list of elements that are associated with a LabelElement with the given
text, defaulting to anexactmatch. -
queryAllByPlaceholderText<
E extends Element> (Node container, dynamic text, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?}) → List< ByPlaceholderText QueriesE> -
Returns a list of elements with the given
textas the value of theplaceholderattribute, defaulting to anexactmatch. -
queryAllByRole<
E extends Element> (Node container, dynamic role, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, dynamic name, bool? selected, bool? checked, bool? pressed, bool? expanded, bool queryFallbacks = false, int? level}) → List< ByRole QueriesE> -
Returns a list of elements with the given
rolevalue, defaulting to anexactmatch. -
queryAllByTestId<
E extends Element> (Node container, dynamic testId, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?}) → List< ByTestId QueriesE> -
Returns a list of elements with the given
testIdvalue for thedata-test-idattribute, defaulting to anexactmatch. -
queryAllByText<
E extends Element> (Node container, dynamic text, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, String? selector, dynamic ignore = 'script'}) → List< ByText QueriesE> -
Returns a list of elements with the given
textcontent, defaulting to anexactmatch. -
queryAllByTitle<
E extends Element> (Node container, dynamic title, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?}) → List< ByTitle QueriesE> -
Returns a list of elements with the given
titleas the value of thetitleattribute, defaulting to anexactmatch. -
queryByAltText<
E extends Element> (Node container, dynamic text, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?}) → E? ByAltText Queries -
Returns a single ImageElement, InputElement or AreaElement with the given
textas the value of thealtattribute, defaulting to anexactmatch. -
queryByDisplayValue<
E extends Element> (Node container, dynamic value, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?}) → E? ByDisplayValue Queries -
Returns a single InputElement, TextAreaElement or SelectElement that has the matching
valuedisplayed, defaulting to anexactmatch. -
queryByLabelText<
E extends Element> (Node container, dynamic text, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, String? selector}) → E? ByLabelText Queries -
Returns a single element that is associated with a LabelElement with the given
text, defaulting to anexactmatch. -
queryByPlaceholderText<
E extends Element> (Node container, dynamic text, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?}) → E? ByPlaceholderText Queries -
Returns a single element with the given
textas the value of theplaceholderattribute, defaulting to anexactmatch. -
queryByRole<
E extends Element> (Node container, dynamic role, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, dynamic name, bool? selected, bool? checked, bool? pressed, bool? expanded, bool queryFallbacks = false, int? level}) → E? ByRole Queries -
Returns a single element with the given
rolevalue, defaulting to anexactmatch. -
queryByTestId<
E extends Element> (Node container, dynamic testId, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?}) → E? ByTestId Queries -
Returns a single element with the given
testIdvalue for thedata-test-idattribute, defaulting to anexactmatch. -
queryByText<
E extends Element> (Node container, dynamic text, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?, String? selector, dynamic ignore = 'script'}) → E? ByText Queries -
Returns a single element with the given
textcontent, defaulting to anexactmatch. -
queryByTitle<
E extends Element> (Node container, dynamic title, {bool exact = true, NormalizerFn normalizer([NormalizerOptions?])?}) → E? ByTitle Queries -
Returns a single element with the given
titleas the value of thetitleattribute, defaulting to anexactmatch. -
within(
Node node) → WithinQueries Queries -
Takes a DOM
nodeand binds it to the raw query functions.