How to update Ranorex Test evidence and status automatically into JIRA X-Ray Test execution using C#?

Yosuva ArulanthuRanorexLeave a Comment

I have used RestSharp package for the below c# example

/*
 * User: Yosuva
 * Date: 31/03/2020
 * Time: 09:38
 */
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Threading;
using WinForms = System.Windows.Forms;

using Ranorex;
using Ranorex.Core;
using Ranorex.Core.Testing;
using RestSharp;
using RestSharp.Authenticators;
namespace MyTest1
{
    /// <summary>
    /// Description of UpdateJIRA.
    /// </summary>
    [TestModule("F4A65631-284B-424A-88DC-A3F23D0AC96A", ModuleType.UserCode, 1)]
    public class UpdateJIRA : ITestModule
    {
        /// <summary>
        /// Constructs a new instance.
        /// </summary>
        public UpdateJIRA()
        {
            // Do not delete - a parameterless constructor is required!
        }

        /// <summary>
        /// Performs the playback of actions in this module.
        /// </summary>
        /// <remarks>You should not call this method directly, instead pass the module
        /// instance to the <see cref="TestModuleRunner.Run(ITestModule)"/> method
        /// that will in turn invoke this method.</remarks>
        void ITestModule.Run()
        {
            Mouse.DefaultMoveTime = 300;
            Keyboard.DefaultKeyPressTime = 100;
            Delay.SpeedFactor = 1.0;
            
            var curTestCase = (TestCaseNode)TestSuite.CurrentTestContainer;
            string tcName = curTestCase.Name;
            var tcStatus = TestSuite.Current.GetTestContainer(tcName).Status;
            string jira_ststus=tcStatus.ToString();
            if (jira_ststus.Equals("Success"))
            {
                jira_ststus="PASS";
            }
            else
            {
                jira_ststus="FAIL";
            }
            string format = "yyyy-MM-dd'T'HH:mm:sszzz"; 
            string dtNow  = System.DateTime.Now.ToString (format);
            
            Byte[] bytes = File.ReadAllBytes(ReportToPDF.ReportToPDF.PDFpath);
        String file = Convert.ToBase64String(bytes);
//code to update just status        
//            var client = new RestClient("https://jira.example.com/rest/raven/1.0/import/execution");
//client.Timeout = -1;
//var request = new RestRequest(Method.POST);
//request.AddHeader("Authorization", "Basic <encoded credentials>");
//request.AddHeader("Content-Type", "application/json");
//request.AddParameter("application/json", "{\r\n    \"testExecutionKey\": \"JOS-1\",\r\n\t    " +
//                     "\"tests\" : [\r\n\t        {\r\n\t            " +
//                     "\"testKey\" : \""+tcName+"\",\r\n\t            " +
//                     "\"start\" : \""+dtNow+"\",\r\n\t            " +
//                     "\"finish\" : \""+dtNow+"\",\r\n\t            " +
//                     "\"comment\" : \"Executed through automation with logs attached\",\r\n\t            " +
//                     "\"status\" : \""+tcStatus+"\"\r\n\t        }\r\n\t     ]\r\n}",  ParameterType.RequestBody);
//IRestResponse response = client.Execute(request);
//Console.WriteLine(response.Content);


//code to update test execution with evidences
var client = new RestClient("https://jira.example.com/rest/raven/1.0/import/execution");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Basic <encoded credentials>");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\r\n    \"testExecutionKey\": \"JOS-1\",\r\n    " +
                     "\"tests\" : [\r\n        {\r\n            " +
                     "\"testKey\" : \""+tcName+"\",\r\n            " +
                     "\"start\" : \""+dtNow+"\",\r\n            " +
                     "\"finish\" : \""+dtNow+"\",\r\n            " +
                     "\"comment\" : \"Executed through automation with logs attached\",\r\n            " +
                     "\"status\" : \""+jira_ststus+"\",\r\n            " +
                     "\"evidences\" : [\r\n                        {\r\n                            " +
                     "\"data\": \""+file+"\",\r\n                            " +
                     "\"filename\": \"pdfreport.pdf\",\r\n                            " +
                     "\"contentType\": \"application/pdf\"\r\n                        " +
"}\r\n                    ]\r\n        }\r\n     ]\r\n}\r\n",  ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);

Report.LogHtml(ReportLevel.Success,"JIRA Status"+response.Content);
Report.LogHtml(ReportLevel.Success,"JIRA Status"+tcStatus);

        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *