Pages

Thursday 11 July 2013

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


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.

No comments: