Thursday, August 29, 2013

Add Remove Object Repository Dynamically in QTP

HP QTP provides a great functionality for adding or removing Object Repository at run time or dynamically. For that ObjectRepositories Utility object of QTP is used. 

Find the below code for Add/Remove Object Repository dynamically in QTP during run time.

'*********************************************************
'Function Name: AddRemoveOR
'Purpose: Use for Add Or Remove Object Repository with Action Name
'Inputs:
    'repositoryPath=Repository Path for Add or Remove with Action
    'actionName=Action Name for Add for Add Or Remove Repository
    ' operationName=Operation Name to be Perform either "Add" or "Remove" Object  Repository
'Return Values: -
'Created By: Krutin Gandhi
'*********************************************************

Function AddRemoveOR(repositoryPath,actionName,operationName)  

    Dim qtApp,qtRepositories, actName, RepPath,  rPosition'Variable Declaration

    RepPath=repositoryPath 'TRS File Path
    actName=actionName   'Get Action Name
    Set qtApp = CreateObject("QuickTest.Application")    ' Create Application Object
    Set qtRepositories = qtApp.Test.Actions(actName).ObjectRepositories    ' Get Associated repositories list

        'Adding Repository to an action
        If operationName="Add" Then       
            If qtRepositories.Find(RepPath) = -1 Then
                qtRepositories.Add RepPath, 1    ' Add the Object Repository to the current action 
            End If

        'Remove Repository to an action
        Else if operationName="Remove" Then
            rPosition=qtRepositories.Find(RepPath) 'Find the Position of the Repository
            If  rPosition<>-1 then
                qtRepositories.Remove rPosition  ' Remove Repository From the Action
            End if           
        End If
    End If

    'Assign nothing
    Set qtApp= Nothing
    Set qtRepositories= Nothing
End Function