Copying and Pasting Text in Edit Controls

Applies to TestComplete 15.0, last modified on November 17, 2021

When testing text edit controls in iOS applications, you often may need to modify the control’s content by pasting text from clipboard. You may also need to copy or cut the selected text from the control.

For information on how to select text, see Selecting Text Within an Edit Control.

Copying Text

You can copy (or cut) the text displayed in the edit control by simulating touches over the appropriate buttons of the edit control’s context menu. Below is the example that demonstrates how to copy text from the edit text control by touching the Copy button on the edit control’s context menu

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
  var selectButton = p.Window(2).WaitChild("Button(\"Select All\")", 3000);
  if (selectButton.Exists)
    selectButton.TouchButton();

  // Copy the text
  var copyButton = p.Window(2).WaitChild("Button(\"Copy\")", 3000);
  if (copyButton.Exists)
    copyButton.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
  selectButton = p.Window(2).WaitChild("Button(\"Select All\")", 3000)
  if selectButton.Exists:
    selectButton.TouchButton()

  # Copy the text
  copyButton = p.Window(2).WaitChild("Button(\"Copy\")", 3000)
  if copyButton.Exists:
    copyButton.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
  Set selectButton = p.Window(2).WaitChild("Button(""Select All"")", 3000)
  If selectButton.Exists Then
    selectButton.TouchButton
  End If

  ' Copy the text
  Set copyButton = p.Window(2).WaitChild("Button(""Copy"")", 3000)
  If copyButton.Exists Then
    copyButton.TouchButton
  End If

End Sub

DelphiScript

procedure Test();
var p, edit, selectButton, copyButton;
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
  selectButton := p.Window(2).WaitChild('Button(''Select All'')', 3000);
  if selectButton.Exists then
    selectButton.TouchButton();

  // Copy the text
  copyButton := p.Window(2).WaitChild('Button(''Copy'')', 3000);
  if copyButton.Exists then
    copyButton.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
  var selectButton = p["Window"](2)["WaitChild"]("[\"Button\"](\"Select All\")", 3000);
  if (selectButton["Exists"])
    selectButton["TouchButton"]();

  // Copy the text
  var copyButton = p["Window"](2)["WaitChild"]("[\"Button\"](\"Copy\")", 3000);
  if (copyButton["Exists"])
    copyButton["TouchButton"]();

}

Pasting Text

You can paste the text from the clipboard to the text edit control by simulating actions over the control’s context menu. Before pasting the text, you should specify the insertion point to add the clipboard content or select any portion of the test to replace it with the clipboard content.

The example below demonstrates how to paste the text from the clipboard to the edit control:

JavaScript, JScript

function Test()
{

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

  // Obtain the iOS TextField object and fill it with text
  var p = Mobile.Device().Process("SampleApp");
  var edit1 = p.Window(0).TextField(0);
  edit1.SetText("Do bats eat cats?");

  // Select and copy the text
  edit1.LongTouch();
  var selectButton = p.Window(2).WaitChild("Button(\"Select All\")", 3000);
  if (selectButton.Exists)
    {
    selectButton.TouchButton();
    var copyButton = p.Window(2).WaitChild("Button(\"Copy\")", 3000);
    if (copyButton.Exists)
      copyButton.TouchButton();
    }

  // Obtain another iOS TextField object and paste the text in it
  var edit2 = p.Window(0).TextField(1);
  edit2.LongTouch();
  var pasteButton = p.Window(2).WaitChild("Button(\"Paste\")", 3000);
  if (pasteButton.Exists)
    pasteButton.TouchButton();

}

Python

