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.
What You Will Learn:
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
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.
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.
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:
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.
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.
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.
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’.
The new step just comes as a sibling to the ‘if’ block.
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.
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:
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.
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.
In the next session, we will explore more options for the Keyword view such as moving and deleting steps, adding and deleting breakpoints.