When we do software test automation, one of the most common tools we use is Quick Test Professional (QTP). VBScript (VBS) is the default programming language for QTP. For a junior tester, recording, modifying and playing a script is enough for a beginner. But such methods have limitations since many test cases need more logic and conditional interaction. Going beyond script recording requires using VBS. VBS not only provides script robustness, but also allows you to build a flexible automation framework. In this blog, I’ll write a brief script to show some basic principles when using VBS for QTP.
VBS – Logical If
The Logical If statement is one of the most widely used in VBS. As in Visual Basic, its format is: “If…then…else…end if.” This script demonstrates its usage and verifies if a file exists on our local computer.
setfso = CreateObject(“scripting.filesystemobject”) ‘define fso object
iffso.FileExists(“C:\boot.ini”) then ‘judge if file exist
msgbox “File does not exist”
VB Script Analysis
Defining the fso variable is the first thing that we need to do. It stands for file system object. Then we use “.” to define its property as “.FileExist”.
If this file boot.ini exists on the C disk root, we output a message box with “File exists” or “File does not exist” by using“if” statement.
Common VBS Programming Usage
“If …then…else…end if” is one of the most commonly used VBS statements, but there are many others such as:
• “select case1…case2…end case”
• ”do while…loop”
Other than standard programming logic statements as mentioned above, VBS has many more advanced programming capabilities. For example, VBS supports Regular Expression, a concise and flexible means to “match” (specify and recognize) strings of text. It also has the capability to get test data from a data list using“Getitem” and “GetROProperty”, or use “wait x” (x is the number of seconds, for example, wait 5 means wait 5 seconds) to set wait times and“SystemUtil.Run” to launch another program. We’ve found these capabilities a must in just about all software test automation projects we work on.
In general, QTP has very strong capabilities for software test automation especially with the flexibility and robustness of VBS. Next time, we’ll introduce more advanced functions and how to use them in your test automation efforts.