Selecting Text Within an Edit Control

Applies to TestComplete 12.60, last modified on September 17, 2018

When testing edit controls in iOS applications, you may often need to select text within them. This topic describes how you can select text in edit controls in tests.

Selecting Text

To select a word or the entire text in an edit control in an iOS application, simulate a touch on the Select or Select All item of the context menu. To call a control’s context menu, use the LongTouch method.

The following example demonstrates how to select the entire text displayed in an edit control:

JavaScript, JScript

function Test()
{
  // Select the iOS device
  Mobile.SetCurrent("iPhone");

  // Obtain the iOS TextField object
  var p = Mobile.Device().Process("SampleApp");
  var edit = p.Window(0).TextField();

  // Invoke the context menu
  edit.LongTouch();

  // Select the text by touching the Select All button
  var selectButton = p.Window(2).WaitChild("Button(\"Select All\")");
  if (selectButton.Exists)
    selectButton.TouchButton();
}

Python

def Test():
  # Select the iOS device
  Mobile.SetCurrent("iPhone")

  # Obtain the iOS TextField object
  p = Mobile.Device().Process("SampleApp")
  edit = p.Window(0).TextField()

  # Invoke the context menu
  edit.LongTouch()

  # Select the text by touching the Select All button
  selectButton = p.Window(2).WaitChild("Button(\"Select All\")")
  if selectButton.Exists:
    selectButton.TouchButton()

VBScript

Sub Test
  ' Select the iOS device
  Mobile.SetCurrent "iPhone"

  ' Obtain the iOS TextField object
  Set p = Mobile.Device.Process("SampleApp")
  Set edit = p.Window(0).TextField

  ' Invoke the context menu
  edit.LongTouch

  ' Select the text by touching the Select All button
  Set selectButton = p.Window(2).WaitChild("Button(""Select All"")")
  If selectButton.Exists Then
    selectButton.TouchButton
  End If
End Sub

DelphiScript

procedure Test();
var p, edit, selectButton;
begin
  // Select the iOS device
  Mobile.SetCurrent('iPhone');

  // Obtain the iOS TextField object
  p := Mobile.Device.Process('SampleApp');
  edit := p.Window(0).TableView().TextField();

  // Invoke the context menu
  edit.LongTouch();

  // Select the text by touching the Select All button
  selectButton := p.Window(2).WaitChild('Button(''Select All'')');
  if selectButton.Exists then
    selectButton.TouchButton();
end;

C++Script, C#Script

function Test()
{
  // Select the iOS device
  Mobile["SetCurrent"]("iPhone");

  // Obtain the iOS TextField object
  var p = Mobile["Device"]["Process"]("SampleApp");
  var edit = p["Window"](0)["TextField"]();

  // Invoke the context menu
  edit["LongTouch"]();

  // Select the text by touching the Select All button
  var selectButton = p["Window"](2)["WaitChild"]("Button(\"Select All\")");
  if (selectButton["Exists"])
    selectButton["TouchButton"]();
}

Getting the Selected Text

To obtain the text selected in an edit control, use the wSelection property of the appropriate test object. The following example demonstrates how to post the text selected in an edit control to the test log:

JavaScript, JScript

function Test()
{
  // Select the iOS device
  Mobile.SetCurrent("iPhone");

  // Obtain the iOS TextField object
  var p = Mobile.Device().Process("SampleApp");
  var edit = p.Window(0).TextField();

  // Invoke the context menu
  edit.LongTouch(10, 10);

  // Select the text
  var selectButton = p.Window(2).WaitChild("Button(\"Select\")", 3000);
  if (selectButton.Exists)
  {
    selectButton.TouchButton();
    var selectedText = edit.wSelection;
    Log.Message(selectedText);
  }

}

Python

def Test():
  # Select the iOS device
  Mobile.SetCurrent("iPhone")

  # Obtain the iOS TextField object
  p = Mobile.Device().Process("SampleApp")
  edit = p.Window(0).TextField()

  # Invoke the context menu
  edit.LongTouch(10, 10)

  # Select the text
  selectButton = p.Window(2).WaitChild("Button(\"Select\")", 3000)
  if selectButton.Exists:
    selectButton.TouchButton()
    selectedText = edit.wSelection
    Log.Message(selectedText)

VBScript

Sub Test
  ' Select the iOS device
  Mobile.SetCurrent "iPhone"

  ' Obtain the iOS TextField object
  Set p = Mobile.Device.Process("SampleApp")
  Set edit = p.Window(0).TextField

  ' Invoke the context menu
  Call edit.LongTouch(10, 10)

  ' Select the text
  Set selectButton = p.Window(2).WaitChild("Button(""Select"")", 3000)
  If selectButton.Exists Then
    selectButton.TouchButton
    selectedText = edit.wSelection
    Log.Message selectedText
  End If

End Sub

DelphiScript

procedure Test();
var p, edit, selectButton, selectedText;
begin
  // Select the iOS device
  Mobile.SetCurrent('iPhone');

  // Obtain the iOS TextField object
  p := Mobile.Device.Process('SampleApp');
  edit := p.Window(0).TableView().TextField();

  // Invoke the context menu
  edit.LongTouch(10, 10);

  // Select the text
  selectButton := p.Window(2).WaitChild('Button(''Select'')', 3000);
  if selectButton.Exists then
  begin
    selectButton.TouchButton();
    selectedText := edit.wSelection;
    Log.Message(selectedText);
  end;

end;

C++Script, C#Script

function Test()
{
  // Select the iOS device
  Mobile["SetCurrent"]("iPhone");

  // Obtain the iOS TextField object
  var p = Mobile["Device"]["Process"]("SampleApp");
  var edit = p["Window"](0)["TextField"]();

  // Invoke the context menu
  edit["LongTouch"](10, 10);

  // Select the text
  var selectButton = p["Window"](2)["WaitChild"]("[\"Button\"](\"Select\")", 3000);
  if (selectButton["Exists"])
  {
    selectButton["TouchButton"]();
    var selectedText = edit["wSelection"];
    Log["Message"](selectedText);
  }

}

Simulating Actions in Keyword Tests

To selecting the text from keyword tests, call the methods described above by using the On-Screen Action or Call Object Method operation. See Calling Object Methods.

See Also

Working With iOS Text Edit Controls
Getting an Edit Control's Text
iOS TextField Support
LongTouch Action (Mobile Objects)
wSelection Property (Mobile Controls)
Testing iOS Applications

Highlight search results