/// <summary> /// Creates the new test step. /// </summary> /// <param name="testBase">The test case.</param> /// <param name="stepTitle">The step title.</param> /// <param name="expectedResult">The expected result.</param> /// <param name="testStepGuid">The unique identifier.</param> /// <returns>the test step object</returns> public static TestStep CreateNewTestStep(ITestBase testBase, string stepTitle, string expectedResult, Guid testStepGuid) { ITestStep testStepCore = testBase.CreateTestStep(); testStepCore.ExpectedResult = expectedResult; testStepCore.Title = stepTitle; if (testStepGuid == default(Guid)) { testStepGuid = Guid.NewGuid(); } TestStep testStepToInsert = new TestStep(false, string.Empty, testStepGuid, testStepCore); return testStepToInsert; }
public static void AddSteps(ITestBase testCase, IEnumerable <Step> steps, string prefix, bool isScenarioOutline) { if (steps == null || testCase == null) { return; } var isThen = false; foreach (var stepDef in steps) { var step = testCase.CreateTestStep(); var text = stepDef.Text; if (isScenarioOutline) { text = TransformParameters(text); } text = HttpUtility.HtmlEncode(text); var dataTable = CreateDataTable(stepDef.Argument, isScenarioOutline); /* * This was to handle the situation when, the "Then" step followed by "And" steps in which case, * they should also go to Expected Result column instead of a normal step. It should resume * populating normal steps as soon as it hits a WHEN, assuming that there won't be Givens * followed by Then */ if (stepDef.Keyword.Trim().Equals(WHEN, StringComparison.InvariantCultureIgnoreCase)) { isThen = false; } if (stepDef.Keyword.Trim().Equals(THEN, StringComparison.InvariantCultureIgnoreCase) || isThen) { isThen = true; step.ExpectedResult = text; step.ExpectedResult += dataTable; step.Title = stepDef.Keyword.Trim(); } else { step.Title = $"{prefix} {stepDef.Keyword} {text}"; step.Title += dataTable; } testCase.Actions.Add(step); } }
static void Main(string[] args) { // provide collection url of tfs var collectionUri = "http://*****:*****@"C: \Users\pankagar\Downloads\Canvas.png", FileMode.Open, FileAccess.Read); var attachmentObject = _witClient.CreateAttachmentAsync(uploadStream, "Canvas.png", "Simple").Result; // create a patchdocument object JsonPatchDocument json = new JsonPatchDocument(); // create a new patch operation for title field JsonPatchOperation patchDocument1 = new JsonPatchOperation(); patchDocument1.Operation = Operation.Add; patchDocument1.Path = "/fields/System.Title"; patchDocument1.Value = "Testing Rest Api"; json.Add(patchDocument1); // create a new patch operation for priority field JsonPatchOperation patchDocument2 = new JsonPatchOperation(); patchDocument2.Operation = Operation.Add; patchDocument2.Path = "/fields/Microsoft.VSTS.Common.Priority"; patchDocument2.Value = "2"; json.Add(patchDocument2); // create testbasehelper object TestBaseHelper helper = new TestBaseHelper(); // create testbase object to utilize teststep helpers ITestBase tb = helper.Create(); // create 2 test steps ts1 and ts2 ITestStep ts1 = tb.CreateTestStep(); ITestStep ts2 = tb.CreateTestStep(); ts1.Title = "title -> title1"; ts2.Title = "title -> title2"; ts1.ExpectedResult = "expected1"; ts2.ExpectedResult = "expected2"; ts1.Description = "description1"; ts2.Description = "description2"; // adding attachment to step1 ts1.Attachments.Add(ts1.CreateAttachment(attachmentObject.Url, "CanvasImage")); // add your steps actions to testbase object tb.Actions.Add(ts1); tb.Actions.Add(ts2); // update json based on all actions (including teststeps and teststep attachemnts) json = tb.SaveActions(json); var xml = ""; /* getting xml for teststeps * xml = tb.GenerateXmlFromActions(); */ // create Test Case work item using all test steps: ts1 and ts2 var testCaseObject = _witClient.CreateWorkItemAsync(json, projectName, "Test Case").Result; int testCaseId = Convert.ToInt32(testCaseObject.Id); // get Test Case using all relations testCaseObject = _witClient.GetWorkItemAsync(testCaseId, null, null, WorkItemExpand.Relations).Result; // update Test Case if (testCaseObject.Fields.ContainsKey("Microsoft.VSTS.TCM.Steps")) { xml = testCaseObject.Fields["Microsoft.VSTS.TCM.Steps"].ToString(); tb = helper.Create(); // create tcmattachemntlink object from workitem relation, teststep helper will use this IList <TestAttachmentLink> tcmlinks = new List <TestAttachmentLink>(); foreach (WorkItemRelation rel in testCaseObject.Relations) { TestAttachmentLink tcmlink = new TestAttachmentLink(); tcmlink.Url = rel.Url; tcmlink.Attributes = rel.Attributes; tcmlink.Rel = rel.Rel; tcmlinks.Add(tcmlink); } // load teststep xml and attachemnt links tb.LoadActions(xml, tcmlinks); ITestStep ts; //updating 1st test step ts = (ITestStep)tb.Actions[0]; ts.Title = "title -> title11"; ts.ExpectedResult = "expected11"; //removing 2ns test step tb.Actions.RemoveAt(1); //adding new test step ITestStep ts3 = tb.CreateTestStep(); ts3.Title = "title -> title3"; ts3.ExpectedResult = "expected3"; tb.Actions.Add(ts3); JsonPatchDocument json2 = new JsonPatchDocument(); // update json based on all new changes ( updated step xml and attachments) json2 = tb.SaveActions(json2); // update testcase wit using new json testCaseObject = _witClient.UpdateWorkItemAsync(json2, testCaseId).Result; /* Note : If you want to remove attachment then create new patchOperation, details are available here : * https://www.visualstudio.com/en-us/docs/integrate/api/wit/work-items#remove-an-attachment */ } }
public JsonPatchDocument BuildJsonPatchDocument(TestCase testCase) { var jsonPatchDocument = new JsonPatchDocument(); var jsonPatchOperations = new List <JsonPatchOperation>(); TestBaseHelper helper = new TestBaseHelper(); ITestBase testBase = helper.Create(); testCase.Steps.ForEach(x => { ITestStep testStep = testBase.CreateTestStep(); testStep.Title = x.Action ?? string.Empty; testStep.ExpectedResult = x.ExpectedResult ?? string.Empty; testStep.Description = x.Action ?? string.Empty; testBase.Actions.Add(testStep); }); var properties = typeof(TestCase).GetProperties(); foreach (var propertyInfo in properties.Where(x => x.Name != "Steps")) { object[] attrs = propertyInfo.GetCustomAttributes(true); foreach (object attr in attrs) { AzDevFieldReferenceAttribute azDevFieldReferenceAttribute = attr as AzDevFieldReferenceAttribute; if (azDevFieldReferenceAttribute != null) { var data = testCase.GetType().GetProperty(propertyInfo.Name).GetValue(testCase, null); if (propertyInfo.Name.Contains("AttachmentReferences")) { var castedData = data as List <AttachmentReference>; castedData.ForEach(x => { jsonPatchOperations.Add(new JsonPatchOperation { Operation = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add, Path = azDevFieldReferenceAttribute.AzDevFieldReference, Value = new { rel = "AttachedFile", url = x.Url } }); }); } else { if (!string.IsNullOrWhiteSpace(data as string)) { jsonPatchOperations.Add(new JsonPatchOperation { Operation = Microsoft.VisualStudio.Services.WebApi.Patch.Operation.Add, Path = azDevFieldReferenceAttribute.AzDevFieldReference, Value = data }); } } } } } jsonPatchDocument.AddRange(jsonPatchOperations); jsonPatchDocument = testBase.SaveActions(jsonPatchDocument); return(jsonPatchDocument); }