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

by Joe Colantonio on June 22, 2012

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

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

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 

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!

Stay tuned for more in the series Learning to Program with QTP. As always, feel free to leave your comments on this or any of my posts. Cheers!

{ 6 comments… read them below or add one }

Jeff Colten June 27, 2012 at 5:34 pm

Thanks Joe. It should be noted that in the Select’s Case statements, multiple expressions can be listed:
Select Case enteredName
Case “Jeff”, “Joe”
MsgBox “Your name begins with a J”
Case “Frank”, “Amy”, “Steve”
MsgBox “I don’t know you”
End Select

Reply

Joe Colantonio June 28, 2012 at 7:40 pm

Jeff Colten » Thanks Jeff – good point! Thanks~Joe

Reply

chikki October 9, 2012 at 10:31 am

Joe, is there any performance factor between if condition and select condition, apart from reading is there any added advantage to go with case.

Reply

Joe Colantonio October 9, 2012 at 8:32 pm

chikki » You should use the select Case is to be used when you have to compare one value against many possible values. If you had a extremely long list of possible values I think the select case would get a little better performance rather than using multi-nested if statements. But I would not use that as the reason to choose on over the other.

Reply

Dhananjaya November 7, 2012 at 5:08 am

Hi Joe,
Your blog is really helpful . In my application data is getting stored in backend in XML files. so i wanted to know how can i validate backend data in xml files using QTP?

Reply

Joe Colantonio November 7, 2012 at 12:50 pm

Dhananjaya » Thanks – glad you like my blog! With respect to QTP and XML

QTP does not have any methods to run the XML file or push it to a server. QTP does have XML functionality to:

1. Verifying that the XML structure of a document adheres to an XML schema by creating XML checkpoints, and
2. Retrieving the attributes/elements of an XML file using the XMLUtil or XMLData objects.

What exactly are you trying to do?

Reply

Leave a Comment

Previous post:

Next post: