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 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.
If..End
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.
If..Else
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!”
Else
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:
If..ElseIf
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”
Else
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.
Cool!
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:
Good initiative. I am looking for quality QTP training for years. So far I had found that training on how to program with vb in qtp expert view works for me the best in a video tutorial mode.
When the lecture recorded his own screen , and share the vb code under the video, so i can give it a go by my self.
Something in this style:
http://www.youtube.com/user/QTPeLearn
But the biggest chalange for me , is the implementation, how to approach a probelm, how to analyse a web app, or a non gui/ data validation testing, and think how vb code can serve me here to automate the test case? should I use INSTR function or MID function? Where can I get an access into a bank of code blocks that can help me to navigate through my needs
Hi Joe,
Will definitely follow this series keep posting.
Thanks,
Navnath Patil.
Thats so true, record and playback hardly works.
Awaiting next post!
Look fwd to seeing more info from you. Have you used any of the frameworks that allow you to generate QTP code? We had one from an Indian Company that we’ve used but as it was proprietary and selling sw wasn’t there thing am now looking at TAO. Any other options that you know that are good?
Thanks…
I have a group of testers that are functionals, not programmers. I was looking at HP ALM Suite as an easy tool for them to test a new web program. You have any comments on HP ALM Suite vs QTP. I know HP ALM Suite can save money.
Bill Emery » Looks to me like QTP (now called Unified Functional Testing) is one of many product that make up HP’s Application Lifecycle Management Suite. Not sure how much is saved purchasing QTP standalone vs buying it as part of a bigger bundle.
David Gross » Thanks David! I have not used any frameworks (expect ones I’ve created) to generate QTP code. Can you send me a link to TAO? Cheers~Joe
Navnath Patil » Great – thanks Navnath!!
Aviad » Awesome feedback Aviad! I’ll try to meet your expectation. Cheers~Joe
Looking forward to this. I work with a small group of testers that uses most of the HP tool sets. In my two years with the team I have been focused on LoadRunner and load testing, but am wanting (needing) to start to branch out with QTP and want to lear more about it.
I follow your blog through the RSS feed and am glad that you continue to keep it updated and add content. I would definitely follow any QTP guidance you provided.
Thanks for sharing this and all of your other content with us!
Slade Burroughs » Cool – thanks Slade! I hope you find the future posts helpful. I’m still trying to figure out what to cover so your feedback much appreciated. Cheers~Joe
Hi Joe,
I think you definitely should continue with this initiative. I have been working with QTP for 6+ years and I love automation, from simple descriptive programming to strong run time libraries and automation frameworks. However, I recognize you are always learning new things and methodologies from others, and from the moment I found your site I have been visiting it often to find HP product news you don’t even easily find at their site. Great work.
Cheers!
Israel
keep it up. I am on board
Humayun » Cool – thanks Humayun! Let me know how your new job is.
Israel Valencia » Hi Isreal – Awesome! I’m glad you find the site helpful – I really appreciate your feedback. Cheers~Joe
Hi Joe,
Can you post some HPO-M47 Exam questions for us. It would be very helpful for preparing QTP11 Exam
Hi Joe,
I have accidentally found your web site. I would say that you have put tremendous effort. I really liked the way u explained the QTP. I am looking for more videos to come.
I want to be learn qtp and do automation :) any suggestion?? I have seen people asking question what is your automation framework. I really would like to know what is framework and how do you setup QTP framework? I think in automation framework is the primary key.
Keep posted nice suggestions!!
Thanks,
Kirti.
Kirti » Thanks Kirti! Glad you find the videos helpful. I will try to create a post/video about the QTP framework I created – thanks for the idea :)
Ram » Hi Ram – I would if I knew them :) Does anyone else know what type of questions are asked on this exam?
i was searching for QTP content online and i found article’s posted by you are really helpful.
Thanks for sharing them and keep posting :)
great work man..!!
Taran Vohra » Thanks Taran – Glad you find the site helpful!
Joe, I’m definitely interested. Please keep these coming!!
I particularly love the suggestion of using QTP to learn to program. In the past, I have treated learning these two technologies as two different learning paths, subsequently finding myself overwhelmed by the learning curves imposed by each, and never getting anywhere with EITHER. Approaching both QTP and VBScript at the same time (on the same learning path) — particularly if concepts are introduced simultaneously with the “how-tos”, is an excellent approach to learning. It’s results-oriented.
BTW, another personal challenge has always been figuring out the syntax of any language I’ve attempted to learn. Experienced programmers always assume that the new learner has some background in another language. So, the more you can clarify syntax (much like an English grammar text would), the BETTER for the wanna-be’s like me.
Thanks!!!
Gwen » Thanks for the feedback Gwen! 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
Kirti » Hi Kirti i created a post QTP – What type of automation framework do you use? that explains the framework that I use. Let me know if you have any questions. Cheers~Joe
Hi Joe! I am VERY interested in your videos. Do you have any existing videos? Please send me the urls.
Great information on your site! Thanks so much! Carol
Thanks Carol! I’ve only created two videos in this series so far — my new years resolution to to make more this year :) Here are the two so far:
Learning to program with QTP
and
QTP Variables
Let me know what you think. Thanks!
Thanks for all the information you provide through this site, we all love it.We will definately follow new series..
Thanks
Ravi
Ravi » Thanks Ravi! I’m trying to get more parts of this series posted.
Thanks for your Excellent initialization to start QTP from the basics.One kind suggestion is that,a training would be even more helpful if you can explain the upcomings information with simple examples and screenshots, so that it would help the concepts clearly.
I am inviting you to Guruface.com which have some of the best QTP/UFT training courses.
https://www.guruface.com/course/1353