Friday, 13 December 2013

Arrays in QTP


Whatis an Array:
We know very well that a variable is a container to store a value. Sometimes, developers are in a position to hold more than one value in a single variable at a time. When a series of values are stored in a single variable, then it is known as array variable.

Arrays are declared the same way a variable has been declared except that the declaration of an array variable uses parenthesis. In the below example, the size of the array is mentioned in the brackets.

'Method 1 : Using Dim
Dim arr1()       'Without Size

'Method 2 : Mentioning the Size
Dim arr2(5)  'Declared with size of 5

'Method 3 : using 'Array' Parameter
Dim arr3
arr3 = Array("america","india","itlay")
Although, the Array size is indicated as 5, it can hold 6 values as array index starts from ZERO.


VBScript Arrays can store any type of variable in an array. Hence, an array can store an integer, string or characters in a single array variable.

The values are assigned to the array by specifying array index value against each one of the values to be assigned. It can be a string.

Example :
<!DOCTYPE html>
<html>
<body>
<script language="vbscript" type="text/vbscript">

Dim arr(5)
arr(0) = "1"           'Number as String
arr(1) = "VBScript"    'String
arr(2) = 100                    'Number
arr(3) = 2.45                   'Decimal Number
arr(4) = #10/07/2013#  'Date
arr(5) = #12.45 PM#    'Time

document.write("Value stored in Array index 0 : " & arr(0) & "<br />")
document.write("Value stored in Array index 1 : " & arr(1) & "<br />")
document.write("Value stored in Array index 2 : " & arr(2) & "<br />")
document.write("Value stored in Array index 3 : " & arr(3) & "<br />")
document.write("Value stored in Array index 4 : " & arr(4) & "<br />")
document.write("Value stored in Array index 5 : " & arr(5) & "<br />")

</script>
</body>
</html>
When the above code is saved as .HTML and executed in Internet Explorer, it produces the following result:

Value stored in Array index 0 : 1
Value stored in Array index 1 : VBScript
Value stored in Array index 2 : 100
Value stored in Array index 3 : 2.45
Value stored in Array index 4 : 7/10/2013
Value stored in Array index 5 : 12:45:00 PM
Multi Dimension Arrays
Arrays are not just limited to single dimenstion and can have a maxinum of 60 dimensions. Two-dimension arrays are the most commonly used ones.

Example :
In the below example, a multi-dimension array is declared with 3 rows and 4 columns.

<!DOCTYPE html>
<html>
<body>
<script language="vbscript" type="text/vbscript">

Dim arr(2,3)    ' Which has 3 rows and 4 columns
arr(0,0) = "America"
arr(0,1) = "England"
arr(0,2) = "New Zealand"          
arr(0,3) = "India"
arr(1,0) = "Korea"          
arr(1,1) = "west indies"          
arr(1,2) = "Bangladesh"          
arr(1,3) = "srilanka"          
arr(2,0) = "Malaysia"            
arr(2,1) = "Australia"           
arr(2,2) = "Brittan"            
arr(2,3) = "Italy"           
        
document.write("Value in Array index 0,1 : " &  arr(0,1) & "<br />")
document.write("Value in Array index 2,2 : " &  arr(2,2) & "<br />")

</script>
</body>
</html>
When the above code is saved as .HTML and executed in Internet Explorer.
ReDim Statement is used to Declare dynamic-array variables and allocate or reallocate storage space.



Preserve- An Optional parameter used to preserve the data in an existing array when you change the size of the last dimension.

varname- A Required parameter, which denotes Name of the variable, which should follow the standard variable naming conventions.

subscripts- A Required parameter, which indicates the size of the array.

Example :
In the below example, an array has been redefined and then preserved the values when the existing size of the array is changed.

Note : Upon resizing an array smaller than it was originally, the data in the eliminated elements will be lost.

<!DOCTYPE html>
<html>
<body>
<script language="vbscript" type="text/vbscript">

  Dim a()
  i=0
  redim a(5)
  a(0)="XYZ"
  a(1)=41.25
  a(2)=22
 
  REDIM PRESERVE a(7)
  For i=3 to 7
  a(i)= i
  Next
 
  'to Fetch the output
  For i=0 to ubound(a)
    Msgbox a(i)
  Next
</script>
</body>
</html>
When we save the above script as HTML and execute it in Internet Explorer, it produces the following result.

XYZ
41.25
22
3
4
5
6
7
There are various inbuilt functions within VBScript which help the developers to handle arrays effectively. All the methods that are used in conjuction with arrays are listed below. Please click on the method name to know in detail.

Function         Description
LBound                A Function, which returns an integer that corresponds to the smallest subscript of the given arrays.
UBound          A Function, which returns an integer that corresponds to the Largest subscript of the given arrays.
Split                   A Function, which returns an array that contains a specified number of values. Splitted based on a Delimiter.
Join                    A Function, which returns a String that contains a specified number of substrings in an array. This is an exact opposite function of Split Method.
Filter                 A Function, which returns a zero based array that contains a subset of a string array based on specific filter criteria.
Is Array                         A Function, which returns a boolean value that indicates whether or not the input variable is an array.
Erase                 A Function, which recovers the allocated memory for the array variables.

Thursday, 12 December 2013

What is Checkpoint in QTP



Checkpoint is a method used to check the content before and after the step, i.e it is used to compare the expected value with the actual value displayed in the application, these checkpoints can be customized according to the requirements.
The checkpoint can be added while recording the application or it can be added after the recording is completed using Active screen (For adding Checkpoint after recording the Active screen must be enabled while recording)
Once the checkpoint is added, the QTP adds a checkpoint to the current row in keyword view and adds a check checkpoint statement to the expert view.


The standard checkpoint checks the object property values in an application or a webpage.
It checks objects like Radio buttons, combo boxes, images, Tables, Web page properties, links etc.
The Table checkpoint can be used to check all the values in the table, it can be created by inserting standard checkpoint on the table.
The page checkpoint can also created by inserting standard checkpoint on the object, the page check point can be used to check the time taken to load a webpage.

    Bitmap Checkpoint

The Bitmap checkpoint can be used to check an area of a webpage or an application as a bitmap. It checks that particular area by pixel to pixel comparison between actual and expected images.


The Image checkpoint is used to check the properties of an image in the application or a webpage like verifying the selected image source file is correct or not.
The difference between Bitmap checkpoint and Image checkpoint is in Bitmap Checkpoint we can check the images on pixels basis where in Image checkpoint it cannot be done.

    Text Checkpoint

The Text checkpoint is used to check the expected text from an application or a web page. It can be a specific region or a small portion of the text displayed.


The Accessibility checkpoint checks the areas of the web page that may not conform to the W3C (World Wide Web Consortium) Web Content Accessibility Guidelines.


The Database checkpoint is used to check the database accessed by the application.
This checkpoint creates a query during record time and the database values are stored as the expected values. The same query is executed during run time and the actual & expected values are compared.


The XML checkpoint checks the data content of XML documents in XML files or XML schema related to the webpage.

Wednesday, 11 December 2013

QTP- Synchronization



Recording Modes in QTP
There are 3 ways of recording a qtp script-
1. Normal mode is the default mode of recording. It recognizes the objects regardless of their location or position in a window.
2. Analog mode records exact mouse movements and keyboard operations. The recorded steps in this mode are not editable. The script is recorded in the form of a track.
3. Low Level records all parent level objects as window test objects and all other as WinObject test objects. This recording mode is useful when objects are not recognized by recording in normal mode. This mode captures all the mouse clicks with respect to locations on the screen.


Synchronization In QTP:-
There are 3 ways of synchronization in qtp-
1. Using Wait() command in the script.
2. Setting default time for each qtp statement in File->setting->web->browser navigation time out.
3. Using WaitProperty() command, you can implement this property by clicking on object to be synchronized and then selecting “Insert->synchronization point” while recording the script.
Explanation: - wait() command will stop qtp from executing next statement for the time specified in the command. Navigation time out will wait for specified time limit if object does not appears in the application and waitproperty() will allow the next statement to execute only if the specified object appears or maximum time limit elapse.


Checkpoints in QTP
There are 9 types of checkpoints in qtp-
1. Standard Checkpoint compares the object values (attributes) captured during recording to the object values produced during a run session.
2. Image Checkpoint allows the users to check the properties of an image in the application. An image checkpoint can be created by inserting standard checkpoints. Implemented using standard check point option.
3. Bitmap Checkpoint we can check particular area of an image or web page as a bit map. Where as in image check point we cannot do it.
4. Table Checkpoint we can compare the content of a table recorded with the content of the table produced during the application execution. Table checkpoint can be implemented using standard checkpoints.
5. Accessibility Checkpoint recognizes area of a webpage that may not conform to the World Wide Web consortium (w3c). E.g. checks if an image include ALT properties, required by w3c web content accessibility guide line.
6. Text Checkpoint checks that a text is displayed in the appropriate place in an application or web page.
7. Page Checkpoint checks features of a web page such as load time; whether a page contains broken links, number of images e.t.c. Implemented using standard check point option
8. Database Checkpoints checks the values stored in a database.
9. XML Checkpoints can checks the content of an XML data files or documents that are part of a web application. XML check point option is supported for all add in environments.

Reusable Check points e.g. create a reusable bitmap check point to verify logo on all the pages of a website.
Steps to create a reusable checkpoint-
a) Record a checkpoint (only checkpoint statement) and open ->resources->object repository, in the OR window open File->Export and replace local objects, and save as .tsr file.
b) Record another script where you want to reuse this checkpoint, right click on action under resource from left pane of page and select ‘associate repository with the action’, browse the reusable repository created in first step. Now, existing checkpoint option will be enabled for your use in your Acton.
Note: To implement an existing checkpoint on an object the parent window of the object should be open (recording mode). (I am not sure about this please confirm)


QTP – Parameters and Variables
Input and output parameters can be stored and called using 4 ways as shown below-
1.Action Variables - Steps to store and retrieve values
a) Open a new test and add one more action, Insert->Call to New Action..
b) Open keyword view, right click on action1 and select “Properties window”.
c) Open parameter tab and add two input parameters a, b and one output parameter c , all variable of type Number. Click ‘Ok’ to save.
d) Open ‘Action1’ in expert view and paste the following statements –
a1= parameter("a")
a2= parameter("b")
parameter("c") = a1+a2

e) Open ‘Action2’ in expert view and paste the following statements and run ‘Action2’ only-
runaction "action1", oneiteration, 2,2,c
msgbox c

2.Data Table Parameters - allows us to create a data driven test that runs several times using the data values from the data table.
Steps-
a) Record a script e.g. login screen of flight reservation sample application.
b) Open ‘keyword view’ and click on ‘Value’ next to the ‘Agent Name field’.
c) Click on <#> symbol, select parameter radio button and click ‘Ok’ button.
d) Open ‘view->Data table’, data table will be displayed with a column having value entered during the script recoding.
e) Add values in table for parameterized and run the script. It will iterate the script with each of the value entered in the table.
3.Environment Variable Parameters- are of two types built in and user defined. Built in variables contain information about your system such as OS name, version, action name, result dir e.t.c.
E.g. this statement will display the OS name and result dir name
a= environment(“OS”) & environment(“ResultDir”)
msgbox (a)
User defined Variables are of two types Internal and External.
User defined Internal variables are defined with in a test and are accessible only to the test in which they are defined.
Steps-
a) Open File->settings->and click on “Environment” tab
b) Select “User defined” from “Variable Type” drop down.
c) Click on “+” and add a new variable with value e.g. variable c with value 10
We can access this variable anywhere in the script e.g.
msgbox(environment(“c”)) will display the value stored in internal variable. Also we can edit .xml file to add new variables and values.
User defined External variables are generated as .xml files and can be accessed in all the scripts using import option.
Steps-
a) Open File->settings->and click on “Environment” tab
b) Select “User defined” from “Variable Type” drop down.
c) Click on “+” and add a new variable with value e.g. variable c with value 10
d) Click on export button and save it.
e) Open a new test and go to File->settings->and click on “Environment” tab.
f) Check “Load variable and values from external file” check box and browse the file saved in step d.
We can access this variable anywhere in the script e.g.
msgbox(environment(“c”)) will display the value stored in external variable.
4.Random Number Parameter inserts any random number within the specified limit.
Steps-
a) Open a new test window in keyword view mode.
b) Right click on ‘Action1’ and select ‘Action properties’.
c) Enter an input parameter in action properties window of type Number and click ‘Ok’ e.g. variable name “a”
d) Now right click on ‘Action1’ from left pane and select ‘’Action call Properties’.
e) Click on ‘Parameter values ‘tab, click on ‘value’ next to the parameter.
f) A <#> symbol will appear, clicking on it will open value configuration option window.
g) Select ‘Parameter’ option button, open dropdown list box and select ‘Random Number’.
h) Define a range in the subsequent window and click ‘Ok’ to save it.
Note: - we can also use a table and environment variable.
msgbox(parameter("a")) run to view value assigned to the variable.