Facebook

Course Name Start Date Time Duration Registration Link
No Training Programs Scheduled ClickHere to Contact
Please mail To sudhakar@qtpsudhakar.com to Register for any training

Sunday, December 5, 2010

Relative and Absolute Paths in QTP

Every Test Automaton Project follows a specific framework to develop scripts. Every framework has a folder structure to maintain scripts and resources. A script can have lot of resources associated with it. We may execute our scripts in different machines.  We may copy our scripts to the server for backup. In such cases we need to ensure that script doesn’t fail because of path change. So how can we make portability of the scripts?

The answer is Relative Path.

We can give two types of paths when associating a resource.

Absolute Path: A complete path to a specific file starting from a fixed location. An absolute path always points to the specified file, regardless of the current directory.

Ex: To associate “D:\Demo Framework\Libraries\Global\WebControls.vbs” file to script1 we can use the same path to specify Absolute Path.

Relative Path: A Relative Path is relative to where that file is located.

Ex: To associate “D:\Demo Framework\Libraries\Global\WebControls.vbs” file to script1 we can use ..\..\Libraries\Global\WebControls.vbs path to specify Relative Path.

The above example explained by considering below sample framework folder structure.


How to specify Relative Path?
A simple logic needs to be followed in specifying Relative Path. If you want to go forward use folder name, to go back use “..\”.

In the above folder structure to associate “D:\Demo Framework\Libraries\Global\WebControls.vbs” to Script1 which is there inD:\Demo Framework\Scripts” we have used ..\..\Libraries\Global\WebControls.vbs”.
To understand this in simple steps
Open the script1 folder D:\Demo Framework\Scripts\Script1. Now, from here how do you go to “D:\Demo Framework\Libraries\Global\WebControls.vbs”.
Step1: Click on Back (Now you’re there at D:\Demo Framework\Scripts\)
Step2: Click on Back (Now you’re there at D:\Demo Framework\)
Step3: From here go to Libraries folder (Now you’re there at D:\Demo Framework\Libraries)
Step4: Go to Global folder (Now you’re there at D:\Demo Framework\Libraries\Global)

As I said for going back we have to use “..\”, two times we have used back, so “..\..\” should be used. To go forward we have to use folder name.

Now the relative path becomes “..\..\Libraries\Global\WebControls.vbs”

We can use Relative Paths for
  • Associated Object Repositories
  • Associated Library Files
  • External Actions
  • Associated Recovery Scenarios
  • Associated Recovery Scenario Function Libraries
  • Search Folders

10 comments :

  1. Hi Sudhakar,
    Shailu here.
    do we have a function to convert relative path to absolute path at runtime?

    Shailender

    ReplyDelete
  2. Yes. we can convert relative path to absolute path. But we cannot set that absolute path value in resources. Read below post to see how to convert a relative path to absolute path.
    http://www.qtpsudhakar.com/2010/12/get-absolute-path-from-relative-path.html

    ReplyDelete
  3. Hi Sudhakar,

    I am able to locate text file using relative path but not able to locate excel path residing at the same location where text file is located.
    Both the files - "new.txt" and "new.xlsx" are stored under "C:\" and QTP script is stored under "C:\QTP\TestScript".

    I am using following code:
    Set objFSO=CreateObject("Scripting.FileSystemObject")
    Set txtFile = objFSO.OpenTextFile("../../new.txt")----this is working fine

    Set ControlExcelApp = CreateObject("Excel.Application")
    Set ControlWB = ControlExcelApp.WorkBooks.Open("../../new.xlsx")----this is throwing error as file could not be found.

    Can you please guide me on this

    Thanks,
    Nirav

    ReplyDelete
  4. hi nirav
    your script is right but you should have to save the xl file in old format(.xls) and try it will work.

    ReplyDelete
  5. Hi Sudhakar,
    Both my VBS and Excel files are in C drive but I am not able to open Excel file after double clicking the VBS file when using relative path.
    The files I am using are:
    C:\sampleScript.vbs
    C:\inputData.xls

    But I am able to open the excel files when placing the both files either on desktop or in the My Documents.
    Please help

    ReplyDelete
  6. Hi Sudhakar,

    I am using UFT 11.5.
    I am trying to use relative paths for associating the libraries. The path for library si "C:\QTP\Solutions\FunctionLibrary\Page_Functions\Page_Functions.qfl" for which I am using relative path as "..\QTP\Solutions\FunctionLibrary\Page_Functions\Page_Functions.qfl". Still it is giving an error as Missing Function Library: Page Functions.

    Please help me in resolving this. Please do let me know what has gone wrong in the above statements or any other setting is required.

    Thanks,
    Sheetal

    ReplyDelete
    Replies
    1. You didn't specify your test path. So that I can say where the problem is

      Delete
  7. Hi,

    The test path is "C:\QTP\Solutions\CommVoice\QTPTestScripts\Sol_Comm_AG02_DPM_BS_SIP_01"

    ReplyDelete
    Replies
    1. ..\..\..\FunctionLibrary\Page_Functions\Page_Functions.qfl

      Use it.

      Delete