Getting an Edit Control's Text

Applies to TestComplete 14.50, last modified on April 22, 2021

While testing edit controls, you can use specific properties and methods of the corresponding program object to perform certain actions and obtain data stored in controls. You can call these methods and properties from your keyword tests, as well as from scripts. This topic describes how to work with the needed properties and methods from your scripts. However, when testing a control from your keyword test, you can use the same methods and properties calling them from keyword test operations. For more information, see Keyword Tests Basic Operations.

If you need to get a portion of the text that an edit control contains, you can, for example, select the needed text, then copy it to the clipboard (see the Copying and Pasting Text in an Edit Control topic). You can retrieve the whole text the same way you would a text portion (for more information see the Getting the Selected Text in an Edit Control topic). Actually, it is more convenient to get the whole text by using the wText property.

wText is a specific property that belongs to the Win32Edit object. This object is automatically associated with standard Windows edit controls during the test run. The wText property allows you to get an edit control’s text and use it to return the edit control’s text entirely. In this case, it does not matter if the text is selected or not. The following example demonstrates how you can obtain the edit control’s text:

JavaScript, JScript

function Main()
{
  var p, Edit;
  // Run Notepad
  WshShell.Run("notepad.exe", SW_SHOWNORMAL);

  // Obtain the edit object
  p = Sys.Process("NOTEPAD");
  Edit = p.Window("Notepad").Window("Edit");
  Edit.Keys("It's the oldest rule in the book");

  // Copy the edit control's text to the clipboard
  Sys.Clipboard = Edit.wText;

  // Post the clipboard content to the log
  Log.Message(Sys.Clipboard);
}

Python

def Main():
  # Run Notepad 
  WshShell.Run("notepad.exe", SW_SHOWNORMAL)

  # Obtain the edit object
  p = Sys.Process("NOTEPAD")
  Edit = p.Window("Notepad").Window("Edit")
  Edit.Keys("It's the oldest rule in the book")

  # Copy the edit control's text to the clipboard
  Sys.Clipboard = Edit.wText

  # Post the clipboard content to the log
  Log.Message(Sys.Clipboard)

VBScript

Sub Main
  Dim p, Edit
  ' Run Notepad
  Call WshShell.Run("notepad.exe", SW_SHOWNORMAL)

  ' Obtain the edit object and fill it with text
  Set p = Sys.Process("NOTEPAD")
  Set Edit = p.Window("Notepad").Window("Edit")
  Edit.Keys "It's the oldest rule in the book"
    
  ' Copy the edit control's text to the clipboard
  Sys.Clipboard = Edit.wText
  
  ' Post clipboard contents to the log
  Log.Message(Sys.Clipboard)
End Sub

DelphiScript

procedure Main;
var p, Edit:OleVariant;
begin
  // Run Notepad
  WshShell.Run('notepad.exe', SW_SHOWNORMAL);
 
  // Obtain the edit object
  p := Sys.Process('NOTEPAD');
  Edit := p.Window('Notepad').Window('Edit');
  Edit.Keys('It''s the oldest rule in the book');

  // Copy the edit control's text to the clipboard
  Sys.Clipboard := Edit.wText;

  // Post the clipboard content to the log
  Log.Message(Sys.Clipboard);
end;

C++Script, C#Script

function Main()
{
  var p, Edit;
  // Run Notepad
  WshShell["Run"]("notepad.exe", SW_SHOWNORMAL);

  // Obtain the edit object
  p = Sys["Process"]("NOTEPAD");
  Edit = p["Window"]("Notepad")["Window"]("Edit");
  Edit["Keys"]("It's the oldest rule in the book");

  // Copy the edit control's text to the clipboard
  Sys["Clipboard"] = Edit["wText"];

  // Post the clipboard content to the log
  Log["Message"](Sys["Clipboard"]);
}

See Also

Working With Edit Controls
Copying and Pasting Text in an Edit Control
Getting the Selected Text in an Edit Control
Entering Text into an Edit Control
Selecting Text Within an Edit Control
wText Property (Edit Controls)

Highlight search results