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

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

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

Check out all the articles in this QTP training series @ Quick Test Professional (QTP) training series

[Note - Click on any image to enlarge]

Condition and Loop Statements in Keyword view

A quick recap of what we 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 with 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 on 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 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.

Firstly, what do 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 particular number or times. Another variation to 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…’

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. For convenience and since the steps that I am about to insert are dummy, I am going to put the code directly into Expert view and we observe it in keyword view.

1) Conditional statements:

QTP Conditional statements

The above is the different variations of the ‘if’ statement that I entered in 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 conditional statement.

QTP Loop Statement

Adding condition statements to the keyword view:

For my examples above 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. 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 “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 requirement 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 the 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. This feature again is no 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 what it looks like in 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 are added any steps that the user adds or records will be directly added under them unless explicitly specified. The statement added or recorded could be a standard step or another conditional or loop statements (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 screen shots below, 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 to the ‘if..then’ block.

To 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 inserted and choosing “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 be explicitly specify that the new step is to be added outside the block by right clicking on the loop statement and choosing “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:

So, that concludes our session on the conditional and loop statements and how they can be inserted from the keyword view. If or loop statements can be pretty much added just 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 within 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” option from the right click menu after choosing the block after which we desire the steps to follow.

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



The Best Software Testing Training You'll Ever Get!

software testing QA training

9 comments ↓

#1 Smriti on 03.07.13 at 6:22 am

good one.. simple explanation for loop and condition statements.
thanks

#2 Sachin on 03.07.13 at 2:31 pm

Nice and simple overview…. good for beginners….

#3 gurusankar on 03.08.13 at 4:45 am

I didn’t receive the mail for the QTP Tutorial 4

#4 Senthil on 03.08.13 at 6:56 pm

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?

#5 Mike Brooks on 03.16.13 at 6:50 pm

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

#6 Shrey on 11.05.13 at 3:13 pm

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???

#7 Shrey on 11.05.13 at 3:55 pm

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.

#8 Hanyu on 12.13.13 at 1:14 am

Hi Shrey,

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

#9 Shivam on 12.17.13 at 2:25 pm

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.