TestComplete Forms - Using Custom Events

Categories: How toRoutinesSource codeTestComplete

Parent page: Using GUI Forms in TestComplete

Even though automation scripts are intended to drive the application under test there are cases when communication with tester is required. With TestComplete, we have a vast arsenal of predefined GUI controls for that (and those, acquainted with Delphi, will be pleasantly surprised by the way it’s organized).

Today’s post provides an example of creating and using dialog with custom event handlers - whenever script user provides input, event handler may be used to provide a customized response.
In the scope of the exercise let’s use the following task.

  • Setup form prompts to choose an environment where test plan is to be run
  • User can choose one of predefined environments: DEV, SIT, UAT
  • Receives a response as a url of the environment
  • User can leave suggested by default test data file or can type filepath directly

Preparing Modal Form

We begin with a sample form.

Add Edit Controls, Buttons, and Labels

Set Object Properties

Form
  • Name – object name to use in the code; ‘Security’ in my example
  • Caption – title of the window when the form is displayed
Label controls
  • Caption – displayed text
Edit Box Control
  • Name – object name to use in the code; ‘ShowUrl’ in my example
  • Text - initial value
  • ReadOnly = True - because user can’t input Url directly
Combo Box Control
  • Name – object name to use in the code; ‘Environment’/’TestData’ in my example
  • Properties.Items - items to select; ‘DEV’, ‘SIT’, ‘UAT’ in my example
  • ItemIndex = 0 - initial selection
  • Properties.DropDownListStyle = lsFixedList - for Environment combobox
  • Properties.DropDownListStyle = lsEditList - for TestData combobox
Buttons
  • Caption – displayed text; ‘OK’/’Cancel’ in my example
  • Cancel = True – for ‘Cancel’ button
  • ModalResult = mrCancel - for ‘Cancel’ button
  • Default = True – for ‘OK’ button
  • ModalResult = mrOK - for ‘OK’ button

Set Event Handler

Properties

Path

Code
Public Sub Setup_Environment_OnChange(Sender)
 Dim sEnvironment
 sEnvironment = UserForms.Setup.Environment.Text
 Select Case sEnvironment
 Case "DEV"
 UserForms.Setup.ShowUrl.Text = "http://dev/"
 UserForms.Setup.TestData.Text = "W:\Test Suite\Data\DEV.xlsx"
 Case "SIT"
 UserForms.Setup.ShowUrl.Text = "http://sit/"
 UserForms.Setup.TestData.Text = "W:\Test Suite\Data\SIT.xlsx"
 Case "UAT"
 UserForms.Setup.ShowUrl.Text = "http://uat/"
 UserForms.Setup.TestData.Text = "W:\Test Suite\Data\UAT.xlsx"
 End Select
End Sub

Example

Public Function Form1()
 Dim objDlg
 Dim sEnvironment, sTestData
 Dim Result
 'Get object's instance
 Set objDlg = UserForms.Setup
 'Execute
 Result = objDlg.ShowModal
 If Result = mrOK Then
 'retrieve the selected input
 sEnvironment = objDlg.Environment.Text
 sTestData = objDlg.TestData.Text
 Else
 'Respond
 MsgBox "Execution cancelled", vbOKOnly
 'exit function or finalize execution
 End If
End Function

Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
This work by the author is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.