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.
Create the file myfile.txt
with any contents in the root directory of the C:
disk and start the Notepad application
In order to structure code we need to perform the following steps:
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(); } }
C:myfile.txt
, and then go through several menu items and check whether the necessary window has been opened.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(); } } }
menus
and caption
, while the script would already be aware how it should be handling things.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.
3.138.179.100