Creating preconditions and postconditions for tests

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.

How to do it...

To create the tests with pre- and postconditions, it is necessary to carry out the following actions:

  1. Create the OnStartTest event handler and add the following code to it:
    Log.Event("Starting Notepad");
    Sys.OleObject("WScript.Shell").Run("notepad.exe");
  2. Create the OnStopTest event handler and add the following code to it:
    Log.Event("Stopping Notepad");
    Sys.Process("notepad").Terminate();
  3. Now, let's create several test examples:
    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");
    }
  4. Let's create the corresponding test items (right-click on the name of the project and navigate to Edit | Test Items). This should appear as follows in the end-result:
    How to do it...
  5. Let's launch the project (right-click on the name of the project and select the Run menu item).

    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.

How it works...

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).

See also

  • Reading up on adding event handlers is available in the Creating event handlers recipe
  • Launching and completion of programs is particularly dealt with in the Running external programs and DOS commands, Running a tested application from the script, and Terminating a tested application recipes in Chapter 2, Working with Tested Applications
..................Content has been hidden....................

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