Testing Modal Windows

Applies to TestComplete 15.20, last modified on January 19, 2022

Modal windows are windows that should be closed or hidden so you can work with the rest of your application. TestComplete supports testing of modal windows.

If a modal window is an unexpected window (that is, if it did not appear during script recording and thus your script does not expect this window to appear during the run), the window will be handled in the normal manner that TestComplete uses for unexpected windows.

Note: TestComplete does not handle modal windows implemented as windowless objects (for example, modal windows in Flex applications or web applications) as unexpected. See Handling Unexpected Windows for more information.

If you expect the modal window to appear, you can test it in the same way as you would test a modeless window. The same way you address a window’s buttons, edit boxes and other controls, simulate keystrokes, mouse clicks and other user actions. The same way you work with window elements - all of these are not dependent on the window type (modal or modeless). So, everything that is available for a modeless window, is also available for modal windows.

Note, however, that the way you call a modal window may affect the way you work with it. A window may be called by TestComplete methods and actions or by functions located in the tested Open Application. TestComplete methods and actions return immediately after they send the appropriate simulation command to the application under test. Application functions do not typically return values until the modal window they call is hidden. So, if you call an application’s function that displays a modal window, the script execution is paused until this method returns.

To avoid this problem, use the Runner.CallObjectMethodAsync method to call application functions that display modal windows. This method calls an application function, but does not wait until the function returns (that is, the method does not pause the script execution in TestComplete), so the next code statement can simulate user actions over the displayed modal window.

The Runner.CallObjectMethodAsync method returns the CallObjectMethodAsyncResult object, which you can use to wait until the application’s function is over and to analyze the function’s results. The following code demonstrates how you can use the Runner.CallObjectMethodAsync method and the Runner.CallObjectMethodAsync:

Note: In the code snippet below, the tested application’s name is MyTestApplication. This application has a form named Form1. The form class contains the MyShowModalDialog method that displays a modal window. This method uses two string parameters and returns an integer value as a result.

JavaScript, JScript

function Test()
{
  var p, w, ResObject;

  // Obtains the desired object
  p = Sys.Process("MyTestApplication");
  w = p.WinFormsObject("Form1");
    
  // Calls the method that displays a modal window
  ResObject = Runner.CallObjectMethodAsync(w, "MyShowModalDialog", "Param1", "Param2");

  // Simulates user actions over the modal window and closes it
  ...
  w = p.WinFormsObject("ModalDialogForm");
  if (w.Exists)
    w.WinFormsObject("OKButton").ClickButton(); // Close

  ...

  // Waits for method completion
  ResObject.WaitForCompletion(2000);

  // Processes the result
  if( ResObject.ReturnValue == 1)
    Log.Message("Success");
  else
    Log.Message("Failure");
}

Python

def Test():

  # Obtains the desired object
  p = Sys.Process("MyTestApplication")
  w = p.WinFormsObject("Form1")
    
  # Calls the method that displays a modal window
  ResObject = Runner.CallObjectMethodAsync(w, "MyShowModalDialog", "Param1", "Param2")

  # Simulates user actions over the modal window and closes it
  ...
  w = p.WinFormsObject("ModalDialogForm")
  if (w.Exists): 
    w.WinFormsObject("OKButton").ClickButton() # Close

  ...

  # Waits for method completion
  ResObject.WaitForCompletion(2000) 

  # Processes the result
  if( ResObject.ReturnValue == 1):
    Log.Message("Success")
  else:
    Log.Message("Failure")

VBScript

Sub Test
  ' Obtains the desired object
  Set p = Sys.Process("MyTestApplication")
  Set w = p.WinFormsObject("Form1")
    
  ' Calls the method that displays a modal window
  Set ResObject = Runner.CallObjectMethodAsync(w, "MyShowModalDialog", "Param1", "Param2")

  ' Simulates user actions over the modal window and closes it
  ...
  Set w = p.WinFormsObject("ModalDialogForm")
  If w.Exists then
    w.WinFormsObject("OKButton").ClickButton ' Close
  End If

  ...

  ' Waits for method completion
  ResObject.WaitForCompletion 2000

  ' Processes the result
  If ResObject.ReturnValue = 1 Then
    Log.Message "Success"
  Else
    Log.Message "Failure"
  End If
End Sub

DelphiScript

procedure Test;
var
  p, w, ResObject : OleVariant;
begin
  // Obtains the desired object
  p := Sys.Process('MyTestApplication');
  w := p.WinFormsObject('Form1');
    
  // Calls the method that displays a modal window
  ResObject := Runner.CallObjectMethodAsync(w, 'MyShowModalDialog', 'Param1', 'Param2');

  // Simulates user actions over the modal window and closes it
  ...
  w := p.WinFormsObject('ModalDialogForm');
  if w.Exists then
    w.WinFormsObject('OKButton').ClickButton(); // Close

  ...

  // Waits for method completion
  ResObject.WaitForCompletion(2000);

  // Processes the result
  if ResObject.ReturnValue = 1 then
    Log.Message('Success')
  else
    Log.Message('Failure');
end;

C++Script, C#Script

function Test()
{
  var p, w, ResObject;

  // Obtains the desired object
  p = Sys["Process"]("MyTestApplication");
  w = p["WinFormsObject"]("Form1");
    
  // Calls the method that displays a modal window
  ResObject = Runner["CallObjectMethodAsync"](w, "MyShowModalDialog", "Param1", "Param2");

  // Simulates user actions over the modal window and closes it
  ...
  w = p["WinFormsObject"]("ModalDialogForm");
  if (w["Exists"])
    w["WinFormsObject"]("OKButton")["ClickButton"](); // Close

  ...

  // Waits for method completion
  ResObject["WaitForCompletion"](2000);

  // Processes the result
  if( ResObject["ReturnValue"] == 1 )
    Log["Message"]("Success");
  else
    Log["Message"]("Failure");
}

See Also

Calling Methods Asynchronously
CallObjectMethodAsync Method
SetObjectPropertyAsync Method
CallObjectMethodAsyncResult Object
Handling Unexpected Windows
Simulating User Actions
About Open Applications

Highlight search results