getByTestId function

dynamic getByTestId(
  1. dynamic root,
  2. String value,
  3. {String key = defaultTestIdKey}
)

Returns the first descendant of root that has its key test ID prop value set to a space-delimited string containing value, or null if no matching descendant can be found.

This method works for:

  • ReactComponent (composite component) render trees (output of render)
  • ReactElement trees (output of renderShallow/Component.render)

Example:

var renderedInstance = render(Dom.div()(
  // Div1
  (Dom.div()..addTestId('first'))(),

  Dom.div()(
    // Div2
    (Dom.div()
      ..addTestId('second')
      ..addTestId('other-id')
    )(),
  ),
));

var first  = getByTestId(renderedInstance, 'first');    // Returns the `Div1` element
var second = getByTestId(renderedInstance, 'second');   // Returns the `Div2` element
var other  = getByTestId(renderedInstance, 'other-id'); // Returns the `Div2` element
var nonexistent = getByTestId(renderedInstance, 'nonexistent'); // Returns `null`

It is recommended that, instead of setting this key prop manually, you should use the UiProps.addTestId method so the prop is only set in a test environment.

Implementation

/* [1] */ getByTestId(dynamic root, String value, {String key = defaultTestIdKey}) {
  final results = getAllByTestId(root, value, key: key);
  return results.isEmpty ? null : results.first;
}