April 30, 2013

Script test in TestComplete

In the last post, we learned how to create a keyword test in TestComplete. I had mentioned then that Script tests are more suitable for people who know a scripting language like VBScript or JScript and who have written test automation before. In this post, let us see how to build a script test to test the Windows Calculator. We will use data parametrization, external data sources, conditional statements and loops. Rest assured that it will not be simple at all.

First, you open TestComplete on your Windows machine. Create a new project using the File | New | New Project... menu item. A wizard starts. Specify the Tested Application Type as Generic Windows Application. Select the scripting language as VBScript. Select Finish. You should have a new project open.

Next, you add the Windows Calculator to the TestedApps folder as shown below. If you add an application to the tested apps, it will be available for launch during the record process from the TestComplete floating toolbar.


Now, click the Record button. TestComplete minimizes to a toolbar. In the toolbar, click the Show Additional Buttons arrow. Then click the Run Tested Application button and select Calculator. In the Calculator, click all digit buttons, 0 to 9 and operator buttons, +, -, * and / and close the application. A keyword test is generated. Right-click on the keyword test and click Convert to Script... Give the script name as TestCalc. TestComplete generates the script test for you.

We will re-use only a few auto-generated statements from the default script test. However, the good thing that TestComplete does is to automatically create name mapping Aliases for you that you can reference in your own code easily. Double-click NameMapping item in Project Explorer to see the window below.

Next, we need to create the test data. This test consists of two operands that are the numbers which will be operated. The test data also consists of the operator that is the operation that will be performed on the two operands. Finally, the test data includes the expected result. Copy the below test data and paste it into a .csv file.

operand1,operator,operand2,result
2,add,3,5
4,subtract,2,2
5,multiply,6,30
100,divide,5,20
123,add,200,323
50,add,30,80
20,subtract,40,-20
51,multiply,4,204
10,divide,2,5
2,add,13,15

Let us write the Main routine of the script test. This routine is run when the script test is run. It launches the Calculator application, opens the .csv file, calls the VerifyResults routine for each row of the test data and finally closes the application. Notice that the back-slashes are escaped in the .csv file path. Copy the code below and paste into TestCalc script test. Note: all source code is in italics font.

Dim wndCalculator

Sub Main
TestedApps.calc.Run
Set wndCalculator = Aliases.calc.wndCalculator
Dim page32770
Set page32770 = wndCalculator.CalcFrame.page32770
DDT.CSVDriver("E:\\Calculation.csv")
'runs the VerifyResults routine for each row in the csv file
DDT.CurrentDriver.DriveMethod("TestCalc.VerifyResults")
wndCalculator.Close
End Sub


Before writing the VerifyResults routine, we need a routine to click the digit buttons in the Calculator application. Note that the button name suffixes in  the application are different from the respective digits. Copy the code below and paste it below the other code.

Sub PerformClick(operand) 
'Perform the click in the calculator application
Dim panel
Set panel = wndCalculator.CalcFrame.page32770
Select Case operand
Case "0"
panel.btn6.ClickButton

Case "1"
panel.btn4.ClickButton

Case "2"
panel.btn.ClickButton        
Case "3"
panel.btn2.ClickButton        
Case "4"
panel.btn7.ClickButton        
Case "5"
panel.btn8.ClickButton        
Case "6"
panel.btn9.ClickButton        
Case "7"
panel.btn10.ClickButton        
Case "8"
panel.btn11.ClickButton        
Case "9"
panel.btn12.ClickButton        
End Select 
End Sub


VerifyResults is the most important routine of this script test. Firstly, it reads the operand 1, operator, operand 2 and expected result values from a row of the .csv file. Next, it calls the PerformClick routine for each digit of the operand. The operand can be a single digit or multi-digit number. Therefore, PerformClick has to be called for each digit of the operand. Then, it clicks the button corresponding to the operator. Then it clicks the button(s) for operand 2. I was not able to get TestComplete to identify the object/ property pair that shows the result in the Calculator. Therefore, I copy the Calculator result to the clipboard and use it as the actual result. Copy the code below and paste it under existing code in the script test.

Sub VerifyResults

'Read the individual operand, operator and result values from the csv file
Dim operand1, operand2, operator, expectedResult, actualResult
operand1 = DDT.CurrentDriver.Value("operand1")
operator = DDT.CurrentDriver.Value("operator")
operand2 = DDT.CurrentDriver.Value("operand2")
expectedResult = DDT.CurrentDriver.Value("result")
Dim logString
logString = operand1 & " "& operator & " " & operand2 & " :The expected result is " & expectedResult & " and the app returns "

Dim count

'Click the buttons for each digit of operand 1

For  count = 1 To len(operand1)
Call PerformClick(left(operand1,1))
If len(operand1)>1 Then operand1=Mid(operand1,2 ,len(operand1)-1)
Next

'Click the button for the operator
Dim panel
Set panel = wndCalculator.CalcFrame.page32770

Select Case operator
Case "add"
panel.btn5.ClickButton '+
Case "subtract"
panel.btn13.ClickButton '-
Case "multiply"
panel.btn1.ClickButton '*
Case "divide"
panel.btn14.ClickButton '/
End Select

'Click the buttons for each digit of operand 2
For  count = 1 To len(operand2)
Call PerformClick(left(operand2,1))
If len(operand2)>1 Then operand2=Mid(operand2,2 ,len(operand2)-1)
Next

'Click the equals button
panel.btn3.ClickButton '=
Sleep (1000)

'Get the result in the app
Call wndCalculator.MainMenu.Click("Edit|Copy")
actualResult = Sys.Clipboard
logString = logString & actualResult
If StrComp(expectedResult, actualResult, vbTextCompare) = 0 Then
  logString = logString & " that matches."
Else
  logString = logString & " that does not match."
End If

Log.Message logString
End Sub


Now, you should be ready to run the script test. Right-click on the script test and click Run and then Run Main routine. You should get the a script test log similar to the one below. Notice that the Event check box is unchecked to de-clutter the test log.


Let me know if you have any questions on the script test above.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.