Structuring code using loops

In the Modifying the recorded test recipe in Chapter 1, Getting Started, we have briefly dwelled on the topic of loops, having used the for loop for clicking several buttons. In this recipe, we will consider this issue in greater detail. Testers who are not familiar with programming, would often make the error of redundantly repeating code. This may relate to recording tests via recorder or with copying blocks of code with further insignificant changes. This is considered to be bad style in programming, because, in the future, when we would need to introduce any changes into the code, we would have to change it in all the places where it had been copied to.

Let's consider such an assignment. We have to create a smoke-test for Notepad, which will be, first of all, quick, and secondly, will carry out only superficial checks just to make sure that the application does not have some critical drawbacks that could stand in the way of conducting complete testing. As a task for such a smoke-test, we will open all the available dialog windows that can be opened in the Notepad application, to check their headings, and then close them. Such windows in Notepad application are eight in number: Open, Save As, Page Setup, Print, Find, Replace, Go To, and Font.

The simplest way to create such a test would be just recording it. However, if in the future we will have new windows to be added to the test, we would have to copy the available code (several lines), change the headings of the menu items and headings of the windows and insert them at the end of the test. In the result, with each added window, our test will tend to bloat. If we make up our minds in the future to check availability of the Cancel button in the test on top of checking the heading of the window, we would have to re-write several lines of code and copy them as many times as many windows are set on testing. And this is just a smoke-test! If we are automating more complicated scenarios, the changes would be just as complex as well.

Let's take a look at how it would be possible to implement such a check correctly.

Getting ready...

Create the file myfile.txt with any contents in the root directory of the C: disk and start the Notepad application

How to do it...

In order to structure code we need to perform the following steps:

  1. First, let's write a script that will launch Notepad with the open file of C:myfile.txt, select one of the necessary menu items (for example, File | Open) and close the dialog window that was opened at the point of selecting the underlying menu item. In the result we will obtain the following function:
    function smokeTest()
    {
      var pNotepad = Sys.Process("notepad");
      var wNotepad = pNotepad.Window("Notepad", "* - Notepad");
      wNotepad.MainMenu.Click("File|Open...");
    
      var dlg = pNotepad.WaitWindow("#32770", "Open", -1, 3000);
      if(!dlg.Exists)
      {
        Log.Error("Dialog window didn't open");
      }
      else
      {
        dlg.Close();
      }
    }
  2. Now, it is necessary to modify the function in such a way that Notepad could open to the next file: C:myfile.txt, and then go through several menu items and check whether the necessary window has been opened.
  3. We have several menu items and several window headings that correspond to the given menu. The simplest way would be declaring two arrays: in the first array we would enumerate the menu items, and in the second array, the window headings.
  4. After that in the loop we would iterate through the loop and select the necessary menu items one-by-one, checking into the corresponding window headings. The new function would look like this:
    function smokeTest()
    {
      var menus = ["File|Open...", "File|Save As...",
      "File|Page Setup...", "File|Print...",
      "Edit|Find...", "Edit|Replace...",
      "Edit|Go To...", "Format|Font..."];
      var captions = ["Open", "Save As", "Page Setup",
      "Print", "Find", "Replace",
      "Go To Line", "Font"];
    
      Win32API.WinExec("notepad.exe c:\myfile.txt", SW_NORMAL);
      var pNotepad = Sys.Process("notepad");
      var wNotepad = pNotepad.Window("Notepad", "* - Notepad");
    
      for(var i = 0; i < menus.length; i++)
      {
        wNotepad.MainMenu.Click(menus[i]);
        var dlg = pNotepad.WaitWindow("#32770", captions[i], -1, 3000);
    
        if(!dlg.Exists)
        {
          Log.Error("Dialog window didn't open");
        }
        else
        {
          dlg.Close();
        }
      }
    }
  5. This new code variation works in the loop with several windows. If, in the future, we have a new window to be added to the given test, we will simply add two new elements to the arrays menus and caption, while the script would already be aware how it should be handling things.

How it works...

At first, we declare the two arrays which contain menu items and windows' headings. If new menu items are added to the tested application in the future, we will be able to add them for testing simply by adding new items to these arrays. Then we would launch the Notepad and initialize variables to work with the application.

Further on, in the loop, we go through all the elements of the menu (the menus array) and select the menu items one-by-one. Each time, having chosen the next menu item, we wait for the new window to appear with the help of the method WaitWindow lasting 3 seconds (3000 milliseconds). If the window does not pop up, the error notification is logged, if the window pops up, it is then closed.

..................Content has been hidden....................

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