Wednesday, 26 October 2011

Working With Files : QTP


Computer file System
In computing , a file system is a method for storing and organizing computer files and the data they contain to make it easy to find and access them.File systems may use a data storage device such as a hard disk or CD-ROM.

Working with and Folders
  •  Creating a Folder:
Option explicit
Dim Objfso,objFolder,strDirectory
strDirectory = "C:\bibhas"   
Set Objfso = CreateObject("Scripting.FileSystemObject")
Set objFolder = Objfso.CreateFolder(strDirectory)
  •  Deleting a Folder :
Set objFolder = CreateObject ("Scripting.FileSystemObject")
objFolder.DeleteFolder("E:\NewFolder") ' This is folder which is already exist in the system
  • Copying a Folder :
Set objFolder = CreateObject ("Scripting.FileSystemObject")
objFolder.CopyFolder "C:\NewFolder","D:\NewFolder1" , True
  • Creating a Folder , After verifying it's present or not :
Option explicit
Dim Objfso,objFolder,strDirectory
strDirectory = "C:\bibhas"   
Set Objfso = CreateObject("Scripting.FileSystemObject")
If Objfso.FolderExists(strDirectory) Then
Set objFolder = Objfso.GetFolder(strDirectory)
msgbox strDirectory & " already Exist !!!"
else
Set objFolder = Objfso.CreateFolder(strDirectory)
end if
  •  Returning a Collection Of Disk Drives available in the System:
Set dFso = CreateObject ( "Scripting.FileSystemObject")
Set collectionDrives = dFso.Drives
For Each oDrive in collectionDrives
msgbox "Drive letter : " & oDrive.DriveLetter
Next
  •  Find Out the Available Space on a Disk Drive :
Set objFso = CreateObject ("Scripting.FileSystemObject")
Set oDrive = objFso.GetDrive("C:")
msgbox "Available Space " &  oDrive.AvailableSpace

Monday, 24 October 2011

Descriptive Programming Examples In QTP

Scenario : Let us script a scenario, where object properties changed 

Dim sLinkDescription
Dim sLinks
Dim lnkCount

Set sLinkDescription=Description.Create
sLinkDescription("name").value="signin"

set sLinks=browser("micclass:=Browser").page("micclass:=Page").ChildObjects(sLinkDescription)

if sLinks.count <>0 then

sLinks(0).click

End If

Sunday, 23 October 2011

Descriptive Programming in QTP

What is Descriptive Programming?
Providing Objects information directly into the Test Script, instead of storing objects information in the Object Repository.
Descriptive Programming is a method of performing operation on the object which is not there in Object Repository. We can also use programmatic descriptions to perform the same operation on several objects with certain identical properties, or to perform an operation on an object whose properties match a description that we determine dynamically during the run session.
Tests using Descriptive programming are faster than Tests using Object Repository .

Benefits of Descriptive Programming

If the AUT (Application Under Test) is having Dynamic Objects, then it is better to use descriptive programming. Incase of object repository, it is Difficult to handle dynamic Objects.


When we have more objects to perform operations, in performance point of view descriptive programming is better than object repository method. If we use object repository for generating huge tests, then QTP performance will be decreased , as size of the object repository will increases.
          
Descriptive Programming is useful if the AUT is having objects that are adding in the Run Time. We can’t add Objects to Object Repository in run time.
          
If we need to start Test Automation process before the AUT is ready , then Descriptive programming will be very useful.   


Descriptive Programming is useful, if the AUT is having similar type of objects or similar name objects. Incase of Object Repository, It will create multiple objects with same description unnecessarily.

Shared Object Repository is not changeable by multiple persons at a time.

Maintenance becomes harder if all the team members have created their own object repositories. In this situation Descriptive Programming is very flexible.

 
Tests using Descriptive Programming can be executed in any version of QTP without any changes.

Code Portability: Just code is enough to run Tests. We can copy and paste in other scripts for any other required requirement.

Reusing of Properties: We can assign properties to a global variable and reuse it for same type of objects.

Plug & Play: Any time Tests will be in ready to run state. No need to worry about any other settings or files.

Just Maintenance of variables: We can store the object properties in the form of variables in a txt / vbs file which will be placed in central location. In this case we just need to maintain that file.


Types of Programmatic Descriptions (Descriptive Programming)


1) Static: - We enter the set of properties and values that describe the object directly in a VBScript statement.
a) Direct specification of description in script

Browser(“micclass:=Browser”).Page(micclass:=page”).Link(“name:= link”).Click

b) Assigning description to the variables and use that variables in script
g_MainBrowser    =“micclass:=Browser”
g_MainPage         =“micclass:=Page”
g_Lnk_Login        =“name:=Login”

Browser(g_MainBrowser).Page(g_MainPage).Link(g_Lnk_Login).Click

2. Dynamic: - We add a collection of properties and values to a Description object, and then enter the Description object name in the statement.

            Set oBrowser = Description.create
            oBrowser (“micclass”).value=”Browser”
            oBrowser (“name”).value= “Google”

            Set oPage = Description.create
            oPage (“micclass”).value=”Page”
            oPage (“name”).value= “Google”

            Set oLink = Description.create
            oLink (“name”).value= “Login”
            oLink (“index”).value= 1

