Untitled

 avatar
unknown
plain_text
2 months ago
2.6 kB
6
Indexable
using System;
using TechTalk.SpecFlow;
using Allure.Commons;
using SAPAutomation.utils;

namespace SAPAutomation.steps
{
    [Binding]
    public class Hooks
    {
        private readonly ScenarioContext _scenarioContext;
        private readonly SapHelper sapHelper = new SapHelper();
        private const string TestUuidKey = "allureTestUuid"; // Key for storing the UUID in ScenarioContext

        public Hooks(ScenarioContext scenarioContext)
        {
            _scenarioContext = scenarioContext;
        }

        [BeforeScenario]
        public void BeforeScenario()
        {
            Console.WriteLine($"[Allure] Starting Test: {_scenarioContext.ScenarioInfo.Title}");

            string testUuid = Guid.NewGuid().ToString();
            _scenarioContext[TestUuidKey] = testUuid; // Store UUID in context

            var testResult = new TestResult
            {
                uuid = testUuid,
                name = _scenarioContext.ScenarioInfo.Title,
                start = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            };

            // Ensure Allure initializes correctly
            AllureLifecycle.Instance.StartTestCase(testResult);
        }

        [AfterScenario]
        public void AfterScenario()
        {
            Console.WriteLine($"[Allure] Finished Test: {_scenarioContext.ScenarioInfo.Title}");

            if (!_scenarioContext.TryGetValue(TestUuidKey, out string testUuid))
            {
                Console.WriteLine("[Allure] Error: Test UUID not found in ScenarioContext.");
                return;
            }

            // Update test result status
            AllureLifecycle.Instance.UpdateTestCase(testUuid, tc =>
            {
                tc.stop = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

                if (_scenarioContext.TestError != null)
                {
                    tc.status = Status.failed;
                    tc.statusDetails = new StatusDetails
                    {
                        message = _scenarioContext.TestError.Message,
                        trace = _scenarioContext.TestError.StackTrace
                    };
                }
                else
                {
                    tc.status = Status.passed;
                }
            });

            // Finalize test case in Allure
            AllureLifecycle.Instance.StopTestCase(testUuid);
            AllureLifecycle.Instance.WriteTestCase(testUuid);

            // Close the SAP session
            sapHelper.CloseSAP();
        }
    }
}
Editor is loading...
Leave a Comment