Closing a tested application

In the Terminating a tested application recipe we have come to understand how to forcibly terminate the tested application, as well as ascertained that this method suits emergency cases exclusively.

In this recipe at hand we will take up an example of application termination.

Getting ready

Add a standard Notepad application to TestedApps (right-click on the TestedApps node in the project, then select Add | New Item and choose the C:Windows otepad.exe file).

How to do it...

In order to close a tested application we need to perform the following steps:

  1. Create and launch the following function:
    function testCloseNotepad()
    {
      var pNotepad = TestedApps.notepad.Run();
      var wNotepad = pNotepad.Window("Notepad", "Untitled - Notepad");
      wNotepad.Activate();
      wNotepad.Keys("Some text to prevent closing");
      TestedApps.notepad.Close();
    }
  2. In the result, the Notepad application will remain running, and in the log we will spot the following notification:

    The application "C:Windows otepad.exe" got a command to close, but it is still running, though the default timeout has expired.

  3. Now, modify the function by adding the following code to it, as shown in this example:
    function testCloseNotepad()
    {
      var pNotepad = TestedApps.notepad.Run();
      var wNotepad = pNotepad.Window("Notepad", "Untitled - Notepad");
      wNotepad.Activate();
      wNotepad.Keys("Some text to prevent closing");
      TestedApps.notepad.Close();
      if(pNotepad.WaitWindow("#32770", "Notepad", -1, 1000).Exists)
      {
        pNotepad.Window("#32770", "Notepad").Keys("~n");
      }
    }
  4. Now the Notepad application has closed; however, the notification is still there in the log. In order to get rid of the notification, let's replace the following line:
    TestedApps.notepad.Close();

    With this one:

    wNotepad.Close();

    Then launch the function again.

  5. In the result, the Notepad application is closed and there are no notifications in the log about it.

How it works...

When we close an application via the TestedApps object (as we have done in the first two examples), TestComplete prompts the application with a termination command and awaits for the application to complete. If this does not happen (as in our case, when Notepad awaits for us to push the button), TestComplete notifies us about that in the log.

Further on, we add the code, which verifies with the help of the WaitWindow method, whether the message box has appeared, and if so, we close it by pressing the buttons combination: Alt + N (which corresponds with pressing the Don't Save button). This way, we achieve the correct termination of the application.

And, finally, we substitute the call of the Close method for TestedApps with analogous one of the window. Thereby, the termination command is sent to the window and TestComplete does not wait for the application to close down.

There's more...

To close the application, it is not necessary to call the Close method. Instead, one could apply other methods for termination, for example, opting for the File | Exit menu item or sending such a combination of keys as Alt + F4 with the help of Keys method. Note, however, that sending key sequence requires that the target window be active, otherwise, it is possible to send the sequence to another window.

See also

  • If you are still unsure how the WaitWindow method works in the previous example, read the Waiting for an object to appear recipe in Chapter 5, Accessing Windows, Controls, and Properties
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.128.105