# Change the enterText method to move the caret to the end of the input text

> WidgetTester.enterText and TestTextInput.enterText now move the caret to the end of the input text.




:::important
These breaking change docs are accurate, as of the release
under which they are published. Over time, the
workarounds described here might become inaccurate.
We don't, in general, keep these breaking change docs up
to date as of each release.

这些破坏性改动文档的准确性仅限于其发布时对应的版本。
随着时间的推移，文档中描述的方案可能会逐渐失效。
通常情况下，我们不会在每次发布新版本时都对这些文档进行同步更新。

The [breaking change index file](/release/breaking-changes)
lists the docs created for each release.

[破坏性改动列表](/release/breaking-changes) 
列出了每个版本的文档。

:::


## Summary

The `WidgetTester.enterText` and `TestTextInput.enterText` methods
now move the caret to the end of the input text.

## Context

The caret indicates the insertion point within the current text in an
active input field. Typically, when a new character is entered, the
caret stays immediately after it. In Flutter the caret position is
represented by a collapsed selection. When the selection is invalid,
usually the user won't be able to modify or add text until they
change the selection to a valid value.

`WidgetTester.enterText` and `TestTextInput.enterText` are 2 methods
used in tests to replace the content of the target text field. Prior
to this change, `WidgetTester.enterText` and `TestTextInput.enterText`
set the selection to an invalid range (-1, -1), indicating there's
no selection or caret. This contradicts the typical behavior of an
input field.

## Description of change

In addition to replacing the text with the supplied text,
`WidgetTester.enterText` and `TestTextInput.enterText` now set the
selection to `TextSelection.collapsed(offset: text.length)`, instead
of `TextSelection.collapsed(offset: -1)`.

## Migration guide

It should be very uncommon for tests to have to rely on the
previous behavior of `enterText`, since usually the selection
should not be invalid. **Consider changing the expected values of
your tests to adopt the `enterText` change.**

Common test failures this change may introduce includes:

- Golden test failures:

  The caret appears at the end of the text, as opposed to before
  the text prior to the change.

- Different `TextEditingValue.selection` after calling `enterText`:

  The text field's `TextEditingValue` now has a collapsed
  selection with a non-negative offset, as opposed to
  `TextSelection.collapsed(offset: -1)` prior to the change.
  For instance, you may see
  `expect(controller.value.selection.baseOffset, -1);`
  failing after `enterText` calls.

If your tests have to rely on setting the selection to invalid,
the previous behavior can be achieved using`updateEditingValue`:

### `TestTextInput.enterText`

Code before migration:

```dart
await testTextInput.enterText(text);
```

Code after migration:

```dart
await testTextInput.updateEditingValue(TextEditingValue(
  text: text,
));
```

### `WidgetTester.enterText`

Code before migration:

```dart
await tester.enterText(finder, text);
```

Code after migration:

```dart
await tester.showKeyboard(finder);
await tester.updateEditingValue(TextEditingValue(
  text: text,
));
await tester.idle();
```

## Timeline

Landed in version: 2.1.0-13.0.pre<br>
In stable release: 2.5

## References

API documentation:

* [`WidgetTester.enterText`][]
* [`TestTextInput.enterText`][]

Relevant issues:

* [Issue 79494][]

Relevant PR:

* [enterText to move the caret to the end][]


[`WidgetTester.enterText`]: https://api.flutter-io.cn/flutter/flutter_test/WidgetTester/enterText.html
[`TestTextInput.enterText`]: https://api.flutter-io.cn/flutter/flutter_test/TestTextInput/enterText.html

[Issue 79494]: https://github.com/flutter/flutter/issues/79494
[enterText to move the caret to the end]: https://github.com/flutter/flutter/pull/79506

