private async void UpdateSingleTestCaseTestStep(TestCase testCase)
        {
            string uri = _uri;
            string personalAccessToken = _personalAccessToken;
            string project             = _project;
            string credentials         = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", personalAccessToken)));

            //POST https://dev.azure.com/{organization}/{project}/_apis/testplan/Plans/{planId}/Suites/{suiteId}/TestCase?api-version=5.0-preview.2


            List <Object> patchDocument = new List <object>();

            TestStepTools xml   = new TestStepTools();
            string        steps = xml.CreateTestStepXML(testCase.TestSteps);

            //System.Diagnostics.Debug.WriteLine(steps);

            patchDocument.Add(new { op = "add", path = "/fields/Microsoft.VSTS.TCM.Steps", value = steps });

            patchDocument.Add(new { op = "add", path = "/fields/Avanade.ACM.TestDataSetup", value = testCase.PreCondition });

            HttpClientHandler handler = new HttpClientHandler();

            //serialize the fields array into a json string
            var patchValue = new StringContent(JsonConvert.SerializeObject(patchDocument), Encoding.UTF8, "application/json-patch+json");

            var requestUri = "/APHP/_apis/wit/workitems/" + testCase.TestCaseId + "?api-version=3.0";
            var method     = new HttpMethod("PATCH");
            var request    = new HttpRequestMessage(method, requestUri)
            {
                Content = patchValue
            };
            string requestTxt = await request.Content.ReadAsStringAsync();

            var response = await _client.SendAsync(request);

            //_logger.LogUriAndPackage(requestUri, requestTxt);
            string responseTxt = await response.Content.ReadAsStringAsync();

            //_logger.Log(responseTxt);

            //if the response is successfull, set the result to the workitem object
            if (response.IsSuccessStatusCode)
            {
                var workItem = await response.Content.ReadAsAsync <WorkItem>();

                Console.WriteLine("Test Case Steps successfully updated for Test Case ID {0}", workItem.Id);
            }
            else
            {
                Console.Write("Error creating Test Case: {0}", response.Content.ReadAsStringAsync().Result);
            }
        }
