Selecting Text Within the Edit Text Control

Applies to TestComplete 12.60, last modified on September 17, 2018
Selecting Part of Text
Simulating LongTouch Action

To select a word in an EditText control, use the LongTouch action of the Android EditText object that TestComplete associates with that control. This action performs a single long touch over the defined coordinates. The following code example selects the word who within the text of the control:

JavaScript, JScript

function Test()
{
  var p, Edit;

  // Select the Android device
  Mobile.SetCurrent("MyDevice");

  // Obtain the EditText object and fill it with text
  p = Mobile.Device().Process("com.example.myapp");
  Edit = p.RootLayout("").Layout("layoutTop").Layout("layout1").EditText("edit1");
  Edit.SetText("But who is to give the prizes?");
  
  // Long touch at the "who" word position
  Edit.LongTouch(50, 15);
}

Python

def Test():

  # Select the Android device
  Mobile.SetCurrent("MyDevice")

  # Obtain the EditText object and fill it with text 
  p = Mobile.Device().Process("com.example.myapp")
  Edit = p.RootLayout("").Layout("layoutTop").Layout("layout1").EditText("edit1")
  Edit.SetText("But who is to give the prizes?")
  
  # Long touch at the "who" word position 
  Edit.LongTouch(50, 15)

VBScript

Sub Test()
  Dim p, Edit

  ' Select the Android device
  Call Mobile.SetCurrent("MyDevice")

  ' Obtain the EditText object and fill it with text
  Set p = Mobile.Device.Process("com.example.myapp")
  Set Edit = p.RootLayout("").Layout("layoutTop").Layout("layout1").EditText("edit1")
  Call Edit.SetText("But who is to give the prizes?")
  
  ' Long touch at the "who" word position
  Call Edit.LongTouch(50, 15)
End Sub

DelphiScript

procedure Test();
var
  p, Edit : OleVariant;
begin
  // Select the Android device
  Mobile.SetCurrent('MyDevice');

  // Obtain the EditText object and fill it with text
  p := Mobile.Device.Process('com.example.myapp');
  Edit := p.RootLayout('').Layout('layoutTop').Layout('layout1').EditText('edit1');
  Edit.SetText('But who is to give the prizes?');
  
  // Long touch at the "who" word position
  Edit.LongTouch(50, 15);
end;

C++Script, C#Script

function Test()
{
  var p, Edit;

  // Select the Android device
  Mobile["SetCurrent"]("MyDevice");

  // Obtain the EditText object and fill it with text
  p = Mobile["Device"]["Process"]("com.example.myapp");
  Edit = p["RootLayout"]("")["Layout"]("layoutTop")["Layout"]("layout1")["EditText"]("edit1");
  Edit["SetText"]("But who is to give the prizes?");
  
  // Long touch at the "who" word position
  Edit["LongTouch"](50, 15);
}

The required touch may differ depending on the device performing the test.
Changing Selection Range

To change the selection range, use the Swipe action of the Device object. The Swipe starting coordinates should overlap with the position of one of the pointers. The pointers specify the beginning and the end of the selection range. You cannot place them after the end or before the beginning of the selection respectively. If you try to do this, one symbol will remain selected. The following example selects "who is" in the text.

JavaScript, JScript

function Test()
{
  var p, Edit;

  // Select the Android device
  Mobile.SetCurrent("MyDevice");

  // Obtain the EditText object and fill it with text
  p = Mobile.Device().Process("com.example.myapp");
  Edit = p.RootLayout("").Layout("layoutTop").Layout("layout1").EditText("edit1");
  Edit.SetText("But who is to give the prizes?");
  
  // Long touch at the "who" word position
  Edit.LongTouch(50, 15);
  
  // Swipe the device to change selection range
  Mobile.Device().Swipe(165, 129, 180, 125, 10, 32);
}

Python

def Test():

  # Select the Android device
  Mobile.SetCurrent("MyDevice")

  # Obtain the EditText object and fill it with text 
  p = Mobile.Device().Process("com.example.myapp")
  Edit = p.RootLayout("").Layout("layoutTop").Layout("layout1").EditText("edit1")
  Edit.SetText("But who is to give the prizes?")
  
  # Long touch at the "who" word position 
  Edit.LongTouch(50, 15)
  
  # Swipe the device to change selection range
  Mobile.Device().Swipe(165, 129, 180, 125, 10, 32)

