diff --git a/Behavioral.Automation.Transformations/Behavioral.Automation.Transformations.csproj b/Behavioral.Automation.Transformations/Behavioral.Automation.Transformations.csproj new file mode 100644 index 00000000..ed255eff --- /dev/null +++ b/Behavioral.Automation.Transformations/Behavioral.Automation.Transformations.csproj @@ -0,0 +1,21 @@ + + + + net6.0 + enable + enable + Debug;Release;Test;Dev;Prod + AnyCPU + + + + + + + + + + + + + diff --git a/Behavioral.Automation.Transformations/ScribanFunctions/ConfigFunction.cs b/Behavioral.Automation.Transformations/ScribanFunctions/ConfigFunction.cs new file mode 100644 index 00000000..db89e391 --- /dev/null +++ b/Behavioral.Automation.Transformations/ScribanFunctions/ConfigFunction.cs @@ -0,0 +1,12 @@ +using Behavioral.Automation.Configs; +using Scriban.Runtime; + +namespace Behavioral.Automation.Transformations.ScribanFunctions; + +public class ConfigFunction : ScriptObject +{ + public static string Config(string configName) + { + return ConfigManager.GetConfig(configName); + } +} \ No newline at end of file diff --git a/Behavioral.Automation.Transformations/Transformations.cs b/Behavioral.Automation.Transformations/Transformations.cs new file mode 100644 index 00000000..db60d4cd --- /dev/null +++ b/Behavioral.Automation.Transformations/Transformations.cs @@ -0,0 +1,82 @@ +using Behavioral.Automation.Transformations.ScribanFunctions; +using Scriban; +using Scriban.Runtime; +using TechTalk.SpecFlow; +using TechTalk.SpecFlow.Infrastructure; + +namespace Behavioral.Automation.Transformations; + +[Binding] +public class Transformations +{ + private readonly ConfigFunction _configFunction; + private readonly ScenarioContext _scenarioContext; + private static readonly List customFunctions = new List(); + private readonly SpecFlowOutputHelper _specFlowOutputHelper; + + public Transformations(ConfigFunction configFunction, ScenarioContext scenarioContext, SpecFlowOutputHelper specFlowOutputHelper) + { + _configFunction = configFunction; + _scenarioContext = scenarioContext; + _specFlowOutputHelper = specFlowOutputHelper; + } + + public static void AddFunction(IScriptObject function) + { + customFunctions.Add(function); + } + + [StepArgumentTransformation] + public string ProcessStringTransformations(string value) + { + var specflowContextVariables = new ScriptObject(); + if (_scenarioContext.Any()) + { + foreach (var item in _scenarioContext) + { + specflowContextVariables.Add(item.Key, item.Value); + } + } + + var context = new TemplateContext(); + + context.PushGlobal(specflowContextVariables); + context.PushGlobal(_configFunction); + foreach (var customFunction in customFunctions) + { + context.PushGlobal(customFunction); + } + + var template = Template.Parse(value); + var result = template.Render(context); + return result; + } + + [StepArgumentTransformation] + public Table ProcessTableTransformations(Table table) + { + var newTable = table; + if (table.Rows.Count > 0) + { + foreach (var row in newTable.Rows) + { + foreach (var value in row) + { + var template = Template.Parse(value.Value); + var result = template.Render(); + row[value.Key] = result; + } + } + } + + return newTable; + } + + [Given("save variable \"(.*)\" with value:")] + [Given("save variable \"(.*)\" with value \"(.*)\"")] + public void SaveStringIntoContext(string variableName, string stringToSave) + { + _scenarioContext.Add(variableName, stringToSave); + _specFlowOutputHelper.WriteLine($"Saved '{stringToSave}' with key '{variableName}' in scenario context"); + } +} \ No newline at end of file diff --git a/Behavioral.Automation.sln b/Behavioral.Automation.sln index 59f0bd7a..2408867b 100644 --- a/Behavioral.Automation.sln +++ b/Behavioral.Automation.sln @@ -22,6 +22,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Behavioral.Automation.Selen EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Behavioral.Automation.Playwright", "Behavioral.Automation.Playwright", "{2A26C467-6A03-4942-9F9B-62F77F992F70}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Behavioral.Automation.Transformations", "Behavioral.Automation.Transformations\Behavioral.Automation.Transformations.csproj", "{14BB98C2-0A86-4E70-A011-038D9DB00A87}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -56,6 +58,10 @@ Global {7460A60F-85B2-4DCE-B190-000503AE8550}.Debug|Any CPU.Build.0 = Debug|Any CPU {7460A60F-85B2-4DCE-B190-000503AE8550}.Release|Any CPU.ActiveCfg = Release|Any CPU {7460A60F-85B2-4DCE-B190-000503AE8550}.Release|Any CPU.Build.0 = Release|Any CPU + {14BB98C2-0A86-4E70-A011-038D9DB00A87}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {14BB98C2-0A86-4E70-A011-038D9DB00A87}.Debug|Any CPU.Build.0 = Debug|Any CPU + {14BB98C2-0A86-4E70-A011-038D9DB00A87}.Release|Any CPU.ActiveCfg = Release|Any CPU + {14BB98C2-0A86-4E70-A011-038D9DB00A87}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE