TestCalc Sample
Description
This sample script illustrates a simple automated testcase
using Phantom. The script opens the Calculator application
(calc.exe) that comes with Microsoft Windows. It verifies
the state of the Calculator application (that it is in Scientific
mode), and that it is initialized to display '0.'. The script
then performs a simple math operation and checks the result.
Some of the Phantom features used are:
- Window Declarations File
- Button Interaction
- Edit Field Interaction
- Conditional Statements
- Exceptions
To use this sample, simply copy and paste
it into a new script and save it. Additionally, download
the 'calc.dec' file and save it to where the sample script
is saved. The calc.dec file can be opened in the Window
Declarations Recorder (WinDR.exe) utility, in Phantom Test
Driver, or in the Phantom Sidekick editor.
[ Back to Samples
]
[ Download Zipped Calc.dec
and TestCalc.psc Files ]
Test Calc |
#
This is a sample script that tests the operation of
# the calc.exe calculator program
included with Windows.
# This illustrates simple window
interaction by reading
# text and interacting with buttons. It
also shows some
# logical operations, exception
usage, and the use of
# the window declarations file.
#
# Note: The calc.dec file must
be in the same directory
# as this script.
# Include the Calculator Declarations
File
# This file would be recorded
using WinDR.exe
use "calc.dec";
# Prepare a global exception
exception e;
string sErr;
# Start the calculator program
System("c:\\Windows\\System32\\calc.exe");
# Ensure the calculator is in
Scientific mode
if(Calculator._View._Scientific.IsChecked()
== false){
# Scientific is not
selected, so select it
disp("Scientific Menu
Selected");
Calculator._View._Scientific.Select();
}
# Verify that the starting display
is '0.'
string sText = Calculator.Edit1.GetText();
sText = trim(sText); #
Remove any spaces
if(sText != "0."){
Calculator.Close();
sErr = "Calculator
not initialized to 0. (";
sErr = sErr + sText + ")";
e.SetError(sErr);
e.throw();
}
disp("Calculator initialized properly");
# Perform a Math Operation 4^2
Calculator.4.Click();
Calculator.x_y.Click();
Calculator.2.Click();
Calculator.TypeKeys("");
# Note that the '=' button is
a '_' in the window
# declarations file. Could
rename the '_' to
# 'Equal' in the declaration file
and use:
# Calculator.Equal.Click();
# Sleep for 1/2 second to ensure
calculator is
# done computing
Sleep(0.5);
# Verify that the result of 4^2
= 16
sText = Calculator.Edit1.GetText();
sText = trim(sText); #
Remove any spaces
if(sText != "16."){
# Capture a bitmap
of calculator error result and
# save it for later
analysis
bitmap b =
Calculator.CaptureBitmap();
b.Save("CalculatorErr.bmp");
Calculator.Close();
sErr = "Incorrect
calculator result (4^2=";
sErr = sErr + sText + ")";
e.SetError(sErr);
e.throw();
}
disp("Calculator computed 4^2
properly");
# Close the calculator
Calculator.Close();
disp("Test successful.");
| |