Untitled

 avatar
unknown
plain_text
3 months ago
2.4 kB
5
Indexable
using TechTalk.SpecFlow;
using NUnit.Framework;
using Allure.Commons;
using System;
using SAPAutomation.utils;

namespace SAPAutomation.steps
{
    [Binding]
    public class Hooks
    {
        private readonly ScenarioContext _scenarioContext;
        private SapHelper sapHelper = new SapHelper();
        private const string TestUuidKey = "allureTestUuid"; // Store 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()
            };

            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;
            }

            if (_scenarioContext.TestError != null)
            {
                AllureLifecycle.Instance.UpdateTestCase(testUuid, tc =>
                {
                    tc.status = Status.failed;
                    tc.statusDetails = new StatusDetails { message = _scenarioContext.TestError.Message };
                });
            }
            else
            {
                AllureLifecycle.Instance.UpdateTestCase(testUuid, tc =>
                {
                    tc.status = Status.passed;
                });
            }

            // Stop and write the test case using the UUID
            AllureLifecycle.Instance.StopTestCase(testUuid);
            AllureLifecycle.Instance.WriteTestCase(testUuid);

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