Micro Focus QTP (UFT) Forums
URL Variables - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: UFT / QTP Beginners (https://www.learnqtp.com/forums/Forum-UFT-QTP-Beginners)
+--- Thread: URL Variables (/Thread-URL-Variables)



URL Variables - kevwhite79 - 03-02-2011

Hi,
I'm very new to QTP so please help. I have created a number of QTP scripts and uploaded them to HPQC, connectivity is established and scripts can be played back ok.
However, my intention is to run these scripts on multiple test environments now (same screens just log in via different URLs). So I wanted to link to an external (XML?) file that contains the URL information so that changing the URL in this file would point the scripts to the desired test environment. I'm not finding this easy to do. Can you give me some advice please on setting up the first few steps of the scripts to check the external file for the URL to use when IE loads. I have tried creating and using a User Defined Environment Variable without success :-( Thanks in advance for any help.
PS If username and password can be stored in the file also that would be great.


RE: URL Variables - tdevick - 03-02-2011

create an XML environment file in this format, and store it in where it can always be found on disk by your tests. You can have as many "<Variable>" tags as you want in your XML file. Each "<Variable>" tag must contain a "<Name>" and a "<Value>" sub-tags.

The file must start with "<Environment>" and end with "</Environment>". Within that context, you can define whatever variables you want. In the example below, if the URL of your test server changes, you change the PRODUCT_URL tag's value.

Code:
<Environment>

    <Variable>
        <Name>LOGIN_ID</Name>
        <Value>automation@gmail.com</Value>
    </Variable>         

    <Variable>
        <Name>LOGIN_PASSWORD</Name>
        <Value>yourpassword</Value>
    </Variable>

    <Variable>
        <Name>PRODUCT_URL</Name>
        <Value>http://www.myproductserver1.com</Value>
    </Variable>

</Environment>
When your script is ready to load and set these variable values, do this in your code:

Code:
'get the currently loaded external file name; blank if not file loaded
fileName=Environment.ExternalFileName
If (fileName="") Then
   Environment.LoadFromFile(" "C:\Automation XML Files\Automation Environment Variables.xml")
End If

We have been keeping our xml file in the QC repository under "Resources", where function libraries are stored. I created a sub-directory there named "xml files" and store our xml file there. That way it is under source control so you can see the change history and you can revert to a previous version if you want.

doing that required a little more work:

Code:
fileName=Environment.ExternalFileName
If (fileName="") Then
    executeFile("[QualityCenter\Resources] Resources\Function Libraries\Quality Center Functions.qfl")
    fileName=downloadEnvironmentVariablesFile("Environment Variable XML files", "C:\Automation XML Files",  "Automation Environment Variables.xml")
    Environment.LoadFromFile(fileName)
End If

"downloadEnvironmentVariablesFile" is a function we wrote and is defined in function library file "Quality Center Functions.qfl":

Code:
Option Explicit

Public Function downloadEnvironmentVariablesFile(ByVal ResourceFileDirectory, ByVal  hostDownloadDirectory,  ByVal EnviromentVariablesFileName)
   Dim index, filename,itemCount, itemName,found,folderList,folderItemCount,folderItemName,itemList,AutomationEnvironmentVariablesXML, itemID,itemType,itemData, AutomationResourceFile, _
             downloadPath

    downloadEnvironmentVariablesFile=""
    set folderList=QCUtil.QCConnection.QCResourceFolderFactory.root.QCResourceFolderFactory.newlist("")
       folderItemCount=folderList.count
    folderItemName=folderList.item(1).name

    found=false
    ' find all folders under the root of the Resources section of QC
    ' search for the folder that contains the Automation environment xml file
    For index =1 to folderItemCount
            itemName=folderList.item(index).name
            If itemName=ResourceFileDirectory Then
                found=true
                exit for
            End If
    Next

    If found Then
        set itemList=folderList.item(index).QCResourceFactory.newlist("")
        itemCount=itemList.count
        found=false
        For index=1 to itemCount
            If itemList(index).name=EnviromentVariablesFileName Then
                found=true
                exit for
            End If
        Next
    
        set AutomationResourceFile=itemList(index)        
        itemName=AutomationResourceFile.name
        itemType=AutomationResourceFile.type
        itemData=AutomationResourceFile.path
        filename=AutomationResourceFile.FileName
'         so far, name , id ,type, and path all work OK
        downloadPath=AutomationResourceFile.DownloadResource(hostDownloadDirectory,True)
        downloadEnvironmentVariablesFile=downloadPath &"\" &  filename
    End If

End Function

Hope this helps.


RE: URL Variables - kevwhite79 - 03-03-2011

Thank you very much. I will try this tomorrow.