Working with text entry

Important

Visual Studio App Center is scheduled for retirement on March 31, 2025. While you can continue to use Visual Studio App Center until it is fully retired, there are several recommended alternatives that you may consider migrating to.

Learn more about support timelines and alternatives.

A common task for a test is to enter text into views. Xamarin.UITest provides two APIs to address this need:

  • IApp.EnterText
  • IApp.ClearText

Each of these APIs will be discussed in more detail below.

The IApp.EnterText method allows a test to enter text into a view. The behavior of this method is slightly different between iOS and Android:

  • iOS - Xamarin.UITest will display the soft keyboard, and then simulate presses on the keyboard.
  • Android - Xamarin.UITest doesn't use the soft keyboard. Instead, it injects itself between the soft keyboard and the application and simulates key presses.

The following snippet shows an example of entering text:

app.EnterText(c=>c.Marked("creditCardTextField"), new string('9', 15));

Disable the Hardware Keyboard

IApp.EnterText will fail if the iOS simulator is configured to use the hardware keyboard. Before running your Xamarin.UITests locally, ensure that the simulator is configured to use the soft keyboard.

Screenshot of the Keyboard menu in the iOS Simulator]

Clearing Text

The IApp.ClearText method will clear the text from the view, for example:

app.ClearText(c=>c.Marked("creditCardTextField"));