Adding Conditional and Loop Statements in Keyword View – QTP Tutorial #4

By Vijay

By Vijay

I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated November 20, 2024

Let’s continue the QTP training series with our next QTP tutorial #4 – Adding Conditional and Loop Statements in Keyword View.

Understanding the QTP keyword and expert view feature in detail is very important to learn QTP. Hence, we have divided this Keyword View tutorial into four articles. This is the third article in the Keyword View series.

=> Click Here For The QTP Training Tutorials Series

Adding Conditional and Loop Statements

Condition and Loop Statements in Keyword View

A quick recap of what we’ve learned so far

We acquainted ourselves with the Keyword View of the QTP and we are trying to understand the different features this view provides and see how these features can be used to write effective tests. To do that, we are working on a basic example where we recorded a series of simple operations on the Sample Flight Application.

This aids in understanding how QTP translates the steps performed by the application into a table with 4 essential columns of the Keyword view. Not only does QTP show the user the operations performed in terms of a table but it also lets the user manipulate the steps from this very table.

We have explored how a standard step can be added to the Keyword view. Apart from a standard step, this view also displays the conditional and loop statements in a test.

In this article, we are going to explore how QTP’s Keyword view handles these statements.

First, what does Loop and If statements do?

Loop Statement: This is a part of programming logic that is used when a particular set of statements have to be executed repeatedly for a specific number of times. Another variation of this statement is to repeat execution for a block of code until a certain condition is satisfied or while the said condition is satisfied.

If statement: As the name indicates, the if statement in a program is used to perform a check for a certain condition to be satisfied, and when it does, a certain block of code gets executed. Variations to an ‘if’ statement is ‘if ..else’ and ‘if.. else if…’

The Keyword view has a different icon to indicate each of these statements in the Keyword view. Let’s include some dummy steps into our program and see how these statements appear in the keyword view. As the steps that I am about to insert are dummy, I am going to put the code directly into Expert view and observe it in the Keyword view.

#1) Conditional Statements

QTP Conditional statements

Given above are the different variations of the ‘if’ statement that I entered into the expert view and as I said all the code does not serve any purpose except for our understanding. We will note how these statements are represented in the Keyword view.

QTP Conditional statements 2

As you can see, each statement type is given a different icon for identification.

#2) Loop Statements

Let’s look at how these statements are represented. We will follow the exact method that we did for the conditional statement.

QTP Loop Statement

Adding Condition Statements to the Keyword View

In my above examples, I have added the conditional statements from the Expert view and we merely looked at their representation in the Keyword view. So far we have not added them directly from the Keyword view. The Keyword view does provide a way for these statements to be added directly to the test. Here is how you can do it.

Click anywhere in the Keyword view, just like how you would do it to add a standard statement, right-click and choose “Insert->Step” followed by the “Conditional Statement” and the statement that you would need.

Here is how it looks:

QTP Conditional statements 3

As you can see above, An If…Then, Elseif..then, Else are the statements that can be chosen based on the requirements of the tester.

Let us choose an ‘if..then’ and see what happens.

QTP Conditional statements if then

This is what gets inserted. Now, just in the same way we would do for a standard step, we will need to define a conditional statement too.

Item: Click on the ‘item’ column corresponding to the ‘if’ statement and all the objects available will be displayed. Again, this feature is not different from the standard step definition. So choose an object that you wish to act on. As an example, I am going to check if the ‘Password’ field exists. So I choose “Password” from the list.

Operation: Again, a list of all the functions available will be displayed when this column is clicked on. Since I am checking if the password field exists or not, I will choose ‘Exist’

Value: The argument/arguments for the function is what this field is. For the ‘Exist’ function an argument is not necessary, so we will leave it empty.

Let us now look at how this new statement looks in the keyword view as well as in the Expert view.

Keyword view

QTP Conditional statement keyword view

Expert view:

QTP Conditional statements Expert view

As you can see, the ‘if’ statement is defined.

Now, we have to add code or statements to define what needs to be done if the condition becomes true.

For the sake of our example, if the password box exists we will enter a value in the field. How can we accomplish this? Once a conditional or loop statement has been added, any steps that the user adds or records will be added directly to them unless explicitly specified. The statement added or recorded could be a standard step or another conditional or loop statement (nesting).

So, to define the ‘Then’ part of our ‘if’ statement, I am going to record the action of entering the password value. To do so, I will choose ‘Record’ from the menu options and enter a password value in the ‘Flights’ Login page. Take a look at the screenshots below, on how the recorded statement got directly added under the ‘if’ statement.

QTP Conditional statements if statement

QTP Conditional statements if statement expert view

Any steps you add further will be added under the ‘if..then’ block.

If you want to add outside the block, select the block (the ‘if’ statement), right-click and choose ‘Insert New Step after block’.

Insert new step after block

The new step just comes as a sibling to the ‘if’ block.

Insert new step after block 1

Now that we have understood the basic functionality, it goes without saying that all the other conditional statements work the same way and any of them can be used as needed by the programmer’s logic.

Adding Loop Statements to the Keyword View

The procedure to add the loop statement is by right-clicking in the keyword where you want it to get inserted and choose “Insert New Step -> Loop Statement” and the required option.

Insert step loop statement

As always, the inserted step has to be defined. Any steps recorded or added after the loop will be within the block and to exit the block, the user has to explicitly specify that the new step is to be added outside the block by right-clicking on the loop statement and choosing the “Insert New step after block” option.

Let us add a ‘For’ statement as an example.

I chose the “For” from the menu option and this is what gets inserted into the keyword view:

QTP for statement

In the value column, I will set the iterator value. I set it as 1 to 3.  Next, I added a statement as “msgbox “x”. Let’s look at how this looks in the keyword view.

