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

Sample Questions in QTP

Sample Questions:


1. What does ChildObject Method Return
a. A Collection Object
b. A string true/false
c. A Boolean True/False
d. The Number of objects matching the child object descriptions
Ans: a

2. How can you make a test bypass the Object Repository during Test Run.
a. Turn the Object Repository Off
b. Use a programmatic Description
c. Delete all objects in all repositories
d. Add the Object in Object Repository Manager
Ans: b

3. When a test is run in Update mode, what options can you select? Select Three
a. the datatable
b. Check point properties
c. Test Object Description
d. Action Names in the test
e. Logical Names in the test
f. Active Screen Images and Values
Ans.C
4. What Object is used to read information from text file
a. Read
b. ReadLine
c. TextStream
d. FileStream
e. OpenTextFile
Ans.b

5. What can you use to handle unpredictable exceptions
a. A Do Loop
b. Recovery Scenario
c. If Then Else
d. Select Case Statemtn
Ans: b

6. You have just stopped recording steps into your tests. Your application under step is still up and visible on your screen. You now wish to add checkpoints into your test using the menu command, insert > Checkpoint from the toolbar. Which checkboxes are available to you? Select two
a. Text checkpoint
b. XML checkpoint
c. Bitmap checkpoint 
d. TextArea checkpoint
Ans:b
7. Browser Navigation Timeout is in which tab of Test Settings
a. Properties
b. Resources
c. Web
d. Web Settings
Ans: c

8. How many tabs are present in Test Settings (File > Settings) window
a. 7
b. 6
c. 5
d. 8
Ans: a

9. Identify the Tabs in the Test Settings (File > Settings) Window
a. Properties, Run, Resources, Parameters, Environment, Web, Recovery
b. Properties, Run, Resources, Parameters, Environment, Web Settings, Recovery
c. Properties, Run, Options, Parameters, Environment, Web, Recovery
d. Properties, Run, Resources, Input Parameters, Environment, Web, Recovery
Ans: a

10.'Generate Script' is in which tab of Test Settings (File > Settings) window
a. Properties
b. Web
c. Resources
d. Recovery
Ans: a

11. The following are the main columns in the Keyword view
a. Item, Operation, Value, Comments
b. Item, Operation, Value, Documentation
c. Item, Operation, Property, Documentation
d. Number, Operation, Value, Documentation
Ans: b

12. For each object and method in an Expert view statement, a corresponding row exists in the Keyword view
a. True
b. False
c. There is a problem with the statement
d. None of the above
Ans: a

13. You can work with one or several function libraries at the same time
a. True
b. False
Ans: a

14. You can insert additional steps on the test objects captured in the Active Screen after the recording session
a. True
b. False
Ans: a


15. Active screen enables you to parameterize object values and insert checkpoints
a. True
b. False
Ans: a

16. A QTP user can increase or decrease the active screen information saved with the test.
a. True
b. False
Ans: a

17. The information pane provides a list of ... in the test.
a. Semantic error
b. Syntax error
c. Common errors
d. Logical errors
Ans: b

18. When we switch from Expert view to Keyword view, QTP automatically checks for syntax errors and shows them in the information pane.
a. True
b. False
Ans: a

19. If the information pane is not open, QTP automatically opens it incase a syntax error is detected
a. True
b. False
Ans: a