Handling Unexpected Windows

Applies to TestComplete 12.60, last modified on September 17, 2018

When recording a script, you define the expected behavior of the application under test. At playback, unexpected situations can occur, like a modal window that displays with a message about an assertion or an error from the OS or another application window may overlap the point where a mouse click should be simulated.

Note: Currently, TestComplete does not handle unexpected windows if they are implemented as windowless objects (see below).

TestComplete only checks for unexpected windows when the script performs an action (Click, DblClick, Activate, etc.). The check does not happen when the script performs certain operations with window properties or when it executes a function, for example, WaitWindow.

The window that impedes the running of the script can be modal or non-modal. A non-modal window is not an issue, since the operating system neither requires non-modal windows to have an input focus nor be closed before working with other windows. In order to bypass these windows during playback, turn on the On overlapping window | Ignore overlapping window option in your project’s properties. TestComplete also generates the OnOverlappingWindow event, which allows you to perform specific actions for non-modal overlapping windows.

If the window is modal, the operating system is not able to switch to other windows until this window is closed. TestComplete handles these windows automatically through a preset sequence. One important step in the sequence, “what does the script do now?”, is defined by you in the Playback section of your project properties. The sequence is --

  1. Script execution is delayed until expiration of the Auto-wait timeout interval that is set in the Playback options of your project.

  2. If the unexpected window is still open, TestComplete generates the OnUnexpectedWindow event.

  3. If the window is not closed by the OnUnexpectedWindow event handler, TestComplete posts an error message to the log along with an image of the unexpected window and then acts according to the Playback settings:

  4. If Stop on error is checked, then the run ends at that point.

  5. Likewise, if Stop execution is checked in the Unexpected Window section. (The difference is that Stop on error stops on any error, while Stop execution stops specifically on unexpected windows.)

  6. Otherwise, depending on the settings set in the On unexpected window section, any of the following actions is taken:

    • The test engine tries to simulate a click on the control currently focused in the window.

    • The test engine simulates pressing the Esc key.

    • The test engine simulates pressing the Enter key.

    • The test engine sends the close command to the window.

  7. If the window is still not closed (which may occur if you do not have a closing-action checked, or if the closing action(s) failed), the run ends.

When an unexpected window appears, TestComplete saves its image and posts an error message to the test log. If you need to perform specific actions, like saving the whole screen as an image, or launching a keyword test, you can use the event handler of the OnUnexpectedWindow event. See Handling Events and Creating Event Handlers for TestComplete Events for more information. Note that if you simulate a mouse click within the OnUnexpectedWindow or OnOverlappingWindow event handler, TestComplete will check for an unexpected window recursively.

Remarks

Currently, TestComplete does not handle the following window types as unexpected:

  • Modal windows and dialogs implemented as windowless objects. That includes --

    • Modal windows in Flex applications (for example, those created using the PopUpManager class).

    • Modal windows in web applications (for example, those created using the div element or JavaScript).

    • ActionScript errors displayed by Flash Player.

    • Forms in mobile applications.

  • Unexpected windows in Delphi and C++Builder CLX applications. This is due to specifics of the CLX library implementation: an application’s window can be activated even when a modal dialog is displayed. Because of this behavior, TestComplete does not treat modal CLX windows as unexpected ones.

To handle windows of these types as unexpected, use the WaitWindow or FindChild method to check if a modal window appears after your test performs actions that can lead to this. The following code snippet demonstrates this:

JavaScript, JScript

function Test()
{
  var p, unexpWnd;

  p = Sys.Process("MyApplication");

  // Do something
  ...

  // Handle possible unexpected window
  unexpWnd = p.WaitWindow("QWidget", "Error", -1, 1000);
  if (unexpWnd.Exists)
  {
    // Post the window image to the log
    Log.Picture(unexpWnd.Picture(), "Unexpected window detected.");
    // Close unexpected window
    unexpWnd.Close();
  }

  // Continue testing
  ...
}

Python

def Test():
  p = Sys.Process("MyApplication")
  
  # Do something
  ...

  # Handle possible unexpected window
  unexpWnd = p.WaitWindow("QWidget", "Error", -1, 1000)
  if unexpWnd.Exists:
    
    # Post the window image to the log
    Log.Picture(unexpWnd.Picture(), "Unexpected window detected.")
    # Close unexpected window
    unexpWnd.Close()

  # Continue testing
  ...

VBScript

Sub Test
  Dim p, unexpWnd

  Set p = Sys.Process("MyApplication")

  ' Do something
  ...

  ' Handle possible unexpected window
  Set unexpWnd = p.WaitWindow("QWidget", "Error", -1, 1000)
  If unexpWnd.Exists Then
    ' Post the window image to the log
    Call Log.Picture(unexpWnd.Picture, "Unexpected window detected.")
    ' Close unexpected window
    unexpWnd.Close
  End If

  ' Continue testing
  ...
End Sub

DelphiScript

procedure Test;
var p, unexpWnd;
begin
  p := Sys.Process('MyApplication');

  // Do something
  ...

  // Handle possible unexpected window
  unexpWnd := p.WaitWindow('QWidget', 'Error', -1, 1000);
  if unexpWnd.Exists then
  begin
    // Post the window image to the log
    Log.Picture(unexpWnd.Picture, 'Unexpected window detected.');
    // Close unexpected window
    unexpWnd.Close;
  end;

  // Continue testing
  ...
end;

C++Script, C#Script

function Test()
{
  var p, unexpWnd;

  p = Sys["Process"]("MyApplication");

  // Do something
  ...

  // Handle possible unexpected window
  unexpWnd = p["WaitWindow"]("QWidget", "Error", -1, 1000);
  if (unexpWnd["Exists"])
  {
    // Post the window image to the log
    Log["Picture"](unexpWnd["Picture"](), "Unexpected window detected.");
    // Close unexpected window
    unexpWnd["Close"]();
  }

  // Continue testing
  ...
}

See Also

Handling Events
Creating Event Handlers for TestComplete Events
OnUnexpectedWindow Event
OnOverlappingWindow Event
Testing Modal Windows

Highlight search results