VBScript

Sub Test()
  Dim p, Edit

  ' Select the Android device
  Call Mobile.SetCurrent("MyDevice")

  ' Obtain the EditText object and fill it with text
  Set p = Mobile.Device.Process("com.example.myapp")
  Set Edit = p.RootLayout("").Layout("layoutTop").Layout("layout1").EditText("edit1")
  Call Edit.SetText("But who is to give the prizes?")
  
  ' Long touch at "who" word position
  Call Edit.LongTouch(50, 15)
  
  ' Swipe the device to change selection range
  Call Mobile.Device.Swipe(165, 129, 180, 125, 10, 32)
End Sub

DelphiScript

procedure Test();
var
  p, Edit : OleVariant;
begin
  // Select the Android device
  Mobile.SetCurrent('MyDevice');

  // Obtain the EditText object and fill it with text
  p := Mobile.Device.Process('com.example.myapp');
  Edit := p.RootLayout('').Layout('layoutTop').Layout('layout1').EditText('edit1');
  Edit.SetText('But who is to give the prizes?');
  
  // Long touch at "who" word position
  Edit.LongTouch(50, 15);
  
  // Swipe the device to change selection range
  Mobile.Device.Swipe(165, 129, 180, 125, 10, 32);
end;

C++Script, C#Script

function Test()
{
  var p, Edit;

  // Select the Android device
  Mobile["SetCurrent"]("MyDevice");

  // Obtain the EditText object and fill it with text
  p = Mobile["Device"]["Process"]("com.example.myapp");
  Edit = p["RootLayout"]("")["Layout"]("layoutTop")["Layout"]("layout1")["EditText"]("edit1");
  Edit["SetText"]("But who is to give the prizes?");
  
  // Long touch at "who" word position
  Edit["LongTouch"](50, 15);
  
  // Swipe the device to change selection range
  Mobile["Device"]["Swipe"](165, 129, 180, 125, 10, 32);
}

The screen coordinates will differ depending on the device the test is performed on.
Selecting All Text
Simulating SelectAll Action

To select all text in an edit text control, use the SelectAll action. The following example demonstrates how to select all of the text contained in the EditText control using this action:

JavaScript, JScript

function Test()
{
  var p, Edit;

  // Select the Android device
  Mobile.SetCurrent("MyDevice");

  // Obtain the EditText object and fill it with text
  p = Mobile.Device().Process("com.example.myapp");
  Edit = p.RootLayout("").Layout("layoutTop").Layout("layout1").EditText("edit1");
  Edit.SetText("But who is to give the prizes?");
  
  // Select all text
  Edit.SelectAll();
}

Python

def Test():

  # Select the Android device
  Mobile.SetCurrent("MyDevice")

  # Obtain the EditText object and fill it with text 
  p = Mobile.Device().Process("com.example.myapp")
  Edit = p.RootLayout("").Layout("layoutTop").Layout("layout1").EditText("edit1")
  Edit.SetText("But who is to give the prizes?")
  
  # Select all text
  Edit.SelectAll()

VBScript

Sub Test()
  Dim p, Edit

  ' Select the Android device
  Call Mobile.SetCurrent("MyDevice")

  ' Obtain the EditText object and fill it with text
  Set p = Mobile.Device.Process("com.example.myapp")
  Set Edit = p.RootLayout("").Layout("layoutTop").Layout("layout1").EditText("edit1")
  Call Edit.SetText("But who is to give the prizes?")
  
  ' Select all text
  Call Edit.SelectAll
End Sub

DelphiScript

procedure Test();
var
  p, Edit : OleVariant;
begin
  // Select the Android device
  Mobile.SetCurrent('MyDevice');

  // Obtain the EditText object and fill it with text
  p := Mobile.Device.Process('com.example.myapp');
  Edit := p.RootLayout('').Layout('layoutTop').Layout('layout1').EditText('edit1');
  Edit.SetText('But who is to give the prizes?');
  
  // Select all text
  Edit.SelectAll();
end;

C++Script, C#Script

function Test()
{
  var p, Edit;

  // Select the Android device
  Mobile["SetCurrent"]("MyDevice");

  // Obtain the EditText object and fill it with text
  p = Mobile["Device"]["Process"]("com.example.myapp");
  Edit = p["RootLayout"]("")["Layout"]("layoutTop")["Layout"]("layout1")["EditText"]("edit1");
  Edit["SetText"]("'But who is to give the prizes?");
  
  // Select all text
  Edit["SelectAll"]();
}

Note: This action only selects text, it does not bring up the TextSelection menu or selection pointers.
Touching Android Buttons

You can use the buttons from the Text selection context menu. This menu can be called by selecting the text using LongTouch or the action, appropriate for your device. Due to technical implementation, TestComplete cannot access these controls directly, so to simulate the touch use the images from the Image Repository. The example below demonstrates how to select all text using the context menu.

JavaScript, JScript

function Test()
{
  var p, Edit;

  // Select the Android device
  Mobile.SetCurrent("MyDevice");

  // Obtain the EditText object and fill it with text
  p = Mobile.Device().Process("com.example.myapp");
  Edit = p.RootLayout("").Layout("layoutTop").Layout("layout1").EditText("edit1");
  Edit.SetText("But who is to give the prizes?");
  
  // Long touch at a word position
  Edit.LongTouch(50, 15);
  
  // Touch the SelectAll button by image
  ImageRepository.myandroidapp2.Button_SelectAll.Touch();
}

Python

def Test():

  # Select the Android device
  Mobile.SetCurrent("MyDevice")

  # Obtain the EditText object and fill it with text 
  p = Mobile.Device().Process("com.example.myapp")
  Edit = p.RootLayout("").Layout("layoutTop").Layout("layout1").EditText("edit1")
  Edit.SetText("But who is to give the prizes?")
  
  # Long touch at a word position 
  Edit.LongTouch(50, 15)
  
  # Touch the SelectAll button by image
  ImageRepository.myandroidapp2.Button_SelectAll.Touch()

VBScript

Sub Test()
  Dim p, Edit

  ' Select the Android device
  Call Mobile.SetCurrent("MyDevice")

  ' Obtain the EditText object and fill it with text
  Set p = Mobile.Device.Process("com.example.myapp")
  Set Edit = p.RootLayout("").Layout("layoutTop").Layout("layout1").EditText("edit1")
  Call Edit.SetText("But who is to give the prizes?")
  
  ' Long touch at a word position
  Call Edit.LongTouch(50, 15)
  
  ' Touch the SelectAll button by image
  ImageRepository.myandroidapp2.TextView_SelectAll.Touch()
End Sub

DelphiScript

procedure Test();
var
  p, Edit : OleVariant;
begin
  // Select the Android device
  Mobile.SetCurrent('MyDevice');

  // Obtain the EditText object and fill it with text
  p := Mobile.Device.Process('com.example.myapp');
  Edit := p.RootLayout('').Layout('layoutTop').Layout('layout1').EditText('edit1');
  Edit.SetText('But who is to give the prizes?');
  
  // Long touch at a word position
  Edit.LongTouch(50, 15);
  
  // Touch the SelectAll button by image
  ImageRepository.myandroidapp2.Button_SelectAll.Touch();
end;

C++Script, C#Script

function Test()
{
  var p, Edit;

  // Select the Android device
  Mobile["SetCurrent"]("MyDevice");

  // Obtain the EditText object and fill it with text
  p = Mobile["Device"]["Process"]("com.example.myapp");
  Edit = p["RootLayout"]("")["Layout"]("layoutTop")["Layout"]("layout1")["EditText"]("edit1");
  Edit["SetText"]("'But who is to give the prizes?");
  
  // Long touch at a word position
  Edit["LongTouch"](50, 15);
  
  // Touch the SelectAll button by image
  ImageRepository["myandroidapp"]["Button_SelectAll"]["Touch"]();
}

The required touch may differ depending on the device performing test.
Simulating Actions From Keyword Tests

This topic explains how to select the text of the edit text control in scripts. You can use the described methods in keyword tests too. To do this, use the On-Screen Action or the Call Object Method operations.

See Also

Working With Android Edit Text Controls
Simulating Text Input on Android Devices
Copying and Pasting Text in an Edit Text Control

Highlight search results