Browser(oBrowser).Page(oPage).Link(oLink).click

Using the Static type to enter programmatic descriptions directly into your statements may be easier for basic object description needs. However, in most cases, using the Dynamic type provides more power, efficiency, and flexibility.

Handling Runtime added objects / Dynamic Objects / Same Description Objects

Dynamic objects               : Property values are getting changed regularly.
Same Description Objects  : Property values are same for multiple objects.
Runtime Added Object       : Object is not there in recording time but it might be add in runtime.

Eg: Always the job links in timesjob and Monster sites will get update. 
Child object method is very useful to get control on Runtime added objects or dynamic objects or same description objects which are there application.
Ex1: If there are multiple signin links on the page, using child objects method we can click on any signin link. 

Friday, 21 October 2011

Dictionary Object In QTP

Dictionary Object  stores data key, item pairs. A Dictionary object stores the items in the array. Each item is associated with a unique key. The key is used to retrieve an individual item and is usually an integer or a string, but can be anything except an array.
Methods:

Add Method
Adds a key and item pair to a Dictionary
object. object.Add (key, item)
Arguments 
object Required. Always the name of a Dictionary object.
key Required. The key associated with the item being added.item Required.
The item associated with the key being added. 
Remarks
An error occurs if the key already exists.
The following example illustrates the use of the Add method.
Dim var1   ‘ Create a variable. 
Set var1 = CreateObject(“Scripting.Dictionary”) 
 var1.Add “a”, “value1”   ‘ Add some keys and items.  
var1.Add “b”, “value2”
Items Method
 Returns an array containing all the items in a Dictionary object. 
      object.Items( )
 Remarks : The object is always the name of a Dictionary object.The following code illustrates use of theItems method:
Set var1 = CreateObject(“Scripting.Dictionary”)   
 var1.Add “value1”, “NewValue1”   ‘ Add some keys and items.  
 var1.Add “value2”, “NewValue2”    
value1= var1.Items   ‘ Get the items.  
  For i = 0 To var1.Count -1 ‘ Iterate the array.
       stmt = stmt & value1 (i) & “<BR> ‘ Create return string.  
      Next
Msgbox stmt  
 Exists Method
 Returns true if a specified key exists in the Dictionary object, false if it does not.
object.Exists(key)
 Arguments
object Required. Always the name of a Dictionary object.
key Required. Key value being searched for in the Dictionary object.
 Remarks :
The following example illustrates the use of the Exists method.
Set var1 = CreateObject(“Scripting.Dictionary”)  
var1.Add “value1”, “Newvalue1”   ‘ Add some   keys and items.   
var1.Add “value2”, “Newvalue2”   
   If var1.Exists(“value3”) Then   
    Msgbox  “Specified key exists.”   
 Else      
 Msgbox  “Specified key doesn’t exist.” 
   End If
Keys Method
Returns an array containing all existing keys in a Dictionary object.
 object.Keys( )
Remarks
The object is always the name of a Dictionary object.
The following code illustrates use of the Keys method:   
Dim var1,var2,var3   ‘ Create some variables.   
 Set var2= CreateObject(“Scripting.Dictionary”)   
 var2.Add “var1”, “Newvalue1”   ‘ Add some keys and items.  
  var2.Add “var3”, “Newvalue2”   
  var1 = var2.Keys   ‘ Get the keys. 
   For i = 0 To d.Count -1 ‘ Iterate the array.    
   stmt = stmt & var1(i) & “<BR>‘ Return results.   
  Next
Msgbox stmt
Remove Method  
Removes a key, item pair from a Dictionary object. 
object.Remove(key) 
Arguments
 object Required. Always the name of a Dictionary object.
key Required. Key associated with the key, item pair you want to remove from the Dictionary object.
Remarks
An error occurs if the specified key, item pair does not exist.
The following code illustrates use of the Remove method:
Dim var1, var2   ‘ Create some variables. 
Set var2 = CreateObject(“Scripting.Dictionary”) 
var2.Add “var1”, “value1”   ‘ Add some keys and items. 
var2.Add “var3”, “value2”
 var2.Add “var4”, “value3” … 
var2.Remove(“var3”)   ‘ Remove second pair.
RemoveAll Method
The RemoveAll method removes all key, item pairs from a Dictionary object.
object.RemoveAll( ) 
Dim var2,var1,var2   ‘ Create some variables.
 Set var1 = CreateObject(“Scripting.Dictionary”) 
var1.Add “v1”, “value1”   ‘ Add some keys and items.
 var1.Add “v2”, “value2” 
var1.Add “v3”, “value3” … 
var2 = var1.RemoveAll   ‘ Clear the dictionary.

Advantages of using it in QTP:

1. can be used as Global variable declaration. so that any test can access the values from it in the run time.
2. You can store and retrieve any number of run time values in to dictonary.
3. It is one of the Parameterization technique we can use in QTP
Disadvantages:
we can not specify the values in the design time like Data table , Action parameters, environment variable.
So it is useful only in Run time , not design time