Tuesday, July 19, 2011

VB Script:QTP




As we all are aware that QTP supports VB script.
VB script: Basic
A variable is a virtual container in the computer's memory where we can store
Program information that may change during the time of our script is running.
In VBScript, variables are always of one fundamental data type, Variant.
A computer program can store information in a variable and then access that
Information later by referring to the variable's name.
Dim Statement
The Dim statement declares and allocates storage space in memory for
Variables. If the Dim statement is used at the start of a procedure then the variable is local to the procedure, if  it is used at start of a global script block the Dim will available throughout the module.
Syntax: Dim varname[([subscripts])][, varname[([subscripts])]]

Option Explicit Statement
Forces explicit declaration of all variables in a script.
If used, the Option Explicit statement must appear in a script before any other
statements. When you use the Option Explicit statement, you must explicitly
declare all variables using the Dim, Private, Public, or ReDim statements. If you
attempt to use an undeclared variable name, an error pop will appear displaying the message as “variable is undefined”

Arrays:
It is a variable which contains a series of variables value. Array variables and scalar variables are declared in the same way, except that the declaration of an array variable uses parentheses ( ) following the variable name. Arrays are useful when we are storing sets of similar data.

Constants 
A constant contains a value that never changes. For example, the value of PI is a constant. Use constants to define values that you know will never change during the execution of your script.VBScript also allows you to define your own constants.


Descriptive programming in QTP
Whenever QTP records any action on any object of an application, it adds some description on how to recognize that object to a repository of objects called object repository. QTP cannot take action on an object until unless its object description is in the Object Repository. But descriptive programming provides a way to perform action on objects which are not in Object repository.
Why to use Descriptive programming?
 Below are some of the situations when Descriptive Programming can be considered useful:
The objects in the application are dynamic in nature and need special handling to identify the object. The best example would be of clicking a link which changes according to the user of the application, Ex. “Logout <<UserName>>”.
When object repository is getting huge due to the no. of objects being added. If the size of Object repository increases too much then it decreases the performance of QTP while recognizing a object.
When you don’t want to use object repository at all. Well the first question would be why not Object repository? Consider the following scenario which would help understand why not Object repository
Scenario 1: Suppose we have a web application that has not been developed yet. Now QTP for recording the script and adding the objects to repository needs the application to be up, that would mean waiting for the application to be deployed before we can start of with making QTP scripts. But if we know the descriptions of the objects that will be created then we can still start off with the script writing for testing
Scenario 2: Suppose an application has 3 navigation buttons on each and every page. Let the buttons be “Cancel”, “Back” and “Next”. Now recording action on these buttons would add 3 objects per page in the repository. For a 10 page flow this would mean 30 objects which could have been represented just by using 3 objects. So instead of adding these 30 objects to the repository we can just write 3 descriptions for the object and use it on any page.Modification to a test case is needed but the Object repository for the same is Read only or in shared mode i.e. changes may affect other scripts as well.
When you want to take action on similar type of object i.e. suppose we have 20 textboxes on the page and there names are in the form txt_1, txt_2, txt_3 and so on. Now adding all 20 the Object repository would not be a good programming approach.
How to use Descriptive programming:
There are two ways in which descriptive programming can be used
By creating properties collection object for the description.
By giving the description in form of the string arguments.By creating properties collection object for the description.To use this method you need first to create an empty description
Control Structures
Control structures allow you to control the flow of execution of your scripts. You can specify that some code should be executed only under certain circumstances, using conditional structures. You can specify that some code should be executed repeatedly, using looping structures. Lastly, you can specify that code from somewhere else in the script should be executed using branching structures.
Conditional Structures  
A)    If...Then...Else:
It allows you to choose which block of code to execute based on a condition or series of conditions.
If condition1 Then
  code block 1
ElseIf condition2 Then
  code block 2
Else
  code block 3
End If
If condition1 is True, code block 1 is executed. If it is False, and condition2 is True, code block 2 is executed. If condition1 and condition2 are False, code block 3 executes.
An If...Then construct may have zero or more ElseIf statements, and zero or one Else statements.
You can use NOT to reverse the logic of the IF structure. (i.e. If Not...Then...Else)
B)    Select Case:
It can be used instead of using some really complex If...Then constructs.
Select Case variable
Case chioce1
  code block 1
Case choice2
  code block 2
...
Case choiceN
  code block N
Case Else
  default code block
End Select
This compares the value of variable with choice1, choice2, and so on. If it finds a match, it executes the code block associated with that choice. If it does not find a match, it executes the default code block.
 Looping Structures
Looping structures allow you to execute the same block of code repeatedly. The number of times it executes may be fixed or may be based on one or more conditions. Be careful not to create an endless loop; that is one where the condition required to break the loop is never met.
 
A)    For...Next :
The looping structures take the following form:
For counter = start to s
  code block
Next
code block is executed with counter having the value start, then again with counter having the value start+1, then start+2, and so on until it has the value s. It then leaves the loop.
Optionally, you may specify a different value to increment counter by using this construct:
For counter = start to s Step stepvalue
  code block
Next
counter will take the values start+stepvalue, for example if stepvalue is 2 counter will count by 2s. If stepvalue is negative, s should be less than start.
B)    For Each...Next :
The loop use the following form:
For Each item In set
  code block
Next
code block is executed with item taking the value of each member of set. set should be an array or a collection.


C)    Do...While looping structure:
Do While booleanValue
  codeblock
Loop
code block is executed as long as booleanValue is True. If it is False to begin with, the loop is not executed at all.
D)    While...Wend looping structures have the following form :
while booleanValue
  code block
Wend
code block is executed as long as booleanValue is True. If it is False to begin with, the loop is not executed at all. Note: Wend is not supported in ASP.NET; use End While instead.
E)    Do...Loop While looping structure:
Do
  code block
Loop While booleanValue
code block is executed as long as booleanValue is True. The loop is executed at least once no matter what.
F)     Do Until...Loop looks like this:
Do Until booleanValue
  code block
Loop
code block is executed as long as booleanValue is False. If it is True to begin with, the loop is not executed at all.
G)    Do...Loop Until uses the following form:
Do
  code block
Loop Until booleanValue
code block is executed as long as booleanValue is False. The loop is executed at least once no matter what.

 Branching Structures:
  Branching structures allow you to jump from one position in the code to another.  
Subroutines:
It  do not return a value. They simply execute.
Sub name (argumentlist)
  code block
End Sub
Functions:
It return a value and have the following form:
Function name (argumentlist)
  code block
name = expression

End Function

QuickTest Professional Unplugged

No comments:

Post a Comment