QTP message box

Conclusion

This concludes our session on the conditional and loop statements and see how they can be inserted from the keyword view. If or loop statements can be pretty much added just in the way a standard step is added. Just like a standard step, these steps also need to be defined.

Steps that are recorded or added after a block statement will be added to the block by default unless specified. When a statement has to be added outside the loop, we need to choose the option “Insert new step after block” from the right-click menu after choosing the block after which we desire the steps to follow.

=> Visit Here For The QTP Training Tutorials Series

In the next session, we will explore more options for the Keyword view such as moving and deleting steps, adding and deleting breakpoints.

Was this helpful?

Thanks for your feedback!

Recommended Reading

  • Working with Keyword View

    This is the tutorial #2 in our QTP Training article series.  => Click Here For The QTP Training Tutorials Series This article is going to give you an insight into the Keyword view of the QTP GUI. During this process, we are going to get a quick introduction to Actions, Object…

  • Adding Standard Steps From Keyword View

    This is the tutorial #3 in our QTP Training series.  => Click Here For The QTP Training Tutorials Series In the last tutorial, we saw how to work with the Keyword View. Today’s article is going to be about adding steps to a QTP test from the keyword view. Before we…

  • Writing Loop and Conditional Statements

    VB Scripting Basics - Writing Loop and Conditional Statements for Building the Programming Logic. => Click Here For The QTP Training Tutorials Series => Also read the complete free VBScripting Tutorial series here In the last VB Scripting article, we saw some of the basic features of the VB script. Here, we…

  • Kotlin Conditional Statements

    Learn about Kotlin Conditional Statements such as When, While, For Loop, If Else, etc with the help of programming examples: In this tutorial, we will learn about the control flow statements provided by Kotlin. These statements are used in the application to validate certain conditions and perform different tasks depending…

  • Unix Conditional Statements

    Conditional Statements Coding in Unix: Shell scripts often need to be constructed to execute different instructions depending on the value of specific control variables. The different paths of execution are specified using conditional instructions. In this tutorial, we will see about relational operators, and shell decision-making using various conditional statements. …

  • Expert View, Step Generator and Checkpoints

    This is the 12th tutorial in the QTP training series. This tutorial will explain to you the Expert View overview and Step generator along with a brief introduction to Checkpoints that will be carried out further until we cover all the details elaborately with examples on how to use these…

  • VBScript Loops: For Loop, Do Loop, and While Loop

    Introduction to Loops in VBScript: VBScript Tutorial #5 In my previous tutorial in this VBScript tutorial series, we learned about ‘Conditional Statements in the VBScript'. In this tutorial, I will discuss the different looping structures that are used in the VBScript. Loop is an important topic in VBScript, hence you…

  • LOOP CONSTRUCTS IN C++1

    An In-Depth Look At Loops In C++ Along With Its Types. In addition to Decision-making Constructs that we have seen in our last tutorial, there may arise some situations wherein we have to execute a block of statements repeatedly. Such a situation requires that we have a condition that checks…


12 thoughts on “Adding Conditional and Loop Statements in Keyword View – QTP Tutorial #4”

  1. I tried to insert Condition statements through
    keyword view but the “If-else” and “If – elseif ” options are not enabled .
    Could you please tel me why its though?

    Reply
  2. IN default record mode the application is running the statement is getting recorded in below statements ,

    Window(“HP MyFlight Sample Application”).Click 310,225
    Window(“HP MyFlight Sample Application”).Click 310,225
    Window(“HP MyFlight Sample Application”).Type “john”
    Window(“HP MyFlight Sample Application”).Click 284,287

    But i believe it should be in visual basic commands
    Dialog(“login).WinEdit(“Agent Name”). set “john”

    I am not able to figure it out . Can u please help me .

    Reply
  3. Hi Shrey,

    Perhaps you need to replace
    If Dialog(“Login”).WinEdit(“Password:”).Exist() Then
    with
    If Dialog(“Login”).WinEdit(“Password:”).Exist(1) Then

    Reply
  4. Hello guys,

    I want to know about how to fail or pass the test step in UFT. For an example
    If Dialog(“Login”).WinEdit(“Password:”).Exist(1) Then
    report.reportevent micpass “password field exist”
    else
    report .reportevent micfail “password field does not exist”
    endif

    Reply
  5. Great article. – The “Adding condition statements to the keyword view” cleared up an important query.

    In addition the “if block” information clarified another issue.

    Many thanks

    Mike

    Reply
  6. Hi Swati

    I have started reading the articles from this website from last couple of days.

    I have one question after reading all the articles. Will it be suffice? Will I be able to start automation testing after having knowledge giving in ur articles.

    Reply
  7. I tried following in QTP.

    Dialog(“Login”).Activate
    Dialog(“Login”).WinEdit(“Agent Name:”).Set “shrey”
    Dialog(“Login”).WinEdit(“Agent Name:”).Type micTab
    If Dialog(“Login”).WinEdit(“Password:”).Exist() Then
    Dialog(“Login”).WinEdit(“Password:”).SetSecure “527912f2104b508c840e503574e197a6437b9ad8”
    End If
    Dialog(“Login”).WinButton(“OK”).Click
    Window(“Flight Reservation”).Close

    But while running it’s showing following Run Error dialog box.

    Object doesn’t support this property or method: ‘Dialog(…).WinEdit(…).Exist’

    Line (5): “If Dialog(“Login”).WinEdit(“Password:”).Exist() Then”.

    Please guide.

    Reply
  8. OK !
    I have started reading this website from yesterday. It looks great for basic understanding.

    I am into testing field from last 2 and half years.

    My question is simple. Will this full tutorial help me in cracking job interview based on automation with QTP???

    Reply

Leave a Comment