How to add an attachment to a test set in ALM OTA via c#?
Asked Answered
I

4

6

I run the following code but nothing shows up in ALM:

AttachmentFactory attachmentFactory = (AttachmentFactory)tsTest.Attachments;
TDAPIOLELib.Attachment attachment = (TDAPIOLELib.Attachment)attachmentFactory.AddItem("test");
attachment.Post();

The AddItem method on the second line keeps asking for "object ItemData" but I have no idea what that is exactly. HP has such poor documentation that there is really nothing explaining it. Does anyone know how to programatically using c# add a file attachment to a test run in HP ALM?

Implicatory answered 6/3, 2015 at 19:45 Comment(1)
Anyone? Bueller? Bueller? Bueller?Implicatory
I
5

After much pain and research I have found an answer. I'm sure there are other ways of accomplishing this that are more efficient but since HP's documentation is the worst on the planet this is the best I could come up with. If anyone has a better way I would LOVE to see it so please post it!

I hope this helps!

try
{
    if (qcConn.Connected)
    {
        string testFolder = @"Root\YourFolder";

        TestSetTreeManager tsTreeMgr = (TestSetTreeManager)qcConn.TestSetTreeManager;
        TestSetFolder tsFolder = (TestSetFolder)tsTreeMgr.get_NodeByPath(testFolder);
        AttachmentFactory attchFactory = (AttachmentFactory)tsFolder.Attachments;
        List tsList = tsFolder.FindTestSets("YourTestNameHere", false, null);

        foreach (TestSet ts in tsList)
        {
            TestSetFolder tstFolder = (TestSetFolder)ts.TestSetFolder;
            TSTestFactory tsTestFactory = (TSTestFactory)ts.TSTestFactory;
            List mylist = tsTestFactory.NewList("");
            foreach (TSTest tsTest in mylist)
            {
                RunFactory runFactory = (RunFactory)tsTest.RunFactory;
                Run run = (Run)runFactory.AddItem("NameYouWantDisplayedInALMRuns");
                run.CopyDesignSteps();

                //runResult just tells me if overall my test run passes or fails - it's not built in. It was my way of tracking things though the code.
                if(runResult)
                    run.Status = "Failed";
                else
                    run.Status = "Passed";
                run.Post();

                //Code to attach an actual file to the test run.
                AttachmentFactory attachmentFactory = (AttachmentFactory)run.Attachments;
                TDAPIOLELib.Attachment attachment = (TDAPIOLELib.Attachment)attachmentFactory.AddItem(System.DBNull.Value);
                attachment.Description = "Attach via c#";
                attachment.Type = 1;
                attachment.FileName = "C:\\Program Files\\ApplicationName\\demoAttach.txt";
                attachment.Post();

                //Code to attach a URL to the test run
                AttachmentFactory attachmentFactory = (AttachmentFactory)run.Attachments;
                TDAPIOLELib.Attachment attachment = (TDAPIOLELib.Attachment)attachmentFactory.AddItem(System.DBNull.Value);
                //Yes, set the description and FileName to the URL.
                attachment.Description = "http://www.google.com";
                attachment.Type = 2;
                attachment.FileName = "http://www.google.com";
                attachment.Post();

                //If your testset has multiple steps and you want to update 
                //them to pass or fail
                StepFactory rsFactory = (StepFactory)run.StepFactory;
                dynamic rdata_stepList = rsFactory.NewList("");
                var rstepList = (TDAPIOLELib.List)rdata_stepList;
                foreach (dynamic rstep in rstepList)
                {
                    if (SomeConditionFailed)
                            rstep.Status = "Failed";
                        else
                            rstep.Status = "Passed";
                        rstep.Post();
                    }
                    else
                    {
                        rstep.Status = "No Run";
                        rstep.Post();
                    }
                }
            }
        }
    }
}
Implicatory answered 13/4, 2015 at 14:15 Comment(1)
For Java null type is: new Variant(Variant.Type.VT_NULL) hope it helps someone :)Pasadis
D
1

I have done something similar, but in Python and against Test Steps, so even if I don't have code you can copy & paste it, this might point you to the right direction.

Instead of calling:

attachmentFactory.AddItem( filename )

Call the function with no parameters (or a null paramer, can't tell since I never used the OTA API with C#):

file = attachmentFactory.AddItem()

Now assign the file to the attachment item, and the rest of its properties:

file.Filename = "C:\\Users\\myUser\\just\\an\\example\\path" + fileName
file.Description = "File description"
file.Type=1
file.Post()

The type specifies it's a local file, and not an URL.

Disembodied answered 10/3, 2015 at 11:59 Comment(0)
M
1

If anyone is wondering how to do that on the requirement-module, here is the code:

Req req = Globals.Connection.ReqFactory.Item(*ID*));
VersionControl versionControl = ((IVersionedEntity)req).VC as VersionControl;
versionControl.CheckOut(string.Empty);
AttachmentFactory attFac = req.Attachments;
Attachment att = (Attachment)attFac.AddItem(System.DBNull.Value);
att.Description = "*Your description here";
att.Type = (int)TDAPI_ATTACH_TYPE.TDATT_FILE; //for URL, change here
att.FileName = "*Your path including filename here*";
att.Post();
versionControl.CheckIn("*Your check-in comment here*");
Moises answered 4/12, 2017 at 16:12 Comment(0)
I
0

No valuable information on Internet! After some digging on OTA documentation I have found this:

AttachmentFactory attachmentFactory =   (AttachmentFactory)TstTest.Attachments;
            TDAPIOLELib.Attachment attachment = (TDAPIOLELib.Attachment)attachmentFactory.AddItem("demoAttach.txt");
            attachment.Description = "Bug Sample Attachment";
            attachment.Post();
            IExtendedStorage exStrg = attachment.AttachmentStorage;
            exStrg.ClientPath = "E:\\TestData";
            exStrg.Save("demoAttach.txt", true);

actually, was in VB script form but I managed to transform in C#. OTA reference:

   '-----------------------------------------
'Use Bug.Attachments to
' get the bug attachment factory.
    Set attachFact = bugObj.Attachments
'Add a new extended storage object,an attachment
' named SampleAttachment.txt.
    Set attachObj = attachFact.AddItem("SampleAttachment.txt")
' Modify the attachment description.
    attachObj.Description = "Bug Sample Attachment"
' Update the attachment record in the project database.
    attachObj.Post
' Get the bug attachment extended storage object.
    Set ExStrg = attachObj.AttachmentStorage
'Specify the location of the file to upload.
    ExStrg.ClientPath = "D:\temp\A"
'-----------------------------------------
'Use IExtendedStorage.Save to
' upload the file.
    ExStrg.Save "SampleAttachment.txt", True
Insulator answered 12/3, 2015 at 12:42 Comment(1)
I have tried your above code in c# but it fails to attach a file to the test run. Did this work for you?Implicatory

© 2022 - 2024 — McMap. All rights reserved.