Preconditions and postconditions are often used both in manual and in automated tests for the purpose of assigning testing conditions or for returning the settings previously changed in the process of the test execution.
One of the most frequently used examples of pre-and postconditions for automated tests is launching and closing of the testing application. Such an approach explicitly allows us to avoid having to repeat these actions at the beginning of the test.
In this recipe we will consider an example of handling the Notepad application, which will automatically open and close in the pre-and postconditions, respectively.
To create the tests with pre- and postconditions, it is necessary to carry out the following actions:
OnStartTest
event handler and add the following code to it:Log.Event("Starting Notepad"); Sys.OleObject("WScript.Shell").Run("notepad.exe");
OnStopTest
event handler and add the following code to it:Log.Event("Stopping Notepad"); Sys.Process("notepad").Terminate();
function testNotepad1() { var np = Sys.Process("NOTEPAD").Window("Notepad"); np.Keys("Test #1"); } function testNotepad2() { var np = Sys.Process("NOTEPAD").Window("Notepad"); np.Keys("Test #2"); }
In the result, all the tests will be launched, while each one of them will have a new copy of the notepad opened. As the test completes, the Notepad application will be closed.
The events OnStartTest
and OnStopTest
will be processed accordingly, before and after the tests are launched.
These events will be triggered individually per each test only in case the tests are launched via test items. If several tests should be launched in series from one and the same function, the events will be triggered just once (before the main function invocation and on its completion).
3.129.194.106