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.
In order to modify a test we need to perform the following steps:
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. 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(); }
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.
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(); } }
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]); } }
We have completed the modification of the recorded code within the three steps:
Test1Modified3
. This three-step accomplishment is called functional decomposition.