Learning to Program with QTP

Automation Testing Published on:
Code Hacker

Announcing a new series: Learning to program using QTP! The series will cover learning to program using QuickTest Professional. Even though QTP is a functional testing tool, and HP (like most vendors,) tries to sell their tools as record and playback — what most will not tell you is that you'll still need to have some basic programming concepts under your belt.

Why can't I just record and playback a script?

First — I've never used a script that I simply recorded and played back for anything other than a quick demo. Simply put: record and playback is not reliable. This is mostly due to hard-coded values captured that may not necessarily work on playback. Also – there's always a need to add validation, verification, synchronization and additional flow- control logic to most scripts. Virtually all these changes require that an engineer have some knowledge of programming.

Eating your own dog food/ Why would I use QTP to Learn Programming?

Have you ever heard the slang “eating your own dog food? It usually means that a company uses the products it has manufactured. So, you would assume that a company like HP would use its products (like QTP and LoadRunner) to test its own software.

I think this also applies to self-learning ; by utilizing the tools you already use in your job to expand your skills. QTP is perfect for learning programming, because it allows you to start small and use a tool that you are already using every day. Plus, you will be learning “on the job” about how to solve real-world problems.

What programming language does QTP use?

Tests in QuickTest Professional are coded using VBScript:

    • VBScript is a programming language that was created by Microsoft as a scaled down version of the company's Visual Basic language.
  • VBScript is also considered a scripting language like Perl, python and ruby. A scripting language's code is interpreted at run time instead of being compiled into an executable like Java or C#.

QTP's Expert View

To get started, you'll want to open up QTP and navigate to the Expert View from the View>Expert View menu option. The Expert view gives one the ability to begin entering VBScript code directly into QTP and running QTP — almost treating it like a computer.

QTP Debugging Features in the Expert View

Using the Expert View also allows one to use some of the basic built debugging features found in QTP like:

  • Adding breakpoints
  • Finding syntax issues
  • Executing a script from any line within the test
  • Stepping through a script line by line
  • Keeping track of a variables run-time value

Don't worry if you aren't familiar with these operations. I plan to go into detail about them in a future post.

First Video in Series “Learning to Program with QTP”

Today we are going to take a look at three basics concepts:

  • Setting up QTP
  • Terminology
  • Some good practices to follow when coding in QTP

Variables – How do I store stuff in QTP?

A variable is used to store dynamic data types like strings, numbers and object values. The variable is a name assigned to a location in your computer memory where the date used by QTP is stored. Think of it as a way to reserve memory in your computer.

For example, say we've created a variable named “Enabled” that QTP uses to store a textbox's enable property that has a value of “true.” The variable called Enabled is really just a pointer to a location in the memory where the value “true” is stored.

How to Declare variables in QTP / UFT

Although VBScript doesn't require that you declare your variables before using them, it's a good practice to do so. To declare a variable in VBscript, you must use the Dim statement:

Dim myName

Setting values

You can put a value into a variable by using the assignment operator which is the equal sign (=). The variable always is on the left of the equal with the value you want to assign it is placed to the right of the equal sign. So to assign the myName variable a value you would write:

myName = “Joe Colantonio”

Now, QTP knows what you're talking about when you type myName:

Msgbox myName

Keep in mind that variable in QTP are not case sensitive so QTP recognizes MYNAME ,MynAme and myName as the same exact variable.

No need to declare a data type

Unlike some other languages, including java and C#, VBScript doesn't require you to declare in advance the data type of a variable. All variables in VBScript are the same data type, which is a Variant. When you run your script at runtime, VBScript automatically assigns a subtype to the variable. So, a variable named myName can store both a string:

myName = “Joe Colantonio”
Msgbox TypeName(myName)

and an integer value:

myName = 10
Msgbox TypeName(myName)

Basically, VBScript has a built-in method of looking at a variable's value and automatically deciding what its subtypes should be.

QTP Literals

Another term to be aware of is “literal.” A literal is value that is hardcoded directly into your script. As a result, said value cannot change during the lifetime of a script –in other words, the time between when a QTP script starts executing until the time it stops. A variable is the opposite — its value can change throughout the lifetime of your script. For example, if we wrote:

