public bool Attach( int TestSetId, AttachmentType Type, string Path, string Description = default(string), string Name = default(string)) { bool success = false; try { if (!Connect(ServerUrl, Username, Password, Domain, Project)) { return(false); } TestSetFactory tsFact = tdc.TestSetFactory; TestSet tSet = tsFact[TestSetId]; string tSetName = tSet.Name; //Strange ALM bug allows you to select a non existant tet set. Calling name ensures it exists AttachmentFactory attachFact = tSet.Attachments; Attachment attach = attachFact.AddItem(DBNull.Value); attach.FileName = Path; TDAPI_ATTACH_TYPE attachType = TDAPI_ATTACH_TYPE.TDATT_FILE; switch (Type) { case AttachmentType.File: case AttachmentType.file: attachType = TDAPI_ATTACH_TYPE.TDATT_FILE; break; case AttachmentType.URL: case AttachmentType.url: case AttachmentType.Url: attachType = TDAPI_ATTACH_TYPE.TDATT_INTERNET; break; } attach.Type = (int)attachType; if (Description != default(string)) { attach.Description = Description; } //Post will fail if attachment path is bad attach.Post(); if ((Name != default(string)) && (attachType == TDAPI_ATTACH_TYPE.TDATT_FILE)) { attach.Rename(Name); attach.Post(); } success = true; } catch (COMException ce) { //If we get an error, delete the attachment //If the file path is invalid and the attachment never uploads, ALM throws an error but posts the attachment anyways //returning no attachment object. So the only way to detect this is to look for 0 sized attachments and delete them TestSetFactory tsFact = tdc.TestSetFactory; TestSet tSet = tsFact[TestSetId]; AttachmentFactory attachFact = tSet.Attachments; List attachList = attachFact.NewList(""); foreach (Attachment a in attachList) { if (a.FileSizeEx == 0) { attachFact.RemoveItem(a.ID); } } rr.AddErrorLine(HandleException(ce)); } finally { Disconnect(); } return(success); }
public bool Attach( int RunId, AttachmentType Type, string Path, string Description = default(string), string Name = default(string)) { bool success = false; try { if (!Connect(ServerUrl, Username, Password, Domain, Project)) { return(false); } RunFactory runFact = tdc.RunFactory; Run testRun = runFact[RunId]; //ALM bug allows selection of invalid runid; use name to catch it string runName = testRun.Name; AttachmentFactory attachFact = testRun.Attachments; Attachment attach = attachFact.AddItem(DBNull.Value); attach.FileName = Path; TDAPI_ATTACH_TYPE attachType = TDAPI_ATTACH_TYPE.TDATT_FILE; switch (Type) { case AttachmentType.File: case AttachmentType.file: attachType = TDAPI_ATTACH_TYPE.TDATT_FILE; break; case AttachmentType.URL: case AttachmentType.url: attachType = TDAPI_ATTACH_TYPE.TDATT_INTERNET; break; } attach.Type = (int)attachType; if (Description != default(string)) { attach.Description = Description; } //Post will not fail if attachment is bad (different than attaching to test set) attach.Post(); int attachId = 0; attachId = attach.ID; //Console.Out.WriteLine(attachId); if ((Name != default(string)) && (attachType == TDAPI_ATTACH_TYPE.TDATT_FILE) && attach.ID > 0) { attach.Rename(Name); attach.Post(); } //If the file path is invalid and the attachment never uploads, ALM throws an error but posts the attachment anyways //returning no attachment object. So the only way to detect this is to look for 0 sized attachments and delete them attach.Refresh(); if (attach.FileSize == 0 && attach.Type == (int)TDAPI_ATTACH_TYPE.TDATT_FILE) { attachFact.RemoveItem(attach.ID); success = false; } else { success = true; } } catch (COMException ce) { rr.AddErrorLine(HandleException(ce)); } finally { Disconnect(); } return(success); }