QTP – Learning to Program Using QTP – Variables

If else
Automation Guild Online Conference Top ad

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

Navnath Patil - April 12, 2012

Hi Joe,

Thanks very much for the post.

In my current project I need to access the name of the variable, I searched a lot but was not able to find the solution. Can you tell me how to display the name of the variable.
Dim var
var = “Hello World”
MsgBox var

This will display “Hello World”
Do you know any method by which we can display/access the actual variable name(var in this case) instead of the value stored in it, using QTP.

Thanks in advance,
Navnath Patil.

Taran Vohra - April 12, 2012

i never knew we had typename and print functions in QTP.
though i knew we dont need to declare variables in VBScript but i had no idea we called them Variant.
wow..!!! thanks for sharing

    Joe Colantonio - April 12, 2012

    Taran Vohra » NP-Thanks for the feedback Taran.

Alfred Esposito - April 12, 2012

Its one thing to stumble through QTP and figuring out certain things. Its completely another having it all laid out here in way that completely makes sense. Eagerly awaiting the next post.

    Joe Colantonio - April 12, 2012

    Alfred Esposito » Cool – Thanks Alfred! I’m still debating if each post in this series should have a video. Would you like to see videos for all the post?Cheers~Joe

Kulwinder - October 7, 2012

Hi Joe,

I am facing a problem in storing the output of the following in a variable

a = Window(“Flight Reservation”).Dialog(“Fax Order No. 1”).WinEdit(“# Tickets:”).Output CheckPoint(“# Tickets:”)

Here “a” is a variable declared in the beginning using DIM

can you please help me in this.


    Joe Colantonio - October 17, 2012

    Kulwinder » Hi Kulwinder – did you figure this out yet? It looks like you are missing parentheses:

    a = Window(“Flight Reservation”).Dialog(“Fax Order No. 1″).WinEdit(“# Tickets:”).Output (CheckPoint(“# Tickets:”))

sekou Diaby - December 22, 2015

Clearly understood.

sekou Diaby - December 22, 2015

Clearly understood. Here, I understood how variable works.

daniel - October 1, 2016

Is ther any possibility to put in msg box parameter value? for ex. i want to find 3 different products in store i have set parameter for all of those products in database. after each run i would like to see msg box with sentence “Product” parameter(describing already searching product from database) “find!”

please help

    Joe Colantonio - October 13, 2016

    Hi Daniel! So not sure I understand – you want the msg box to pull the parameters from the DB dynamically?

Click here to add a comment

Leave a comment: