flutter/packages/flutter_test/lib
Adam 37d5dc45d1
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));
```
2024-10-08 17:27:32 +00:00
..
fix_data Update flutter.dev links from misc packages to more permanent destinations (#150532) 2024-06-20 23:09:08 +00:00
src Add bySemanticsIdentifier finder for finding by identifier (#155571) 2024-10-08 17:27:32 +00:00
flutter_test.dart docImports for flutter_test (#151189) 2024-07-02 23:05:52 +00:00