{"id":25105,"date":"2016-11-02T18:10:48","date_gmt":"2016-11-02T22:10:48","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/visualstudioalm\/?p=25105"},"modified":"2019-02-14T15:56:08","modified_gmt":"2019-02-14T23:56:08","slug":"how-to-use-test-step-using-rest-client-helper","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/devops\/how-to-use-test-step-using-rest-client-helper\/","title":{"rendered":"How to use Test Step using REST Client Helper?"},"content":{"rendered":"<p>Test Case is the backbone for all manual testing scenarios. You can create test case using the web client from Test or Work hubs OR from Microsoft Test Manager (MTM), which then are stored in Team Foundation Server or Visual Studio Team Services. Using these clients you can create test artifacts such as test cases with <em>test steps, test step attachments, shared steps, parameters, shared parameter.\u00a0<\/em>Test case is also a work item\u00a0and using <a target=\"_blank\" href=\"https:\/\/www.visualstudio.com\/en-us\/docs\/integrate\/api\/wit\/overview\">Work Item\u00a0REST API<\/a> support one can create a work item of type test case, see here: <a target=\"_blank\" href=\"https:\/\/www.visualstudio.com\/en-us\/docs\/integrate\/api\/wit\/work-items#create-a-work-item\">Create a work item<\/a>.<\/p>\n<h4>Problem<\/h4>\n<p>Until this release, there is no support to modify\/update test steps in a test case work item. Work item saves test steps, associated test step attachment, or expected results in custom XML document, and there is a need of helper to create that custom XML for test steps updates.<\/p>\n<h4>Solution<\/h4>\n<p>With the\u00a0<a target=\"_blank\" href=\"https:\/\/www.visualstudio.com\/en-us\/articles\/news\/2016\/nov-02-team-services#rest-client-helpers-for-test-step-operations\">current\u00a0deployment<\/a>, we have added support to Create\/Read\/Update\/Delete test step (action, and expected result)\u00a0and test step attachments.\u00a0ITestBase interface exposes required key method &#8211; loadActions and saveActions that\u00a0provide helper methods\u00a0for both in C# and JS to do above mentioned operations.<\/p>\n<h4>Requirement<\/h4>\n<p>C# Client (<a target=\"_blank\" href=\"https:\/\/www.nuget.org\/packages\/Microsoft.TeamFoundationServer.Client\/15.106.0-preview\">Microsoft.TeamFoundationServer.Client<\/a>)\u00a0as released in\u00a0<a target=\"_blank\" href=\"https:\/\/www.visualstudio.com\/en-us\/articles\/news\/2016\/oct-12-team-services\">previous deployment<\/a>.\nOR\nJS Client (<a target=\"_blank\" href=\"https:\/\/github.com\/Microsoft\/vss-web-extension-sdk\">vss-sdk-extension<\/a>) (<strong>Note<\/strong>:\u00a0JS changes will be available only after <a target=\"_blank\" href=\"https:\/\/www.visualstudio.com\/en-us\/articles\/news\/2016\/nov-02-team-services\">current deployment<\/a> completes.)<\/p>\n<h4>Walk through using new helper in C# client<\/h4>\n<p>Here, let&#8217;s walk through step-by-step on how to consume these newly added helper classes. We have also added GitHub sample for the same with some more operations (link given at the bottom of the post).<\/p>\n<ol>\n<li>Create an instance of TestBaseHelper class and generate ITestBase object using that.\n<pre class=\"code\" style=\"padding: 9.5px;margin: 0 0 10px;font-size: 13px;line-height: 1.42857143;color: #333;background-color: #f5f5f5;border: 1px solid #ccc\">TestBaseHelper helper = new TestBaseHelper();\nITestBase testBase = helper.Create();\n<\/pre>\n<\/li>\n<li>ITestBase exposes methods for create test step, generate xml, save actions and load actions. You can even assign title, set expected result and description with each test step and associate attachment using attachment URL. In the end, all test steps are added to actions associated with testBase object (see below).\n<pre class=\"code\" style=\"padding: 9.5px;margin: 0 0 10px;font-size: 13px;line-height: 1.42857143;color: #333;background-color: #f5f5f5;border: 1px solid #ccc\">ITestStep testStep1 = testBase.CreateTestStep();\ntestStep1.Title = \"title1\";\ntestStep1.ExpectedResult = \"expected1\";\ntestStep1.Description = \"description1\";\ntestStep1.Attachments.Add(testStep1.CreateAttachment(attachmentObject.Url, \"attachment1\"));\n\ntestBase.Actions.Add(testStep1)\n<\/pre>\n<\/li>\n<li>A call to SaveActions uses the helper classes and calls appropriate field setting of the test case &#8211; Test Steps to save newly added steps, expected result and attachment links. A JSON patch document created using &#8220;SaveActions&#8221; is used to createWorkItemAsync as shown below.\n<pre class=\"code\" style=\"padding: 9.5px;margin: 0 0 10px;font-size: 13px;line-height: 1.42857143;color: #333;background-color: #f5f5f5;border: 1px solid #ccc\">JsonPatchDocument json = new JsonPatchDocument();\n\n\/\/ create a title field\nJsonPatchOperation patchDocument1 = new JsonPatchOperation();\npatchDocument1.Operation = Operation.Add;\npatchDocument1.Path = \"\/fields\/System.Title\";\npatchDocument1.Value = \"New Test Case\";\njson.Add(patchDocument1);\n\n\/\/ add test steps in json\n\/\/ it will update json document based on test steps and attachments\njson = testBase.SaveActions(json);\n\n\/\/ create a test case\nvar testCaseObject = _witClient.CreateWorkItemAsync(json, projectName, \"Test Case\").Result;\n<\/pre>\n<\/li>\n<li>To modify a test case and its steps, you need to\u00a0get the test case and just call &#8220;LoadAction&#8221; which internally uses helper class to parse the given xml and attachmentlinks as shown below. This will populate the testBase class with all details as appropriate.\n<pre class=\"code\" style=\"padding: 9.5px;margin: 0 0 10px;font-size: 13px;line-height: 1.42857143;color: #333;background-color: #f5f5f5;border: 1px solid #ccc\">testCaseObject = _witClient.GetWorkItemAsync(testCaseId, null, null, WorkItemExpand.Relations).Result;\n\n\/\/ initiate testbase object again\ntestBase = helper.Create();\n\n\/\/ fetch xml from testcase object\nvar xml = testCaseObject.Fields[\"Microsoft.VSTS.TCM.Steps\"].ToString();\n\n\/\/ create tcmattachemntlink object from workitem relation, teststep helper will use this\nIList tcmlinks= new List();\nforeach (WorkItemRelation rel in testCaseObject.Relations)\n{\n    TestAttachmentLink tcmlink = new TestAttachmentLink();\n    tcmlink.Url = rel.Url;\n    tcmlink.Attributes = rel.Attributes;\n    tcmlink.Rel = rel.Rel;\n    tcmlinks.Add(tcmlink);\n}\n\n\/\/ load teststep xml and attachemnt links\ntestBase.LoadActions(xml, tcmlinks);\n<\/pre>\n<\/li>\n<li>Once testBase object has been loaded with test case information, you can update test steps and attachments in the\u00a0test case object.\n<pre class=\"code\" style=\"padding: 9.5px;margin: 0 0 10px;font-size: 13px;line-height: 1.42857143;color: #333;background-color: #f5f5f5;border: 1px solid #ccc\">ITestStep testStep;\n\/\/updating 1st test step\ntestStep = (ITestStep)testBase.Actions[0];\ntestStep.Title = \"New Title\";\ntestStep.ExpectedResult = \"New expected result\";\n\n\/\/removing 2nd test step\ntestBase.Actions.RemoveAt(1);\n\n\/\/adding new test step\nITestStep testStep3 = tb.CreateTestStep();\ntestStep3.Title = \"Title 3\";\ntestStep3.ExpectedResult = \"Expected 3\";\ntestBase.Actions.Add(testStep3);\n<\/pre>\n<\/li>\n<li>Update test case object using new changes in the test steps and attachments.\n<pre class=\"code\" style=\"padding: 9.5px;margin: 0 0 10px;font-size: 13px;line-height: 1.42857143;color: #333;background-color: #f5f5f5;border: 1px solid #ccc\">JsonPatchDocument json2 = new JsonPatchDocument();\njson2 = testBase.SaveActions(json2);\n\/\/ update testcase wit using new json\ntestCaseObject = _witClient.UpdateWorkItemAsync(json2, testCaseId).Result;\n<\/pre>\n<\/li>\n<\/ol>\n<p>As shown above, you can now use the helper classes provided to update test case steps, and still use the existing Work Item REST APIs for test case work item. You can find comprehensive samples\u00a0for both C# and JS here on GitHub project: <a target=\"_blank\" href=\"https:\/\/github.com\/pankagar\/RESTApi-Sample\">RESTApi-Sample<\/a>.<\/p>\n<ul>\n<li>Test Management Team<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Test Case is the backbone for all manual testing scenarios. You can create test case using the web client from Test or Work hubs OR from Microsoft Test Manager (MTM), which then are stored in Team Foundation Server or Visual Studio Team Services. Using these clients you can create test artifacts such as test cases [&hellip;]<\/p>\n","protected":false},"author":217,"featured_media":45953,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[227,253,1,252],"tags":[],"class_list":["post-25105","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-agile","category-azure-devops-server","category-devops","category-testing"],"acf":[],"blog_post_summary":"<p>Test Case is the backbone for all manual testing scenarios. You can create test case using the web client from Test or Work hubs OR from Microsoft Test Manager (MTM), which then are stored in Team Foundation Server or Visual Studio Team Services. Using these clients you can create test artifacts such as test cases [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/25105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/users\/217"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/comments?post=25105"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/25105\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media\/45953"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media?parent=25105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/categories?post=25105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/tags?post=25105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}