Copying and Pasting Text in an Edit Text Control

Applies to TestComplete 15.47, last modified on January 20, 2023
Copying Text
Getting wSelection Property

To copy text to the clipboard, first select it, and then copy the selected part of the text using the wSelection property of the Android EdtText object. The following example shows how to copy a part of the text:

JavaScript, JScript

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

  // Obtain the EditText object and fill it with text
  var p = Mobile.Device().Process("com.example.myapp");
  var Edit = p.RootLayout("").EditText("edit1");
  Edit.SetText("Do cats eat bats?");

  // Select a portion of the text by simulating a long touch over the control
  Edit.LongTouch(50, 20);

  // Obtain the selected text and post it to the test log
  var SelectedText = Edit.wSelection;
  Log.Message(SelectedText);
}

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("").EditText("edit1")
  Edit.SetText("Do cats eat bats?")

  # Select a portion of the text by simulating a long touch over the control
  Edit.LongTouch(50, 20)

  # Obtain the selected text and post it to the test log
  SelectedText = Edit.wSelection
  Log.Message(SelectedText)

VBScript

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

  ' Obtain the EditText object and fill it with text
  Set p = Mobile.Device.Process("com.example.myapp")
  Set Edit = p.RootLayout("").EditText("edit1")
  Edit.SetText("Do cats eat bats?")

  ' Select a portion of the text by simulating a long touch over the control
  Call Edit.LongTouch(50, 20)

  ' Obtain the selected text and post it to the test log
  SelectedText = Edit.wSelection
  Log.Message(SelectedText)
End Sub

DelphiScript

procedure Test();
var
  p, Edit, SelectedText;
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('').EditText('edit1');
  Edit.SetText('Do cats eat bats?');

  // Select a portion of the text by simulating a long touch over the control
  Edit.LongTouch(50, 20);

  // Obtain the selected text and post it to the test log
  SelectedText := Edit.wSelection;
  Log.Message(SelectedText);
end;

C++Script, C#Script

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

  // Obtain the EditText object and fill it with text
  var p = Mobile["Device"]["Process"]("com.example.myapp");
  var Edit = p["RootLayout"]("")["EditText"]("edit1");
  Edit["SetText"]("Do cats eat bats?");

  // Select a portion of the text by simulating a long touch over the control
  Edit["LongTouch"](50, 20);

  // Obtain the selected text and post it to the test log
  var SelectedText = Edit["wSelection"];
  Log["Message"](SelectedText);
}
Touching Android Buttons

You can also copy the text by simulating the touch on the "copy" button. Due to technical implementation, TestComplete cannot access these controls, and the touch must be simulated using images from Image Repository. Below is the example that demonstrates how to copy text from the edit text 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("Do cats eat bats?");

  // Select text
  Edit.LongTouch(50, 20);

  // Copy text
  ImageRepository.myandroidapp.Button_Copy.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("Do cats eat bats?")

  # Select text 
  Edit.LongTouch(50, 20)

  # Copy text
  ImageRepository.myandroidapp.Button_Copy.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")
  Edit.SetText("Do cats eat bats?")

  ' Select text
  Call Edit.LongTouch(50, 20)

  ' Copy text
  Call ImageRepository.myandroidapp.Button_Copy.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('Do cats eat bats?');

  // Select text
  Edit.LongTouch(50, 20);

  // Copy text
  ImageRepository.myandroidapp.Button_Copy.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"]("Do cats eat bats?");

  // Select text
  Edit["Longtouch"](50, 20);

  // Copy text
  ImageRepository["myandroidapp"]["Button_Copy"]["Touch"]();
}

The touch required to select text may differ depending on the device performing the test (See Selecting Text Within the Edit Text Control).
Cutting Text

To cut text, simulate a touch on the Cut button. Due to technical implementation, TestComplete cannot access these controls, and the touch must be simulated using images from Image Repository. Below is an example that demonstrates how to cut text from the EditText 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("Do cats eat bats?");

  // Select text
  Edit.LongTouch(50, 20);

  // Copy text
  ImageRepository.myandroidapp.Button_Cut.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("Do cats eat bats?")

  # Select text 
  Edit.LongTouch(50, 20)

  # Copy text
  ImageRepository.myandroidapp.Button_Cut.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")
  Edit.SetText("Do cats eat bats?")

  ' Select text
  Call Edit.LongTouch(50, 20)

  ' Copy text
  Call ImageRepository.myandroidapp.Button_Cut.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('Do cats eat bats?');

  // Select text
  Edit.LongTouch(50, 20);

  // Copy text
  ImageRepository.myandroidapp.Button_Cut.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"]("Do cats eat bats?");

  // Select text
  Edit["Longtouch"](50, 20);

  // Copy text
  ImageRepository["myandroidapp"]["Button_Cut"]["Touch"]();
}

Pasting Text

Before pasting text, you need to specify the insertion point to add the clipboard content, or select any portion of text to replace it with the clipboard content (see Selecting Text Within the Edit Text Control)

Simulating a Touch on the Button Object

If TestComplete can find the Paste button object within your application, you can use the Touch action. The following example demonstrates how to paste text by touching the accessible button:

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("Do cats eat bats?");

  // Select text
  Edit.LongTouch(50, 20);

  // Copy text
  ImageRepository.myandroidapp.Button_Copy.Touch();

  // Move the pointer
  Edit.LongTouch(100, 20);

  // Paste text
  p.Layout("NO_ID", 2).TextView("NO_ID").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("Do cats eat bats?")

  # Select text 
  Edit.LongTouch(50, 20)

  # Copy text
  ImageRepository.myandroidapp.Button_Copy.Touch()

  # Move the pointer 
  Edit.LongTouch(100, 20)

  # Paste text
  p.Layout("NO_ID", 2).TextView("NO_ID").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("Do cats eat bats?")

  ' Select text
  Call Edit.LongTouch(50, 20)

  ' Copy text
  Call ImageRepository.myandroidapp.Button_Copy.Touch

  ' Move the pointer
  Call Edit.LongTouch(100, 20)

  ' Paste text
  Call p.Layout("NO_ID", 2).TextView("NO_ID").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('Do cats eat bats?');

  // Select text
  Edit.LongTouch(50, 20);

  // Copy text
  Edit.LongTouch(50, 20);
  ImageRepository.myandroidapp.Button_Copy.Touch;

  // Move the pointer
  Edit.LongTouch(100, 20);

  // Paste text
  p.Layout('NO_ID', 2).TextView('NO_ID').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"]("Do cats eat bats?");

  // Select text
  Edit["Longtouch"](50, 20);

  // Copy text
  ImageRepository["myandroidapp"]["Button_Copy"]["Touch"]();

  // Move the pointer
  Edit["Longtouch"](100, 20);

  // Paste text
  p["Layout"]("NO_ID", 2)["TextView"]("NO_ID")["Touch"]();
}

The touch required to select text may differ depending on the device performing the test (see Selecting Text Within the Edit Text Control).
Touching Paste Button by Image

If the paste button is not recognized by TestComplete, use images from ImageRepository. The following code demonstrates how you can paste text using the image from ImageRepository:

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").RootLayout("");
  Edit = p.Layout("layoutTop").Layout("layout1").EditText("edit1");
  Edit.SetText("Do cats eat bats?");

  // Select text
  Edit.LongTouch(50, 20);

  // Copy text
  ImageRepository.myandroidapp.Button_Copy.Touch();

  // Move the pointer
  Edit.LongTouch(100, 20);

  // Paste text
  ImageRepository.myandroidapp.Button_Paste.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").RootLayout("")
  Edit = p.Layout("layoutTop").Layout("layout1").EditText("edit1")
  Edit.SetText("Do cats eat bats?")

  # Select text 
  Edit.LongTouch(50, 20)

  # Copy text
  ImageRepository.myandroidapp.Button_Copy.Touch()

  # Move the pointer 
  Edit.LongTouch(100, 20)

  # Paste text
  ImageRepository.myandroidapp.Button_Paste.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").RootLayout("")
  Set Edit = p.Layout("layoutTop").Layout("layout1").EditText("edit1")
  Call Edit.SetText("Do cats eat bats?")

  ' Select text
  Call Edit.LongTouch(50, 20)

  ' Copy text
  Call ImageRepository.myandroidapp.Button_Copy.Touch

  ' Move the pointer
  Call Edit.LongTouch(100, 20)

  ' Paste text
  Call ImageRepository.myandroidapp.Button_Paste.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').RootLayout('');
  Edit := p.Layout('layoutTop').Layout('layout1').EditText('edit1');
  Edit.SetText('Do cats eat bats?');

  // Select text
  Edit.LongTouch(50, 20);

  // Copy text
  Edit.LongTouch(50, 20);
  ImageRepository.myandroidapp.Button_Copy.Touch;

  // Move the pointer
  Edit.LongTouch(100, 20);

  // Paste text
  ImageRepository.myandroidapp.Button_Paste.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")["RootLayout"]("");
  Edit = p["Layout"]("layoutTop")["Layout"]("layout1")["EditText"]("edit1");
  Edit["SetText"]("Do cats eat bats?");

  // Select text
  Edit["Longtouch"](50, 20);

  // Copy text
  ImageRepository["myandroidapp"]["Button_Copy"]["Touch"]();

  // Move the pointer
  Edit["Longtouch"](100, 20);

  // Paste text
  ImageRepository["myandroidapp"]["Button_Paste"]["Touch"]();
}

The touch required to select text may differ depending on the device performing the test (see Selecting Text Within the Edit Text Control).
Simulating Actions From Keyword Tests

This topic explains how to copy and paste text to the edit text control in scripts. You can use the described methods and properties 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 (Legacy)
Entering Text into an Edit Text Control
Selecting Text Within the Edit Text Control
wSelection Property (Mobile Controls)
wText Property (Mobile Edit Controls)
SetText Action (Mobile Controls)

Highlight search results