def Test():

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

  # Obtain the iOS TextField object and fill it with text
  p = Mobile.Device().Process("SampleApp")
  edit1 = p.Window(0).TextField(0)
  edit1.SetText("Do bats eat cats?")

  # Select and copy the text
  edit1.LongTouch()
  selectButton = p.Window(2).WaitChild("Button(\"Select All\")", 3000)
  if (selectButton.Exists):
    selectButton.TouchButton()
    copyButton = p.Window(2).WaitChild("Button(\"Copy\")", 3000)
    if (copyButton.Exists):
      copyButton.TouchButton()

  # Obtain another iOS TextField object and paste the text in it
  edit2 = p.Window(0).TextField(1)
  edit2.LongTouch()
  pasteButton = p.Window(2).WaitChild("Button(\"Paste\")", 3000)
  if pasteButton.Exists:
    pasteButton.TouchButton()

VBScript

Sub Test

  ' Select the iOS device
  Mobile.SetCurrent "iPhone"

  ' Obtain the iOS TextField object and fill it with text
  Set p = Mobile.Device.Process("SampleApp")
  Set edit1 = p.Window(0).TextField(0)
  edit1.SetText "Do bats eat cats?"

  ' Select and copy the text
  edit1.LongTouch
  Set selectButton = p.Window(2).WaitChild("Button(""Select All"")", 3000)
  If selectButton.Exists Then
    selectButton.TouchButton
    Set copyButton = p.Window(2).WaitChild("Button(""Copy"")", 3000)
    If copyButton.Exists Then
      copyButton.TouchButton
    End If
  End If

  ' Obtain another iOS TextField object and paste the text in it
  Set edit2 = p.Window(0).TextField(1)
  edit2.LongTouch
  Set pasteButton = p.Window(2).WaitChild("Button(""Paste"")", 3000)
  If pasteButton.Exists Then
    pasteButton.TouchButton
  End If

End Sub

DelphiScript

procedure Test();
var p, edit1, edit2, selectButton, copyButton, pasteButton;
begin

  // Select the iOS device
  Mobile.SetCurrent('iPhone');

  // Obtain the iOS TextField object and fill it with text
  p := Mobile.Device.Process('SampleApp');
  edit1 := p.Window(0).TextField(0);
  edit1.SetText('Do bats eat cats?');

  // Select and copy the text
  edit1.LongTouch();
  selectButton := p.Window(2).WaitChild('Button(''Select All'')', 3000);
  if selectButton.Exists then
    begin
    selectButton.TouchButton();
    copyButton := p.Window(2).WaitChild('Button(''Copy'')', 3000);
    if copyButton.Exists then
      copyButton.TouchButton();
    end;

  // Obtain another iOS TextField object and paste the text in it
  edit2 := p.Window(0).TextField(1);
  edit2.LongTouch();
  pasteButton := p.Window(2).WaitChild('Button(''Paste'')', 3000);
  if pasteButton.Exists then
    pasteButton.TouchButton();

end;

C++Script, C#Script

function Test()
{

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

  // Obtain the iOS TextField object and fill it with text
  var p = Mobile["Device"]["Process"]("SampleApp");
  var edit1 = p["Window"](0)["TextField"](0);
  edit1["SetText"]("Do bats eat cats?");

  // Select and copy the text
  edit1["LongTouch"]();
  var selectButton = p["Window"](2)["WaitChild"]("[\"Button\"](\"Select All\")", 3000);
  if (selectButton["Exists"])
    {
    selectButton["TouchButton"]();
    var copyButton = p["Window"](2)["WaitChild"]("[\"Button\"](\"Copy\")", 3000);
    if (copyButton["Exists"])
      copyButton["TouchButton"]();
    }

  // Obtain another iOS TextField object and paste the text in it
  var edit2 = p["Window"](0)["TextField"](1);
  edit2.LongTouch();
  var pasteButton = p["Window"](2)["WaitChild"]("[\"Button\"](\"Paste\")", 3000);
  if (pasteButton["Exists"])
    pasteButton["TouchButton"]();

}

Simulating Actions From Keyword Tests

To interact with edit control's context menu to copy, cut and paste the text in edit controls 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
Selecting Text Within an Edit Control
iOS TextField Support
Testing iOS Applications (Legacy)

Highlight search results