myFirstName = “Joe”
msgBox “Hey ” & myFirstName & ” where you going with that gun in your hand”

The text “Hey” is a literal and can't change since it's hardcoded into the script. Also in this example, the & (ampersand) is an operator that allows you to concatenate (or join) one or more strings together.

QTP VBScript Operators

Operators used in QTP in the script's VBScript code are used to change or test a value. You can also use them to perform things like standard mathematical operations: plus (+), multiply (*), divide (/) and subtract (-).

Sum = 50 + 20
Msgbox Sum

QTP How to see results

You may be wondering how you can see the current value of a variable. So far we have used the msgbox function to show the result but there are two other ways to see a variables value.

  • Print Statement

    You can also use the QTP's print statement to display info in the QuickTest print log window. The print window will remain open until you close it. For example:

    myName = “Joe Colantonio”
    print myName

  • ReportEvent Method

    Both the msgbox and print statements are helpful if your debugging a script but what if you want to store the variable's value to view and keep as part of the test run's history. For this you would use the ReportEvent Method. This will report an event to the run results of your tests:

    myName = “Joe Colantonio”
    Reporter.ReportEvent micDone, “My variable name was ” & myName,myName

When bad things happen

If you are creating input parameters for your QTP script, you'll need a way to verify that the data being passed to your variables is what is expected. For example, if your script is performing a calculation and someone passes a string rather than a number, an exception will occur. The example below will generate a Type mismatch error:

x = 10
y = “joe”
sum = x + y
msgbox sum

When you are accepting inputs from a user or reading from a file you often have no control over what is being passed to your test's variables. Luckily, VBScript has some functions that can help.

Is functions

One method of checking what has been passed to your variables is to use the IsNumeric function. For example the following code will check if the variable “y” contains a number, and will return either true or false.

y = “joe”
isThisNumber = IsNumeric(y)
msgbox isThisNumber

There are also some other IS functions you can use:

  • IsDate() – Returns a Boolean value indicating whether an expression can be converted to a date.
  • IsEmpty() – Returns a Boolean value indicating whether a variable has been initialized.
  • IsNull() – Returns a Boolean value that indicates whether an expression contains no valid data.
  • IsObject() – Returns a Boolean value indicating whether a variable has been initialized.
  • IsArray() – Returns a Boolean value indicating whether a variable is an array.

That's it for variables!

Stay tuned for my next post, which will cover Flow Control operations in QTP.

QTP – Learning to Program Using QTP – Variables

QTP/UFT – Making Decisions Using If..Then and Select Case Statements

Clean Test Code – Test Automation Code is Real Code

How to make decision in QTP Unified Functional Testing

Creating automation test scripts usually requires verification that an application's property or value is correct. This verification usually produces a pass or fail in your test results.

It is also necessary at times to perform an action in your script only if a certain condition occurs. This is called branching, which is the process of making decisions in your QTP script and then, based on those decisions, running one block of code and not the others.

In QTP's VBscript there are two types of branching:

  • If..Then Statements
  • Select Case Statements

QTP Comparison Operators

Before we get to the IF statement, we need to take a quick look at VBScript's comparison operators. For example:

myName = “Joe”
enteredName = inputbox(“Enter a name”)
isEqual = myName = enteredName
msgbox isEqual

If you enter Joe in the input box, the return value will be True. If you enter any other name the value will be False. This is the function of comparison operators. Here's the list of VBScript operators:

Operation Description
= equals
<> Not equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to

QTP UFT Logical Operators

In addition to comparison operators, you can also use logical operators. The two logical operators I use all the time are the And (&&) and Or (||) operators – both of which are used for doing logical operations on expressions. For example:

myFirstName = “Joe”
myLastName = “Colantonio”
enteredFirst = inputbox(“Enter a first name”)
enteredLast = inputbox(“Enter a first name”)
isThisMyName = (enteredFirst = myFirstName AND myLastName = enteredLast)
msgbox isThisMyName

If you enter Joe in the first input box and Colantonio in the second input box, the return value will be True. If you were to change one of the values and rerun the test, the logical operator's value would be False. To determine whether at least one of the values is what you expected, you would need to use the OR operator, which would look like this:

myFirstName = “Joe”
myLastName = “Cola”
enteredFirst = inputbox(“Enter a first name”)
enteredLast = inputbox(“Enter a first name”)
isThisMyName = (enteredFirst = myFirstName OR myLastName = enteredLast)
msgbox isThisMyName

Because at least one of the variables values is expected, the above will return True.

Finally, this brings us to how we make decisions with QTP's VBScript code using the IF statement.

The If Statements

The If..Then statements can be used to evaluate whether a condition is true or false, and depending on the result, to specify one or more blocks of code statements to run. There are three types of If statements that are commonly used in QTP.


Sometimes when scripting a test you only want to run a block of code if a certain condition is found and do nothing if the condition is not found. In those cases you could use an IF ..End statement. For example — say I wanted to check the name a person entered in an input box, and only if the name is the same as mine display a message:

myName = “Joe”
enteredName = inputbox(“Enter a name”)
isEqual = myName = enteredName
If isEqual = True Then
MsgBox “Cool – thats my name too!”
End If

This is the simplest form of an IF statement.


The If..Else statement is used when you need to run one block of code for one condition and another block for all other conditions. Let's use the same example as the above but this time, if the person types in a name other than Joe, show a different message box.

myName = “Joe”
enteredName = inputbox(“Enter a name”)
isEqual = myName = enteredName
If isEqual = True Then
MsgBox “Cool – thats my name too!”
Msgbox “Hi ” & enteredName & ” my name is ” & myName
End If

This time if you type in Joe you should get:

And if you type in another name say “Gino” you should get a different message box:


The If..ElseIf statement is used if you have more than two things to evaluate. For example:

myName = “Joe”
enteredName = inputbox(“Enter a name”)
isEqual = myName = enteredName
If isEqual = True Then
MsgBox “Cool – thats my name too!”
Elseif enteredName = “Jimi” then
MsgboX “Hi Jimi – Jimi Hendrix is my favorite guitar player”
Elseif enteredName = “Spoke” then
Msgbox “Live long and prosper”
Elseif enteredName = “Yoda” then
Msgbox “May the force be with you”
Msgbox “Hi ” & enteredName & ” my name is ” & myName
End If

As you can see, a nested If..ElseIf statement can check for multiple conditions. If you have a lot of things to check for, however, it can get a little cumbersome. Which is why, for readability's sake, it's often better to use a Select Case statement.

The Select Case Statement

In addition to the If..Then statement, there's a similar statement called the Select Case. If you have multiple conditions that need to be evaluated, the Select Case is usually easier to read than a long, nested If..Then.ElseIf statement. Also — if you're evaluating different possible results for the same expression, like the above If Else if statement, the Select Case is definitely the better option. This is how the previous example would look as a Select Case:

myName = “Joe”
enteredName = inputbox(“Enter a name”)
Select Case enteredName
Case “Joe”
MsgBox “Cool – thats my name too!”
Case “Jimi”
MsgboX “Hi Jimi – Jimi Hendrix is my favorite guitar player”
Case “Spock”
Msgbox “Live long and prosper”
Case “Yoda”
Msgbox “May the force be with you”
Case Else
Msgbox “Hi ” & enteredName & ” my name is ” & myName
End Select

Did you notice how the Case Else will run if none of the other options return true? Using a Case Else statement is considered a good practice even if you think there'll never be a situation where the Case Else would execute.


Also make sure to read Clean Test Code – Test Automation Code is Real Code

Here are more post in my Learning to Program with QTP:

QTP – Learning to Program Using QTP – Variables

QTP – Learning to Program with QTP – Video 0 “Hello World!”

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
Code Hacker

Must See TestResults.io Product Review

Posted on 11/21/2022

In this post, we'll look at TestResults.io for a product review. When creating ...

Unlocking end-to-end testing for all with mabl

Posted on 09/27/2022

A recent *study showed that 75% of organizations use a combination of manual ...

AI Powered API Testing (Meet Loadmill)

Posted on 09/21/2022

I just learned of a great tool (Loadmill) to help automated API test ...