Modifying the recorded test

As we have seen in the previous recipe, a script can be recorded automatically; however, in the result of such recording, an unreadable code will be generated, which is difficult to modify.

Let's suppose that in the result of requirements for the tested application altering, we have to add 20 more button-clicks to various buttons. The simplest way is to copy the last line of code (in which the = button is clicked), however, the size of our script will increase significantly, which will worsen the readability.

In this recipe, we will modify the recorded code in such a way that we will minimize the necessitated actions to append the new Calculator Plus button-clicks.

How to do it...

In order to modify a test we need to perform the following steps:

  1. First and foremost, we will make the code conformable to a unified style so that button-clicks appear in the same way in any given case. To this end, we should get rid of the variable btn2; and the code that stands for the button-click should be re-written in exactly the same manner that the button-clicks for the rest of the buttons have been coded.
  2. Together with that, we will join declaration of the wndSciCalc variable with its initialization and assign the variable a different name. In the result, the code will appear as follows:
    function Test1Modified1()  
    {  
      wndSciCalc = Sys.Process("CalcPlus").Window("SciCalc", "Calculator Plus");  
      wndSciCalc.Window("Button", "2").ClickButton();  
      wndSciCalc.Window("Button", "+").ClickButton();  
      wndSciCalc.Window("Button", "2").ClickButton();  
      wndSciCalc.Window("Button", "**").ClickButton();  
      wndSciCalc.Window("Button", "5").ClickButton();  
      wndSciCalc.Window("Button", "=").ClickButton();  
    }  

    Tip

    Downloading the example code

    You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

  3. The same repetitive actions are best to be coded as loops. In the given case, we will use the for loop, and the text of the major buttons will be announced as an array:
    function Test1Modified2()
    { 
      var aButtons = ["2", "+", "2", "**", "5", "="];  
      var wCalcPlus = Sys.Process("CalcPlus").Window("SciCalc", "Calculator Plus");
      for(var i = 0; i < aButtons.length; i++)
      {
        wCalcPlus.Window("Button", aButtons[i]).ClickButton();  
      }
    }
  4. Lastly place code for button-clicks into a separate function:
    function clickCalcButton(caption)  
    {  
      var wCalcPlus = Sys.Process("CalcPlus").Window("SciCalc", "Calculator Plus"); 
      wCalcPlus.Window("Button", caption).ClickButton(); 
    }  
      function Test1Modified3()  
    {  
      var aButtons = ["2", "+", "2", "**", "5", "="];  
      for(var i = 0; i < aButtons.length; i++)  
      {  
        clickCalcButton(aButtons[i]);  
      }  
    } 

How it works...

We have completed the modification of the recorded code within the three steps:

  • On the first step we simply arranged the code so that it followed the same style. These changes are cosmetic; we need them just to simplify the following modifications.
  • On the second step we made a serious change: made an addition of the loop to iterate through the repetitive actions of the button-click. As a result, first of all, we reduced by half the size of the function; secondly, we facilitated further work. Now, if we need to add new button-clicks, it would be enough to add the heading of the button to the array, and the loop will automate any further actions with the button.
  • Finally, on the third step, we have organized the calculator into a separate function. Thus, we have concealed the details of the realization of these actions by leaving only high-level actions in the testing function Test1Modified3. This three-step accomplishment is called functional decomposition.

See also

  • The Organizing script code in the project and Creating framework using the OOP approach recipes in Chapter 3, Scripting, will help you to arrange you modifications in script code
..................Content has been hidden....................

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