Exemple #2
0
        public async Task <WorkItem> CreateTestCaseWorkItemAndTestStep(TestCase testCase)
        {
            string uri = _uri;
            string personalAccessToken = _personalAccessToken;
            string project             = _project;
            string credentials         = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", personalAccessToken)));

            //POST https://dev.azure.com/{organization}/{project}/_apis/testplan/Plans/{planId}/Suites/{suiteId}/TestCase?api-version=5.0-preview.2


            List <Object> patchDocument = new List <object>();

            string iterationPath = "APHP Virginia\\VA SIT\\Iteration " + _iteration;

            patchDocument.Add(new { op = "add", path = "/fields/System.IterationPath", value = iterationPath });

            if (!string.IsNullOrEmpty(testCase.TestCaseName))
            {
                patchDocument.Add(new { op = "add", path = "/fields/System.Title", value = testCase.TestCaseName });
                //Console.Write(testCase.TestCaseName + "\r\n");
            }
            if (!string.IsNullOrEmpty(testCase.TestCaseType))
            {
                patchDocument.Add(new { op = "add", path = "/fields/APHP.ALM.TestCaseType", value = testCase.TestCaseType });
                //Console.Write(testCase.TestCaseType + "\r\n");
            }
            if (testCase.Priority != null)
            {
                patchDocument.Add(new { op = "add", path = "/fields/Microsoft.VSTS.Common.Priority", value = testCase.Priority });
                //Console.Write(testCase.Priority + "\r\n");
            }
            if (!string.IsNullOrEmpty(testCase.Complexity))
            {
                patchDocument.Add(new { op = "add", path = "/fields/Avanade.ACM.Complexity", value = testCase.Complexity });
                //Console.Write(testCase.Complexity + "\r\n");
            }
            if (!string.IsNullOrEmpty(testCase.ScenarioType))
            {
                patchDocument.Add(new { op = "add", path = "/fields/Avanade.ACM.ScenarioType", value = testCase.ScenarioType });
                //Console.Write(testCase.ScenarioType + "\r\n");
            }
            if (!string.IsNullOrEmpty(testCase.TestObjective))
            {
                patchDocument.Add(new { op = "add", path = "/fields/Avanade.ACM.TestObjective", value = testCase.TestObjective });
                //Console.Write(testCase.TestObjective + "\r\n");
            }
            if (!string.IsNullOrEmpty(testCase.TestDescription))
            {
                patchDocument.Add(new { op = "add", path = "/fields/System.Description", value = testCase.TestDescription });
                //Console.Write(testCase.TestDescription + "\r\n");
            }
            if (!string.IsNullOrEmpty(testCase.PreCondition))
            {
                patchDocument.Add(new { op = "add", path = "/fields/Avanade.ACM.TestDataSetup", value = testCase.PreCondition });
                //Console.Write(testCase.PreCondition + "\r\n");
            }
            if (!string.IsNullOrEmpty(testCase.Application))
            {
                patchDocument.Add(new { op = "add", path = "/fields/APHP.ALM.TestCaseApplication", value = testCase.Application });
                //Console.Write(testCase.Application + "\r\n");
            }
            if (!string.IsNullOrEmpty(testCase.ApplicationArea))
            {
                patchDocument.Add(new { op = "add", path = "/fields/Microsoft.VSTS.CMMI.ApplicationArea", value = testCase.ApplicationArea });
                //Console.Write(testCase.ApplicationArea + "\r\n");
            }
            if (!string.IsNullOrEmpty(testCase.ApplicationProcess))
            {
                patchDocument.Add(new { op = "add", path = "/fields/Microsoft.VSTS.CMMI.ApplicationProcess", value = testCase.ApplicationProcess });
                //Console.Write(testCase.ApplicationProcess + "\r\n");
            }
            if (!string.IsNullOrEmpty(testCase.ApplicationSubArea))
            {
                patchDocument.Add(new { op = "add", path = "/fields/APHP.ALM.TestCaseApplicationSubArea", value = testCase.ApplicationSubArea });
                //Console.Write(testCase.ApplicationSubArea + "\r\n");
            }
            if (!string.IsNullOrEmpty(testCase.UserName))
            {
                patchDocument.Add(new { op = "add", path = "/fields/Avanade.ACM.Author", value = testCase.UserName });
                //Console.Write(testCase.UserName + "\r\n");
            }

            TestStepTools xml   = new TestStepTools();
            string        steps = xml.CreateTestStepXML(testCase.TestSteps);

            //System.Diagnostics.Debug.WriteLine(steps);

            patchDocument.Add(new { op = "add", path = "/fields/Microsoft.VSTS.TCM.Steps", value = steps });

            HttpClientHandler handler = new HttpClientHandler();

            //using (var client = new HttpClient(handler, false))
            //{
            //    //set our headers
            //    client.BaseAddress = new Uri(uri);
            //    client.DefaultRequestHeaders.Accept.Clear();
            //    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            //    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);

            //serialize the fields array into a json string
            var patchValue = new StringContent(JsonConvert.SerializeObject(patchDocument), Encoding.UTF8, "application/json-patch+json");

            var requestUri = "/APHP/" + project + "/_apis/wit/workitems/$Test Case?api-version=3.0";
            var method     = new HttpMethod("PATCH");
            var request    = new HttpRequestMessage(method, requestUri)
            {
                Content = patchValue
            };
            string requestTxt = await request.Content.ReadAsStringAsync();

            var response = await _client.SendAsync(request);

            _logger.LogUriAndPackage(requestUri, requestTxt);
            string responseTxt = await response.Content.ReadAsStringAsync();

            _logger.Log(responseTxt);

            //if the response is successfull, set the result to the workitem object
            if (response.IsSuccessStatusCode)
            {
                var workItem = await response.Content.ReadAsAsync <WorkItem>();

                Console.WriteLine("Test Case Successfully Created: Test Case #{0}", workItem.Id);
                return(workItem);
            }
            else
            {
                Console.Write("Error creating Test Case: {0}", response.Content.ReadAsStringAsync().Result);
                return(null);
            }
            //}
        }