QC & QTP – How to Attach a File to a Test Sets Attachment Tab

Automation Guild Online Conference Top ad

QC & QTP – How to Attach a File to a Test Sets Attachment Tab

Do you need your test scripts to save test files in QC? Or do you may have a file that contains test data that you want your scripts to read from? Yesterday I received an email form my blog friend Kumar that asked how this can be done using Quality Center and QTP. Hopefully this post will explain how this functionality can be achieved. You also might want to check out How to use QTP to save and retrieve a Quality Center Test Resource

Steps to attach a file in QC

1. In QTP Create a new test named TestLabAttachment and save in a folder in Quality Center:

2. In QC add the QTP script ‘TestLabAttachment' into a test set in the QC ‘Test Lab” section

3. Create a local text file on your QTP machine that you want to add to the QC ‘Attachments' tab. For example:

3. In QTP add the following lines of code:

Set nowTest = QCUtil.CurrentTestSet

Set attachmentPath = nowTest.Attachments

Set nowAttachment = attachmentPath.AddItem(Null)

‘Replace with the path to your file:

nowAttachment.FileName = “C:\QTPAUTOMATION\DATA\testenv.txt”

nowAttachment.Type = 1


4. Run QTP script. In the Run dialog select ‘Results Location' tab and click on ‘New run Results in QC project”. Select the Test Set that the current script is saved in.

5. After the QTP script runs go back to QC and look under the test set's ‘Attachments' tab. The file should appear:

Steps to retrieve file from the QC Test Set ‘Attachments' tab

    1. Open QTP
    2. In the same script that you created above TestLabAttachment enter the following code (“I admit this code is ugly – but it works. If you know a better way please let me know”)

qcAttachmentName = "yourfilename" 'For example testenv.txt
localPathToCopyToOrFrom = "the path to save the QC file to" ' For example C:\OTA
Set nowTest = QCUtil.CurrentTestSet
Set otaAttachmentFactory = nowTest.Attachments
Set attachList = otaAttachmentFactory.NewList ("")

IF attachList.Count > 0 Then

 For i = 1 to attachList.Count

     Set attachObj = attachList.Item(i)
     tempPath = attachObj.FileName

     'Get temp file path and create array to hold elements
     arrayPath = split(tempPath,"\")
     eCount = Ubound(arraypath)

     'We use the last element to grab the file name we will use in target folder
     tempName = arrayPath(eCount)
     'Check to see if file name exist in QC
     If instr(1,tempPath, qcAttachmentName) Then
         attachObj.Load True,""

         Set objFSO = CreateObject("Scripting.FileSystemObject") 
         localPath = localPathToCopyToOrFrom &"\" & tempName

        'Check to see if the file exist -- if it does delete it before copying new file

            if objFSO.FileExists(localPath) then
            end if

             objFSO.MoveFile tempPath,localPath    

             'Check to see if a file with the same name exist in the target folder.
             'If it does delete it before renaming
             localPathToCopyToOrFrom = localPathToCopyToOrFrom &"\" & qcAttachmentName

         if objFSO.FileExists(localPathToCopyToOrFrom)then
          end if

             objFSO.MoveFile localPath,localPathToCopyToOrFrom    
            exit for
  end If



msgbox "File not found"

End IF




Set nowTestSet = Nothing
Set otaAttachmentFactory = Nothing

  1. Run QTP script. In the Run dialog select ‘Results Location' tab and click on ‘New run Results in QC project”. Select the Test Set that the current script is saved in.

4. After the QTP script runs go the folder where you saved your file. The QC file should now be in it.

Also be sure to check out Application Lifecycle Management for the Whole Team

Kumar S - August 22, 2011

Nice work Joe. Thank you again.

greg woffindin - October 4, 2011

Thanks, this worked fine. The problem i am having is how do i skip this function when i am running a test locally vs through quality center. I need to confirm a QC connection and skip this when it’s not connected (running locally) .

    Joe Colantonio - October 20, 2011

    greg woffindin » Hi Greg – sorry for my late response. If you have not figured this out already you could use something like this:
    ‘This is if the test is truly local meaning the QTP script is not stored in QC
    qcEnabled = QCUtil.IsConnected
    if qcEnabled = “True” then
    yourcode QC attachement code
    end if

    If the script is stored in QC and you want to run the script from QTP not QC you need to store the script in a test lab. When you click run in the ‘Results Location” tab point to the scripts test lab in the ‘new run results in QC’ ‘Test Set’ option.

    Hope this helps you.


Greg Woffindin - January 15, 2012

Thanks, I figured out what i needed. Actually what i have is first a check to see if an attachment is stored in QC in the test lab section, if not it then checks for a copy of the attachment on the pc running the script and then if it can’t find it uses a default value. I was loading some environment variables and wanted to allow someone running the script via QC to easily change them and have a way to run the scripts on a local pc for creating/troubleshooting and be able to change them.

    Joe Colantonio - January 15, 2012

    Greg Woffindin » Awesome – thanks for the update Greg!

Murali - January 20, 2012

Hello Joe,
You have an excellent site with tons of resources. I am glad to say I have been a beneficiary of this site since the time I found. Many Thanks for maintaing such a wonderful site.
Have a question about creating shortcut keys for menu items of an AUT. We have a custom written .Net application which doesnt lend itself easily to the recording feature of QTP(11.0), as it appears all the objects are private. Someone suggested the only way of accessing the Menu Items would be to custom map shortcut keys and then navigate to the objects on the pages opened.
Wondering if you ran into a similar situation and considered taking this approach. would appreciate your thoughts and guidance on how this may be achieved.


    Joe Colantonio - January 27, 2012

    Murali » Hi Murali – thanks! I’m glad that you find the site helpful! I have seen instances when I do need to use either SendKeys or a Windows type function to access a AUT functionality. I would give either method a try and see how reliable it works for your app. Also I would ask a developer if there was a way to make the menu more automation friendly. Either by using standard controls or exposing a method the help QTP recognize it. Hope this helps. Cheers~Joe

Venkat - April 9, 2012


Can you help me in explaining how we can able to attach a file to the current step at runtime using QTP? We have a requirement to upload a snapshot for each of the step that has been executed using automated script. We were able to upload a file or snapshot to current test run, but not to step. Appreciate your help.


    Joe Colantonio - April 20, 2012

    Venkat » Hi Venkat I haven’t had time to try to write to a specific test step in a test. BUt does the QTP>Options>Run>Screen capture set to “always” options capture enough info to meet your requirements?

      Abhishek - February 26, 2013

      Hey, it is set as always but still the screenshot is not attached fr each step.

      Actually, i have used a report fail fn and within that upload function exist. I’ve used the below fn

      Public Function UploadScreenshotQC(ScrnshotFileName)
      Dim Test_CurrentRun,Test_Attachment,Test_AttachItem’,Test_AttachStore
      Dim Local_Path,Local_FileName

      Set Test_CurrentRun=QCUtil.CurrentRun

      ‘Check that the test is running from QC, otherwise it will exit
      If (Test_CurrentRun Is Nothing) Then
      Exit Function
      End If

      ‘Attach the File to the current test
      Set Test_Attachment=Test_CurrentRun.Attachments
      Set Test_AttachItem=Test_Attachment.AddItem(Null)

      End Function

        michaeltewierik - October 1, 2014

        ‘sFilePath is the location and name you want the file captured to;
        ‘oToCapture is the object you want too screenshot;
        ‘NOTE: If no object exists and you want the whole desktop (as in the case of a catastrophic failure in test) just supply NULL as the object;
        Function ScreenshotForTestResult(sFilePath,oToCapture)
        If isNull(oToCapture) or isEmpty(oToCapture)
        Desktop.CaptureBitmap sFilePath,True
        oToCapture.CaptureBitmap sFilePath,True
        End If
        Reporter.ReportEvent micDone,”Screen Capture”,”Screen Capture”,sFilePath
        End Function

Poornima - September 11, 2012

Very Helpful , Thank you :)

Poornima - September 11, 2012

Joe, Is there any way to provide a link to the attachments in that particular test set inside the QC Report? This would help the person looking into the QC report as they can directly click on the link to go to the uploaded attachments

Abhijit Mahajan - October 26, 2012

Can I use above code (attachment to QC) with others tools as well like Testing anywhere as for those tools we are not storing scripts in QC only uploading results .

    Joe Colantonio - November 2, 2012

    Abhijit Mahajan » You should be able to upload text file usingg this vbscript regardless of what test tool created it.

Abhishek - February 27, 2013

