Untitled
unknown
plain_text
2 months ago
3.5 kB
7
Indexable
using Allure.Commons; using System; using TechTalk.SpecFlow; using SAPAutomation.utils; namespace SAPAutomation.steps { [Binding] // ✅ Required for SpecFlow to detect Hooks! public class Hooks { private readonly ScenarioContext _scenarioContext; private static FeatureContext _featureContext; // **Make this static to persist across scenarios** public Hooks(ScenarioContext scenarioContext) { _scenarioContext = scenarioContext; } // ✅ [BeforeTestRun] MUST be static [BeforeTestRun] public static void BeforeTestRun() { Console.WriteLine("[DEBUG] Running BeforeTestRun Hook."); AllureLifecycle.Instance.CleanupResultDirectory(); } // ✅ [BeforeFeature] MUST be static and must store FeatureContext [BeforeFeature] public static void BeforeFeature(FeatureContext featureContext) { _featureContext = featureContext; // ✅ Ensure FeatureContext is assigned Console.WriteLine($"[DEBUG] Running BeforeFeature Hook: {featureContext.FeatureInfo.Title}"); if (!_featureContext.ContainsKey("Allure.Net.Commons.AllureContext")) { string containerId = Guid.NewGuid().ToString(); _featureContext["Allure.Net.Commons.AllureContext"] = containerId; var container = new TestResultContainer { uuid = containerId, name = _featureContext.FeatureInfo.Title }; AllureLifecycle.Instance.StartTestContainer(container); Console.WriteLine($"[DEBUG] Created Allure Test Container: {containerId}"); } else { Console.WriteLine("[WARNING] AllureContext already exists."); } } // ❌ [BeforeScenario] should NOT be static [BeforeScenario] public void BeforeScenario() { Console.WriteLine($"[DEBUG] Running BeforeScenario Hook: {_scenarioContext.ScenarioInfo.Title}"); if (_featureContext == null) { Console.WriteLine("[ERROR] FeatureContext is NULL in BeforeScenario! This should not happen."); throw new Exception("FeatureContext was not initialized in BeforeFeature!"); } if (!_featureContext.TryGetValue("Allure.Net.Commons.AllureContext", out string featureContainerId)) { Console.WriteLine("[WARNING] AllureContext missing! Initializing..."); featureContainerId = Guid.NewGuid().ToString(); _featureContext["Allure.Net.Commons.AllureContext"] = featureContainerId; } string testUuid = Guid.NewGuid().ToString(); _scenarioContext["AllureTestUuid"] = testUuid; var testResult = new TestResult { uuid = testUuid, name = _scenarioContext.ScenarioInfo.Title, start = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() }; AllureLifecycle.Instance.StartTestCase(testResult); AllureLifecycle.Instance.UpdateTestContainer(featureContainerId, container => { container.children.Add(testUuid); }); } } }
Editor is loading...
Leave a Comment