Add bySemanticsIdentifier finder for finding by identifier (#155571)

## Add `bySemanticsIdentifier` finder for finding by identifier

### Description

This pull request introduces a new finder, `CommonFinders.bySemanticsIdentifier`, to the Flutter testing framework. This finder allows developers to locate `Semantics` widgets based on their `identifier` property, enhancing the precision and flexibility of widget tests.

### Motivation

Establish a consistent and reliable method for locating elements in integration and end-to-end (e2e) tests. Unlike `label` or `key`, which may carry functional significance within the application, the `identifier` is purely declarative and does not impact functionality. Utilizing the `identifier` for finding semantics widgets ensures that tests can target specific elements without interfering with the app's behavior, thereby enhancing test reliability, maintainability, and reusability across testing frameworks.

### Changes

- **semantics.dart**
  - Updated documentation to mention that `identifier` can be matched using `CommonFinders.bySemanticsIdentifier`.
  
- **finders.dart**
  - Added the `bySemanticsIdentifier` method to `CommonFinders`.
  - Supports both exact string matches and regular expression patterns.
  - Includes error handling to ensure semantics are enabled during tests.
  
- **finders_test.dart**
  - Added tests to verify that `bySemanticsIdentifier` correctly finds widgets by exact identifier and regex patterns.
  - Ensures that the finder behaves as expected when semantics are not enabled.

### Usage

Developers can use the new finder in their tests as follows:

```dart
// Exact match
expect(find.bySemanticsIdentifier('Back'), findsOneWidget);

// Regular expression match
expect(find.bySemanticsIdentifier(RegExp(r'^item-')), findsNWidgets(2));
```
This commit is contained in:
Adam
2024-10-08 13:27:32 -04:00
committed by GitHub
parent 138144bb2f
commit 37d5dc45d1
3 changed files with 100 additions and 12 deletions

View File

@@ -299,6 +299,55 @@ void main() {
expect(find.bySemanticsLabel('Foo'), findsOneWidget);
semanticsHandle.dispose();
});
testWidgets('Throws StateError if semantics are not enabled (bySemanticsIdentifier)', (WidgetTester tester) async {
expect(
() => find.bySemanticsIdentifier('Add'),
throwsA(
isA<StateError>().having(
(StateError e) => e.message,
'message',
contains('Semantics are not enabled'),
),
),
);
}, semanticsEnabled: false);
testWidgets('finds Semantically labeled widgets by identifier', (WidgetTester tester) async {
final SemanticsHandle semanticsHandle = tester.ensureSemantics();
await tester.pumpWidget(_boilerplate(
Semantics(
identifier: 'Add',
button: true,
child: const TextButton(
onPressed: null,
child: Text('+'),
),
),
));
expect(find.bySemanticsIdentifier('Add'), findsOneWidget);
semanticsHandle.dispose();
});
testWidgets('finds Semantically labeled widgets by identifier RegExp', (WidgetTester tester) async {
final SemanticsHandle semanticsHandle = tester.ensureSemantics();
// list of elements with a prefixed identifier
await tester.pumpWidget(_boilerplate(
Row(children: <Widget>[
Semantics(
identifier: 'item-1',
child: const Text('Item 1'),
),
Semantics(
identifier: 'item-2',
child: const Text('Item 2'),
),
]),
));
expect(find.bySemanticsIdentifier('item'), findsNothing);
expect(find.bySemanticsIdentifier(RegExp(r'^item-')), findsNWidgets(2));
semanticsHandle.dispose();
});
});
group('byTooltip', () {