Using QCUtil it is possible to add screenshot to current test, current run, test set level. But is it posible to add the screensot in test step level….Pls advice

Laura - April 18, 2013

I have results from a SOAPUI Pro script that I would like to attach to the Test Lab Test Suite, but I can only see how to do it one at a time. Is there a way to attach multiple files at once?

Priyanka - June 10, 2013

how to attach multiple files in test lab module for each test case in ALM 11.0 version? Is there any script other than QTP?

    Joe Colantonio - September 2, 2013

    Not sure I understand your question – you want a way to do this without using QTP? You can use just vbscript using OTA

Drew - November 7, 2013

Hi Joe-
I have a BPT in which an XML file is generated; the XML file is then attached to the test set (via QTP). Is there a way within QTP to ‘refresh’ the connection to QC/ALM so that the next test in the (same) BPT can access the attached file? Maybe something using QCUtil?

Thanks in advance for any suggestions!!

Vamsee - December 5, 2013

Hi the above code is to attach a file to test plan module right!
but not test lab module….can you please explain me how to attach a file to test lab module in qc. Appreciate your help.

Vamsee - December 5, 2013

Sorry Joe….its working my mistake
thanks! this site is very helpful for me as a beginner thanks a lot

Supriya - December 11, 2013

I want to know whether we can attachment can be done except HTML format using QTP?

Reddy - December 17, 2013

Joe – Could you please tell me how delete the attachment from test lab/test set’s Attachments’ tab.

Reddy - December 18, 2013

Joe – Please ignore my previous request about ‘How to delete an attachment from test lab/test set’. i found my answer, below is the code to delete an attachment for test lab.

Function RemoveQCAttachements(NameOfTheFile)
Set nowTest = QCUtil.CurrentTestSet
Set otaAttachmentFactory = nowTest.Attachments
Set attachList = otaAttachmentFactory.NewList (“”)
var_count = attachList.Count
For i= 1 to var_count
strFileName = attachList.Item(i).Name ‘Name of the file
strFilePathwithName = attachList.Item(i).FileName ‘File complete path with name
intAttachmentID = attachList.Item(i).ID ‘File Attachment ID
otaAttachmentFactory.RemoveItem(intAttachmentID) ‘remove attachment
End Function

Sometimes late response also resolves the issues :) thank you !!

Sreedhar - April 17, 2014

I am getting my connection lost with the current test which i am executing but i Have the QC CONNECTION LIVE.

Naveen - June 10, 2014

Hi Joe, I’m naveen from CSC, I got a requirement like… I’ve to download excel spread sheet from qc, modify it and then upload it to same place, but here QC is not accepting to override the file, its creating new file by addir +1 +2 etc, please suggest me how can I save/override same file with modifications.


Arjunbala - October 13, 2014

Hi Joe.. I just want to check whether all the testcases inside the testlab are executed and it has attachments for the verifying results I need to generate the report for the same to validate the attachments are there or not.. with out using QTP.. I think we need to use some SQL queries for that.. Please help me in that..ASAP..

Soumya - November 25, 2014

I need to search for a particular attached file name in test plan module.how to get the test case name with which the file attached.

Vivekraj Pugalendhi - March 20, 2015

Hi Joe. Thanks a lot for sharing all these. Really appreciate your efforts and support. Thanks again….!!!!

Philip - June 11, 2015

hi Joe,

Please help me with the code for the below scenario:

QTP script to write a sql query result into an excel sheet and then upload this into the test lab folder as attachment in QC..

Can this be done..??


Sumit - October 21, 2016

The below code work fine to update any file to ALM – QC

Public Function addAttachToTDRun (strFileName)
Dim CurrentRun, attachment

On error resume next
If QCUtil.IsConnected Then
Set CurrentRun =QCUtil.CurrentRun
Set attachment = CurrentRun.Attachments
If Err.Number = 424 Then
Reporter.ReportEvent micWarning, “Not running through QC so can’t save results”, “”
Reporter.ReportEvent micDone, “Attachment Saved to QC run data”, strFileName
Set attlist = attachment.NewList(“”)
Set att = attachment.AddItem(Null)
att.FileName = strFileName
att.Type = 1
att.Save False
End If

Reporter.ReportEvent micWarning, “Not connected”, “Not connected to Quality Center”

End If

On error goto 0

End Function

Click here to add a comment

Leave a comment: