Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added draft of transformations #129

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Configurations>Debug;Release;Test;Dev;Prod</Configurations>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant Configurations

<Platforms>AnyCPU</Platforms>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Behavioral.Automation" Version="1.9.0" />
<PackageReference Include="Scriban" Version="5.4.1" />
<PackageReference Include="SpecFlow" Version="3.9.69" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Behavioral.Automation.Configs\Behavioral.Automation.Configs.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -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);
}
}
82 changes: 82 additions & 0 deletions Behavioral.Automation.Transformations/Transformations.cs
Original file line number Diff line number Diff line change
@@ -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<IScriptObject> customFunctions = new List<IScriptObject>();
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");
}
}
6 changes: 6 additions & 0 deletions Behavioral.Automation.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down