Information in this topic applies only to web tests that implement the default approach. This method is not supported in cross-platform web tests. |
Description
The SetText
action sets the contents of a control, that is, replaces the previous contents with the specified text.
To get a control’s text, use the wText
(for edit controls) or the wText
(for combo box controls) property.
Declaration
TestObj.SetText(Text)
TestObj | A variable, parameter or expression that specifies a reference to one of the objects listed in the Applies To section | |||
Text | [in] | Required | String | |
Result | None |
Applies To
The method is applied to the following objects:
Developer Express ButtonEdit, Developer Express HyperlinkEdit, Developer Express MemoEdit and 28 more objects, Developer Express PasswordBoxEdit, Developer Express TextEdit, Ext JS Number Field, Infragistics WebDateTimeEdit, Infragistics WebMaskEdit, Infragistics WebTextEdit, Java Swing Password Field, Java Swing TextArea, Java Swing TextField, JavaFX PasswordField, JavaFX TextField, jQuery UI Autocomplete, MFC Edit Browse, Oracle Forms Text Field, Qt LineEdit, Qt Text Edit, Silverlight AutoCompleteBox, Silverlight PasswordBox, Silverlight TextBox, Telerik RadMaskedEditBox for WinForms, Telerik RadTextBox for WinForms, Web Text Box, Win32 ComboBox, Win32 Edit, WPF TextBox, YUI 2 AutoComplete, YUI 2 Rich Text Editor, YUI 3 AutoComplete « Collapse the list
View Mode
This method is available in the Object Browser panel and in other panels and dialogs in both Basic and Advanced view modes.
Parameters
The method has the following parameter:
Text
The text to enter in the control. To enter multi-line text in a multi-line control, separate the lines by using new line characters or the appropriate constants (see the example at the end of the topic).
Result Value
None.
Remarks
-
By default, TestComplete uses the
SetText
method to record text input in password edit boxes and stores password values as project password variables:JavaScript, JScript
Aliases.MyApp.MainWindow.Password.SetText(Project.Variables.Password1);
Python
Aliases.MyApp.MainWindow.Password.SetText(Project.Variables.Password1)
VBScript
Aliases.MyApp.MainWindow.Password.SetText(Project.Variables.Password1)
DelphiScript
Aliases.MyApp.MainWindow.Password.SetText(Project.Variables.Password1);
C++Script, C#Script
Aliases["MyApp"]["MainWindow"]["Password"]["SetText"](Project.Variables.Password1);
If your passwords are recorded as text stings and are not masked in tests, make sure the Tools > Options > Engines > Recording > Record text input into simple editors as option is set to SetText (the default value).
-
The
SetText
action does not activate the application window that contains the control, though it moves the insertion point to the beginning of the control’s text. Unlike thewText
(for edit controls) and thewText
(for combo box controls) properties,SetText
fails when it is applied to a disabled or read-only control. -
The
SetText
action works differently for web and desktop objects. For test objects in desktop applications, the action raises only text change events and does not raise keystroke events. For test objects on web pages, the action fires theonchange
event (a “text change” event), and “keystroke” events likekeyup
,input
andkeydown
that may trigger keyboard-related scripts (for example, autocompletion popups). Anyway, if the expected event does not occur, try using theKeys
action. -
Web Text Box, jQuery UI Autocomplete, YUI 2 AutoComplete, YUI 3 AutoComplete: The method moves the insertion point to the end of the control’s text.
-
To enter text in a certain place within a control, use the
Keys
action to set the insertion point position and to type the desired text. You can find an example in the Setting Insertion Point in an Edit Control topic. -
To simulate certain keystrokes in a control (such as Del, Home, Ctrl+C and so on) or character-by-character input, use the
Keys
action as well. -
To control whether the
SetText
action should be used to record text input into edit controls, use the Record text input into simple editors as option.
Example
The following script demonstrates how you can enter single-line and multi-line text into Notepad. (Notepad must be running before you run this script).
JavaScript, JScript
function Test()
{
var edit = Sys.Process("notepad").Window("Notepad").Window("Edit");
edit.SetText("The quick brown fox");
aqUtils.Delay(2000);
edit.SetText("The quick brown fox\r\n" +
"jumps over\r\n" +
"the lazy dog");
}
Python
def Test():
edit = Sys.Process("notepad").Window("Notepad").Window("Edit")
edit.SetText("The quick brown fox")
aqUtils.Delay(2000)
edit.SetText("The quick brown fox\r\n" +\
"jumps over\r\n" +\
"the lazy dog")
VBScript
Sub Test
Dim edit
Set edit = Sys.Process("notepad").Window("Notepad").Window("Edit")
edit.SetText "The quick brown fox"
aqUtils.Delay 2000
edit.SetText "The quick brown fox" & vbNewLine & _
"jumps over" & vbNewLine & _
"the lazy dog"
End Sub
DelphiScript
procedure Test;
var edit;
begin
edit := Sys.Process('notepad').Window('Notepad').Window('Edit');
edit.SetText('The quick brown fox');
aqUtils.Delay(2000);
edit.SetText('The quick brown fox' + #13#10 +
'jumps over' + #13#10 +
'the lazy dog');
end;
C++Script, C#Script
function Test()
{
var edit = Sys["Process"]("notepad")["Window"]("Notepad")["Window"]("Edit");
edit["SetText"]("The quick brown fox");
aqUtils["Delay"](2000);
edit["SetText"]("The quick brown fox\r\n" +
"jumps over\r\n" +
"the lazy dog");
}
See Also
wText Property (Edit Controls)
Keys Action
Entering Text into an Edit Control