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.
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).
In order to close a tested application we need to perform the following steps:
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(); }
The application "C:Windows otepad.exe" got a command to close, but it is still running, though the default timeout has expired.
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"); } }
TestedApps.notepad.Close();
With this one:
wNotepad.Close();
Then launch the function again.
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.
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.
WaitWindow
method works in the previous example, read the Waiting for an object to appear recipe in Chapter 5, Accessing Windows, Controls, and Properties18.118.128.105