You are on page 1of 8

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:

2) Loop Statements: Let‟s look at how these statements are represented. We will follow the exact method that we did for conditional statement. As you can see each statement type is given a different icon for identification. We will note how these statements are represented in the keyword view.The above is the different variations of the „if‟ statement that I enter ed in the expert view and as I said all the code does not serve any purpose except for our understanding. .

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. Here is how it looks: . Keyword view does provide a way for these statements to be added directly to the test. Click anywhere in the keyword view. just like how you would do it to add a standard statement. So far we have not added them directly from the keyword view. right click and choose “Insert->Step” followed by “Conditional Statement” and the statement that you would need. Here is how you can do it.

then. As an example. just the way we would do for a standard step..Then. Since I am checking if the password field exists or not.. Else are the statements that can be chosen based on the requirement of the tester. Operation: Again. I will choose „Exist‟ Value: The argument/arguments for the function is what this field is. This is what gets inserted. Keyword view Expert view: . Now. An If. So I choose “Password” from the list. 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. So choose an object that you wish to act on. I am going to check if the „Password‟ field exists.. we will need to define a conditional statement too. This feature again is no different from the standard step definition.As you can see above.then‟ and see what happens. Let us choose an „if. Elseif. Item: Click on the „item‟ column corresponding to the „if‟ statement and all the objects available will be displayed. a list of all the functions available will be displayed when this column is clicked on.

I am going to record the action of entering the password value.As you can see. we have to add code or statements to define what needs to be done if the condition becomes true. the recorded statement got directly added under the „if‟ statement. Now. The statement added or recorded could be a standard step or another conditional or loop statements (nesting). select the block (the „if‟ statement). For the sake of our example. 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. To do so. to define the „Then‟ part of our „if‟ statement.. To want to add outside the block.then‟ block. if the password box exists we will enter a value in the field. right click and choose „Insert New Step after block‟ . the „if‟ statement is defined. Take a look at the screen shots below. Any steps you add further will be added under to the „if. I will choose „Record‟ from the menu options and enter a password value in the „Flights‟ Login page. So.

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. Now. 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.The new step just comes as a sibling to the „if‟ block. .

Conclusion: So. I chose the “For” from the menu opti on and this is what gets inserted into the keyword view: In the value column I will set the iterator value. that concludes our session on the conditional and loop statements and how they can be inserted from the keyword view. Let‟s look at how this looks in the keyword view. Just like a standard step these steps also need to be defined. When a statement has to be added outside the loop we need to . Let us add a ‘For’ Statement as an example.As always. I set it as 1 to 3. Steps that are recorded or added after a block statement will be added within the block by default unless specified. 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. If or loop statements can be pretty much added just the way a standard step is added. Next I added a statement as “msgbox “x””.

.choose the option “Insert new step after block” option from the right click menu a fter choosing the block after which we desire the steps to follow.