How to use QTP to save and retrieve a Quality Center Test Resource

Overview

Sometimes in your QTP script you need to save and retrieve data stored in a text file across multiple machines. Quality Center has a ‘resource’ module where you can store a resource like a text file. I use this functionality so much that  I created two QuickTest Professional functions specifically for this:

  • QcSaveToResource – This function will save a text file from a local machine to an existing resource in QC
  • QCGetResource – This function will copy a file stored in QC to a local drive.

 ( Also check out my video : How to get a QC resource using QTP with OTA)

How to save a text file to QC (QcSaveToResource)

The following code will save a text file from a QuickTest Pro lab machine to an existing ‘Test Resource’ in QC’s Test Resource module:

QcSaveToResource "Joe","qcresourcetest.txt","C:\QTPAUTOMATION\DATA","",""

How to get a Quality Center test resource and save to a local dir (QCGetResource)

QCGetResource "qcresourcetest.txt","C:\Temp"

QTP QC function that you can use!

'------------------------------------------------------------
'
'@Function Name: QCSaveToResource

'@Documentation Saves a text file to the QC Test Resources module

'@Created By: Joe Colantonio

'@Return Values: NONE

'@Example: QcSaveToResource "Joe","qcresourcetest.txt","C:\QTPAUTOMATION\DATA","",""

'-----------------------------------------------------------
Function QCSaveToResource(QcResourceName,fileNameToSave,fileToSavePath,resourceType,opt)
'**************************

'Upload a resource

'**************************

Set qcConn = QCUtil.QCConnection

Set oResource = qcConn.QCResourceFactory

Set oCurrentResources =oResource.NewList("")

Set oNewResource = Nothing
resourceCount = oCurrentResources.Count
For iNowResourceNum = 1 To resourceCount
nowResource =
 oCurrentResources.Item(iNowResourceNum).Name

   if UCase(nowResource) = UCase(QcResourceName) then

    Set oNewResource = oCurrentResources.Item(iNowResourceNum)

    resourceFound = "True"

   end if

Next
If resourceFound = "True" Then

 oNewResource.Filename = fileNameToSave

 oNewResource.ResourceType = "Test Resource"
 oNewResource.Post

 oNewResource.UploadResource fileToSavePath, True

Else

 reporter.ReportEvent micFail,"Did not find a resource in the Test Resource module named " & QcResourceName,"Verify that a resource exist in the QC Test Resource module!"

End If
Set oCurrentResources = Nothing

Set oResource = Nothing
End Function
'------------------------------------------------------------
'
'@Function Name: QCGetResource

'@Documentation Saves a text file from a QC Test Resource to a local dir

'@Created By: Joe Colantonio

'@Return Values: NONE

'@Example: QCGetResource "qcresourcetest.txt","C:\Temp"

'-----------------------------------------------------------
Function QCGetResource(resourceName,saveTo)
Set qcConn = QCUtil.QCConnection

Set oResource = qcConn.QCResourceFactory

Set oFilter = oResource.Filter
  oFilter.Filter("RSC_FILE_NAME") = resourceName
Set oResourceList = oFilter.NewList
If oResourceList.Count = 1 Then

  Set oFile = oResourceList.Item(1)
oFile.FileName = resourceName
oFile.DownloadResource saveTo, True

End If
Set qcConn = Nothing

Set oResource = Nothing

Set oFilter = Nothing

Set oFlieList = Nothing

Set oFile = Nothing
End Function

Comments

  1. 1415 says

    Can you let me know how to download all the resources that are stored in the quality center ‘test resources’ folder to local machine using OTA script?

    • Joe Colantonio says

      @1415: Hi – When you say all resource do you want everything – including function libraries and object repositories? I’m not 100% sure how to do this. I have not tried to do this yet but you should be able to step-thru and modify my code example to do this. Also HP’s KB has an article that shows how to download resources using a custom DLL that may be better for your situation. Check HP article KM838074 .

  2. Niranjan says

    I want to upload a file into the resource without checking for existance of the file in the test resources module.

    Could please let me know the same on how to do it.

    Regards,
    S.Niranjan

  3. William says

    Hi Joe
    I encounter an issue while save my resoucre file to QC. It alerts that “The external resource could not be uploaded to the QualityCenter server repository”. Do you know the root cause of the problem?

    • Joe Colantonio says

      @William: Hi William – I’ve seen this error if I try to save a resource name that is not unique or it could be caused by a type/spelling error.

  4. William says

    Hi Joe
    Many thanks for your replying! I used the full file path like “C:\Temp\test.vbs” as fileToSavePath parameter value. However, while I was using “C:\Temp\” as fileToSavePath value, the error message was not displayed, but the attachment is blank.

  5. Asheena Andrews says

    Hi Joe,

    I am looking for help generating automatic test execution status reports from QC. Can you suggest how I can do it? I am not a coder and so need step by step instructions. I would really appreciate quick help please. Also I have used many of your videos on Service test and load runner that you posted on youtube. I found it very useful. Thanks.

    Asheena

    • Joe Colantonio says

      Asheena Andrews » Hi Asheena – my team just uses the standard QC Analysis reports. To get the info we need, in the test lab, we usually set the QC set filter option first. Is there a specific issue you are having?

  6. Kumar S says

    Hi Joe, How do I get the file from other places like Test Plan tab or do the files have to exist in the Resources tab? We use QC 9.2 and do not have Resources tab.
    Are there any other places that we can save the files in QC and retrieve using QTP?

    With the existing code, qcConn.QCResourceFactory displays error saying — ‘Object doesn’t support this property or method’

    THanks

    • Joe Colantonio says

      Kumar S » Hi Kumar – if you are running your QTP test in a test set in QC you can save and retrieve files in the test lab’s ‘Attachments’ tab. I can send you a code example of this if you want. Let me know.Cheers~Joe

  7. Kumar S says

    Hi Joe, This is exactly what I want to do. We have a test set with different test scripts and I want to have some attachments (input xml files) within this test set that I would like to read from each script and use appropriate input data. Any sample code would be awesome.

    Thank you

  8. says

    Hi Joe,

    When I try to upload one Text file. I’m getting below errors.

    Failed to Post Simple Key Entity
    Line (49): “oNewResource.Post”.

    When I click on “Retry”. I’m getting below error

    The resource must be assigned a name before it can be uploaded or downloaded from the Application Lifecycle Management Platform.

    Line (50): “oNewResource.UploadResource fileToSavePath, True”.

    Can I know why actually this error is poping up.

    Actually, my requirement is to upload Result Excel files into specific Folder (say “Results” folder) in Resources or TestPlan tab.

    Can anybody know the above procedure?

    regards,
    Baba Fakruddin D

    • Joe Colantonio says

      Baba » Hi Baba – I just tried the code that I posted against ALM11 and it worked for me. In ALM do you have a Results resource setup under Resources? Can you send me exact code you are using? Thanks~Joe

  9. Kari says

    Hello Joe,
    I was wondering if it would be possible to save a file to the Attachments section in a test case(Test Name)? ex: Root\folders\Test Set\Test Name

    We run our tests on another stand alone machine that has QTP. And using QTP to update QC fields. When the test fails I want to create a txt file with the failure results and save the file to the attachments for that test case via QTP to QC.
    Is this possible?

    Any tips or suggestions are much appreciated!
    Thanks,
    Kari

  10. Achraf says

    Hello,
    I was wondering if it is possible to the same thing but for downloading a resource that is in “Check Out” or to an older version of a resource? (i have versioning enabled in my project)
    Thanks,

    • Joe Colantonio says

      Achraf » Hi Achraf – Unfortunately I don’t know. I would try but I don’t have a QC environment that uses version control. Please let us know if you figure it out. Cheers~Joe

      • Achraf says

        Hello again,
        For a file in checkout, DownloadResource already does that automatically (it downloads the version in checkout by the user if possible)
        For downoading older versions of the file, the only way i found, was to build the full physical path to the version of file i want, and then use the TDConnection.ExtendedStorage object to download it. (however building the path is a bit tricky and requires much longer to explain how it’s done)

        Thanks and regards,

  11. John Pellen says

    Hi Joe,
    In following code I am trying to upload excel file to Data table resource in Quality Center. But I am getting two errors as following.

    1) on line “oRes.Post”, I got error “Failed to Post simple Key entry”.

    2) on line “oRes.UploadResource “, I got error “The external resources could not be uploaded to the Quality Center Server Repository ”

    ============================================
    Code
    ============================================

    Set QCResource =
    QCUtil.QCConnection.QCResourceFactory

    Set NewQCResource = QCResource.NewList(“”)

    For i=1 to NewQCResource.Count
    If NewQCResource(i).Name = “InputDataFile” Then
    Set oRes = NewQCResource(i)
    Msgbox oRes.FileName
    Exit For
    End If
    Next

    oRes.FileName = “InputData”
    oRes.ResourceType = “Data table”
    oRes.Post
    oRes.UploadResource “C:\InputData.xls”,True

    Set oRes = Nothing
    Set NewQCResource = Nothing
    Set QCResource = Nothing

    • Joe Colantonio says

      John Pellen » Hi John – Sorry, I have not see this issue before and I’ve been unable to repro on my machine. Have you figured it out yet? Anyone else have any ideas?

  12. vasu says

    Hi Joe,
    Big thanks for the QTP Paches repository in your Blog.I recently downloaded QTP-00092 Patch for firefox 9 and 10 support.
    My question is how to test a webEdit object by Boundary Value Analysis using QTP(Especially for maximum length+1 case).
    suppose my WebEdit maxlength is 10 characters and if want to test it for 11 characters.
    If i use .Set method QTP throwing an error as Parameter is Incorrect.
    If i use Type Method,I’m getting the error as object does’nt support this method.
    So how should we test with QTP in this particular negative scenario????????????
    Thanks in-advance

    • Joe Colantonio says

      vasu » Are the errors QTP errors or application under test errors/dialogs? I would think that either set or type would work for you- have you tried sendkeys?

      • vasu says

        Hi Joe.we are using QTP-11.00 and QC(ALM)-11.00.Recently we integrated QTP and QC after installing QC-QTP addin and QTP-11.00 addin and checked the checkbox in “Tools<Options,Run<Allow other HP products to run tests and components" in QTP.We saved our script in TestPlan module (subject<some folder) from QTP.When we click on TestScript tab in TestPlan we are getting QC dialog box errors as
        "Attempted to read or write protecte memory.This is often an indication that other memory is corrupt."If we click on OK button then again we are getting dialog as
        "External component has thrown an exception."we are unable to view script in QC.
        Plz suggest me any solution regarding this issue.
        Environment-windows 7

        Thanks in-advance

  13. Lina says

    Hey, Joe,

    I am using vapi xp vbscript test to upload .xls file to the QC Test Resources. But after running it I got error: “The external resource could not be uploaded to the QualityCenter server repository. ” (there are no duplicate names in resources). Then I checked QC Test Resources and there was file name inserted, but download button was disabled. I can’t figure out what is wrong. I see others were also getting same error. So maybe you or someone else have figured out what’s wrong? It would help me a lot in my job.

    BR,
    Lina

    • Joe Colantonio says

      Lina » HI Lina the only time I’ve seen this error is when I’m using the wrong path or resource name. Anyone else have any ideas?

  14. vasu says

    How to get min length of a webedit using QTP(VB scripting) ???
    just like getting max length by
    maxL=Browser(“”).page(“”).Webedit(“”).getroProperty(“max length”)

  15. sam says

    @ John Pellan – on line “oRes.UploadResource “, I got error “The external resources could not be uploaded to the Quality Center Server Repository ”

    Resolution :
    Remove backslash ‘\’ for uploadfolder name and verify that Upload file name is the same as it exists on local machine

  16. Fulge says

    Hi!
    I want to know how can I call a .bat file that is located on my Resources tab in ALM from my QTP test? Is this possible?

    This .bat file call the application I want to test with specific login information according to the test that is executed, or at least that’s the idea.

    How can I call this . bat file from within QTP script?

    Any Idea will be appreciated.

    • Joe Colantonio says

      Fulge » You should be able to save a.bat file to a test resource in ALM and call it using the code I posted. Are you having an issue with the code?

  17. Venky says

    Hi Joe,

    Thanks for the excellent post.
    In addition, How would I add a ‘Resource Folder’ through API? I have tried something like this,

    Set resFolder = tdc.QCResourceFolderFactory
    resFolder.AddItem (“test_fol”)

    but it doesn’t create folder. Can you please help how to create resource folder throught API?

  18. Renjith says

    I am having a project requirement , which I need to retrieve defect time stamp from HP Quality center to check, time of arrival of the defect.Please send me the code otherwise I wont get appraisal this time. I have given word to my manager.

  19. says

    I’m also getting the “The resource must be assigned a name before it can be uploaded or downloaded from the Quality Center server.” error when I upload.

    TDAPIOLELib.QCResourceFactory resFact = (TDAPIOLELib.QCResourceFactory)connection.QCResourceFactory;
    TDAPIOLELib.IResourceStorage res = (TDAPIOLELib.IResourceStorage)resFact[id];

    res.UploadResource(file, true);

    any suggestions?

    • says

      I wasn’t setting the “resource.FileName” property of the resource before I was trying to upload. That specific error went away but now I’m getting “The external resource could not be uploaded to the QualityCenter server repository.”

      • Joe Colantonio says

        Drew » Drew finally figured this out. Here is his code – Thanks Drew for sharing your resolution!
        ///

        /// Updloads a file a specified resource
        ///

        ///The ID of the file to upload to
        ///The local path of the file to upload
        /// True if successful
        public bool Upload_Resource(int FileId, string ResourcePath)
        {
        try
        {
        TDAPIOLELib.QCResourceFactory resFact = (TDAPIOLELib.QCResourceFactory)connection.QCResourceFactory;
        TDAPIOLELib.QCResource resource = (TDAPIOLELib.QCResource)resFact[FileId];

        string[] FilePath = ResourcePath.Split(‘\\’);

        TDAPIOLELib.IVersionControl _ivc;
        TDAPIOLELib.IVersionedEntity _ive;

        _ive = (TDAPIOLELib.IVersionedEntity)resource;
        _ivc = (TDAPIOLELib.IVersionControl)_ive.VC;
        _ivc.CheckOut(“Uploading file to resource.”);

        try
        {
        resource.FileName = FilePath[FilePath.Length – 1];
        resource.ResourceType = “Test Resource”;
        resource.Post();

        TDAPIOLELib.IResourceStorage res = (TDAPIOLELib.IResourceStorage)resource;
        res.UploadResource(ResourcePath.Replace(FilePath[FilePath.Length – 1], “”), true);

        _ivc.CheckIn(“Uploading file to resource.”);

        return true;
        }
        catch
        {
        _ivc.UndoCheckout();
        return false;
        }
        }
        catch { return false; }
        }

  20. somanath says

    hi Joe,
    I am also getting error “failed to post simple key entity” oNewResource.post . I am using exactly the same code written by you above . Can you please help me . Can you tell me the probable reasons for this .
    Many many thanks in adavance :):):)

    • Joe Colantonio says

      somanath » Hi – not sure why this is happening — it does work for me. Anyone else have any ideas?

  21. Cha says

    Hi There,

    I have to download particular folder in QC resources which contain .vbs file, which related to my automation suite however I am wondering if you can help me in this.

    Thanks,

      • Cha says

        Hi Joe,

        Thanks for reply.
        Function which I used “QCGetResource” downloads only 1 file at a time which is been passed, i need to download entire folder from resouces tab,

        However really appriciate if you would provide code snippet for that.

        Thanks

  22. Sri says

    Hi Joe, Good day….
    In our organization we are planning to move all our scripts, Library files, repository, data files and xml files to Test Resource folder in QC. Can you please let me know how to call these file from QC in the script???
    Thank you for your help.

    • says

      Sri » Hi Sri – you should be able to associate your function libraries from QTP>File>Settings and in the Test Settings click on Resources. You can point to a library in QC and associate it with your test script. For Object Repository you can do the same by going into QTP’s menu Resources>Associate Repositories and select your OR’s stored in QC. Is this what you are looking for?

  23. Santhosh says

    Hello Joe,

    Thanks a lot for such a wonderful site. I’m also facing the same issue what Sri is referring to.. Can you please assist with the code for the following..
    1. To connect qtp and qc during run time.
    2. To import library, excel, repository during run time
    3. To add the test cases to test lab during run time
    4. To exectue the test scripts added in test lab
    5. To store and view the result.

    Thanks a ton for your suppport.

    Cheers,
    Santhosh.

  24. Ben says

    Joe, I am attempting to use the code included in this blog post and
    am wondering what the filter would be to search by resource name instead of file name. I have several resources that contain the same filename (i.e. different copies of the same file listed under different resource names).

    PS. It appears that all resources are returned when it fails to find the one you set in the filter. In case anyone is wondering why they are getting back a large number of resources instead of just 1.

    • Ben says

      Figured it out. Looks like it’s nice and obvious. Just “RSC_NAME”. I was unable to find a list of possible filter values in the OTA documentation. Where can I get one?

  25. Hara Prasad says

    Hi Joe,

    Very useful post. One question though – here “oFilter.Filter(“RSC_FILE_NAME”) = resourceName” filters by resource name. What other filters are available and where can I find the documentation for these. It would help me a lot if I could also filter on the resource’s parent folder.

    Please advise.

    Regards,
    Hara Prasad

    • Candace says

      Hara –

      Check the OTA reference for for TDFilter, you’ll see: “The values can be constants, special tokens such as “[Today]”, “[PreviousWeek]”, or “[NextYear]”, or folder names, such as “^Subject\LinkFolder^”. Use the set filter dialog in the the relevant module of the ALM user interface to explore the possibilities.”

      So open the Filter dialog under resources, set up the filter that you’d want, and then you can Copy/Paste the actual filter. It contains what you need, for example, when I set up a filter to give me the Test Resource with folder name = My Host Names, it looks like:

      [Filter]{
      TableName:RESOURCES,
      ColumnName:RSC_FOLDER_NAME,
      LogicalFilter:0000019\^Resources\My Host Names^,
      VisualFilter:0000019\^Resources\My Host Names^,
      NO_CASE:
      }

  26. sandhya says

    Hi Joe,
    I used the Function to getResource but it works with one reource at a time. I was wondering if it is possible to build the resources list available and do the download by iterating through the list?
    It is very cumbersome to download the resources one at a time.
    Thanks
    Sandhya

  27. shanker says

    Hi Joe,

    Today I am feeling great to find your website.
    I want to download a resource from QC Resource which is saved in a subfolder at 3rd or 4th level from Resources folder. The oFilter.Filter(“RSC_FILE_NAME”) = resourceName, in your code is not working for this.

    Can you please help.

    Regards,
    Shanker

  28. Srini says

    Is there is a API to connect and extra data from Performance Center(subset of ALM) using QTP. I need to read few columns within the test lab during run time.

    Thank You,
    Srini

    • says

      Srini » Hi Srini – I’m still researching this. I don’t have Performance Center installed so I’m not sure. I know that ALM 11 have a REST api but I don’t think it touches the performanace center info. Does anyone else know if this is possible?

  29. Parminder says

    Hi Joe,

    I am trying to download a resource from ALM. Though my code works fine for the first time but when i try to download the same resource again then i am getting below error :

    The resource could not be downloaded to the Application Lifecycle Management client machine.

    Line (39): “oFile.DownloadResource sLocalPath, True”.

    Do you have any Idea about this error?

    I am using the below code :

    Set oQCCon = CreateObject(“TDApiOle80.TDConnection”)

    sFileName = “DateTime.vbs”

    oQCCon.InitConnectionEx “http://qc:8080/qcbin”

    oQCCon.Login sUserName, sPassword

    If (oQCCon.LoggedIn True) Then
    Reporter.ReportEvent micFail, “QC Authenticated Failed. Unable to Login”, “Username : ” & sUserName & ” Password : ” & sPassword
    ExitTest
    End If

    oQCCon.Connect sDomain, sProject

    If (oQCCon.Connected True) Then
    Reporter.ReportEvent micFail, “QC Project Failed to Connect to ” & sProject, “QC Project Failed to Connect to ” & sProject
    ExitTest
    End If

    Set oResource = oQCCon.QCResourceFactory
    Set oRec = oResource.NewList(“”)

    For Each oResrc in oRec
    sPath = oResrc.Path
    If sPath = sResoucePath Then
    Set oFile = oResrc
    oFile.FileName = sFileName
    oFile.DownloadResource sLocalPath, True
    Exit For
    End If
    Next

    Set oResource = Nothing
    Set oRec = Nothing
    Set oFile = Nothing

    oQCCon.Disconnect
    oQCCon.Logout
    oQCCon.ReleaseConnection

    Set oQCCon = Nothing

    Thanks,
    Parminder

      • KANNAN says

        Hi Joe
        I need to upload an Excel file to a Folder in Test lab.
        While uploading, it has to replace the already existing file in the test lab. Can u please suggest me the code. It will be very helpful you suggest me at the earliest since my project is in hurry to wrap things up.

  30. Mithilesh says

    Hi Joe, Nice post. I was playing around with your code. Can I access the modified Time from the resource? My idea is to read the modified time and overwrite download to local drive from QC only if the QC resource is a new version. Your help is highly appreciated. Cheers : )

  31. Sandesh Sawant says

    Hi Joe,

    When we run the QTP test from QC Test Lab, then the test results are stored in QC testset. But is there any way by which I can copy the entire test report folder from testset to my local machine ?

  32. Shalabh says

    Hi Joe,

    I have to write a function in which we can directly save the test case from QTP in ALM test plan without associating object repository or function library.

    I have to save all the test case present in datasheet to ALM in one go.

    Can you please help me to guide.

  33. GHOUL says

    Hi,
    I have a QTP project for test and I must import it into HP QC.
    How can I do this?
    (sorry for my bad english) ;)
    thanks

  34. oindrila says

    All the data sheets were in RDP.
    Now we are integrating with QC.I have kept the file as Resources\Test data\applicationName\file.xls

    In function library,I have a code to fetch this data table for a test called ‘file’.
    when I run the test,I am getting the error – [ALM\Resources] Resources\Test data\applicationName\file.xls could not be found.

    Is this a QC permission issue?Because in QC,I am unable to edit any data in those files.I can only upload and download those files.

    In Environment.xml file(external & user defined), value for testdata = [ALM\Resources]

  35. HARSHA R says

    I am trying to download the attachments from QC- testlab to my local system. The folder in the testlab has many subfolders, under which the testset is created and the attachments are attached.

    My requirement is that while i am downloading the attachments from QC to my local system, i need to make sure that the tree structure is created even in my local drive.

    Can any one help me on this!!! AS i am very new to Automation testing …..

    Thanks in Advance for your kind support and help.

  36. Deepa Krishnaswamy says

    Hi Joe,

    In our project a user has deleted a Test Resource, However we are not able to find the details in either the AUDIT_LOG Table or the AUDIT_PROPERTIES Table.

    Please let me know which table should be used to find the details regarding the updates to the Test Resources
    Thanks in advance

Leave a Reply

Your email address will not be published. Required fields are marked *