Is there any way to get the Target name defined in specflow's srs profile ?
Below is what is defined in srs profile,
<Targets>
<Target name="Login">
<Filter>#login</Filter>
</Target>
</Targets>
I want to get the name of the target "Login" and save it in a variable in C#.
We don't have an API to get the target name, but you can get to it with a little bit more stuff in the srProfile.
This is how you do it:
<?xml version="1.0" encoding="utf-8"?>
<TestProfile xmlns="http://www.specflow.org/schemas/plus/TestProfile/1.5">
<Targets>
<Target name="Target1">
<DeploymentTransformationSteps>
<EnvironmentVariable variable="RUNNER_TARGET" value="Target1" />
</DeploymentTransformationSteps>
</Target>
<Target name="Target2">
<DeploymentTransformationSteps>
<EnvironmentVariable variable="RUNNER_TARGET" value="Target2" />
</DeploymentTransformationSteps>
</Target>
</Targets>
</TestProfile>
You specify a deployment transformation step which sets an environment variable to the name of the target.
In your bindings, you can get the value of the environment variable by normal .NET APIs.
var targetName = Environment.GetEnvironmentVariable("RUNNER_TARGET");
You can find a complete example here: https://github.com/SpecFlowOSS/SpecFlow.Plus.Examples/tree/master/AccessTargetName
Full disclosure: I am one of the developers of SpecFlow and SpecFlow+
Related
Documentation says:
Sometimes we require ALL targets to be wrapped in the same way, for example to add buffering and/or retrying. NLog provides <default-wrapper /> syntax for that. You simply put this element in the <targets /> section and all your targets will be automatically wrapped with the specified wrapper.
It also provides the following example:
<nlog>
<targets>
<default-wrapper xsi:type="BufferingWrapper" bufferSize="100"/>
<target name="f1" xsi:type="File" fileName="f1.txt"/>
<target name="f2" xsi:type="File" fileName="f2.txt"/>
</targets>
<targets>
<default-wrapper xsi:type="AsyncWrapper">
<wrapper-target xsi:type="RetryingWrapper"/>
</default-wrapper>
<target name="n1" xsi:type="Network" address="tcp://localhost:4001"/>
<target name="n2" xsi:type="Network" address="tcp://localhost:4002"/>
<target name="n3" xsi:type="Network" address="tcp://localhost:4003"/>
</targets>
</nlog>
So, <default-wrapper /> element should be a child of <targets> to be applied to each <target> within the same parent.
On the other hand, Extended Json NLog Config Example uses default-wrapper on the same level as targets element:
"default-wrapper": {
"type": "AsyncWrapper",
"overflowAction": "Block"
},
"targets": {
So, I have the following questions:
Is the Extended Json NLog Config Example correct?
Will all targets from targets element be wrapped with the default-wrapper, placed out of the targets?
What if there are default-wrapper within targets and default-wrapper out of targets in the same configuration file?
Yes there was some discussion about whether XML-config and JSON-config should be completely 1-to-1. See also: https://github.com/NLog/NLog.Extensions.Logging/pull/283
Because JSON-config represented targets-section as a dictionary of known target-names, then it didn't feel natural to have default-wrapper and default-target-parameters as reserved magic-strings.
Instead they were moved out of targets-section, as you have discovered from the documentation.
Yes all items in the targets-section will be wrapped, when using default-wrapper.
Have not tested what happens if placing default-wrapper and default-target-parameters inside the targets-section-dictionary. Maybe check the NLog InternalLogger whether it complains about unknown configuration-items.
Notice more work is being done to give default-wrapper and default-target-parameters a better name, since it might not be easy to guess that they both apply to the targets-section. See also: https://github.com/NLog/NLog.Extensions.Logging/pull/500
I have seen the question here How to execute before and after test run only once in parallel execution of specflow - although I am unable to understand how to run the Hooks just once in parallel
I am using Specflow+ Runner as my test provider, my thread count is 3, I have a BeforeTestRun hook that creates my test data, I want to execute that Hook only once, but by default BeforeTestRun is ran on each thread, I have also looked at the example here:
https://github.com/SpecFlowOSS/SpecFlow.Plus.Examples/tree/master/CustomDeploymentSteps,
But I am unsure how it works, could anyone help me out on this one.
[BeforeTestRun()]
public static void BeforeTestRun()
{
Console.WriteLine("test");
}
Output is:
test
test
test
My Default.srprofile looks like this
<?xml version="1.0" encoding="utf-8"?>
<TestProfile xmlns="http://www.specflow.org/schemas/plus/TestProfile/1.5">
<Settings projectName="TestAutoProject" />
<Execution stopAfterFailures="2" testThreadCount="3" testSchedulingMode="Sequential" />
<TestAssemblyPaths>
<TestAssemblyPath>Test.dll</TestAssemblyPath>
</TestAssemblyPaths>
<Targets>
<Target name="IE">
<Filter>Browser_IE</Filter>
<DeploymentTransformationSteps>
<EnvironmentVariable variable="Test_Browser" value="IE" />
</DeploymentTransformationSteps>
</Target>
<Target name="Chrome">
<Filter>Browser_Chrome</Filter>
<DeploymentTransformationSteps>
<EnvironmentVariable variable="Test_Browser" value="Chrome" />
</DeploymentTransformationSteps>
</Target>
<Target name="Firefox">
<Filter>Browser_Firefox</Filter>
<DeploymentTransformationSteps>
<EnvironmentVariable variable="Test_Browser" value="Firefox" />
</DeploymentTransformationSteps>
</Target>
</Targets>
<DeploymentTransformation>
<Steps></Steps>
</DeploymentTransformation>
</TestProfile>
Just started working with NLog and have it running with the following configuration:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
throwConfigExceptions="true">
<targets async="true">
<target name="logfile"
xsi:type="File"
layout="${longdate} [${level:uppercase=true}] (${threadid}) ${logger}: ${message} ${onexception:${newline}Exception\: ${exception:format=type,message,method,stacktrace:maxInnerExceptionLevel=5:innerFormat=shortType,message,method}}"
fileName="logs/current.log"
archiveFileName="logs/Archive/${ticks}.log"
archiveEvery="Minute"
archiveOldFileOnStartup="true"
keepFileOpen="false"
/>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>
</nlog>
Everything is working as expected. However, I need to have the rotated file be in the format of ${archive_start_ticks}_${arhive_end_ticks}.log rather than the current format which is ${archive_end_ticks}.log.
I was initially hoping I could name the active log file as ${ticks} and then, on archive, use the active log file's name as a parameter into the archive file to compose some like:
fileName="logs/${ticks}"
archiveFileName="logs/Archive/${fileName}_${ticks}.log"
Of course, there's two issues here:
Using ${ticks} for the active file creates a new file for each log line.
I can't seem to reference the original fileName as an input variable into archiveFileName.
That said, what is the best way to achieve this goal? Is this something NLog can handle natively or with minor extensions?
Updating in case anyone ever cares:
I bailed on using the FileTarget with configurations and wrote my own Target wrapped in a BufferedWrapper. On each flush, I use the first and last LogEvents to determine the timespan which gives me what I need to for the required file format with little custom code to support.
I have a VS 2010 solution that uses config transforms like so:
<Target Name="AfterBuild">
<TransformXml Condition="'$(IsDesktopBuild)' == 'false'" Source="Web.config" Transform="$(ProjectConfigTransformFileName)" Destination="$(PublishDir)\_PublishedWebsites\$(ProjectName)\Web.config" />
<Delete Files="$(PublishDir)\_PublishedWebsites\$(ProjectName)\Web.Debug.config" Condition="'$(IsDesktopBuild)' == 'false'" />
<Delete Files="$(PublishDir)\_PublishedWebsites\$(ProjectName)\Web.Release.config" Condition="'$(IsDesktopBuild)' == 'false'" />
<Delete Files="$(PublishDir)\_PublishedWebsites\$(ProjectName)\Web.Something.config" Condition="'$(IsDesktopBuild)' == 'false'" />
<Delete Files="$(PublishDir)\_PublishedWebsites\$(ProjectName)\Web.Else.config" Condition="'$(IsDesktopBuild)' == 'false'" />
<AspNetCompiler Condition="('$(IsDesktopBuild)' != 'false') AND ('$(MvcBuildViews)'=='true')" VirtualPath="temp" PhysicalPath="$(ProjectDir)\..\$(ProjectName)" />
<AspNetCompiler Condition="('$(IsDesktopBuild)' == 'false') AND ('$(MvcBuildViews)'=='true')" VirtualPath="temp" PhysicalPath="$(PublishDir)\_PublishedWebsites\$(ProjectName)" />
</Target>
This is in each website project (.csproj) in .sln.
When I run the v 4.0 MSBuild with a /p:PublishDir specified everything is fine. But when I run this as a Build Definition using Default template (GitTemplate.xaml) the folder for some reason resolves to $(ProjectDir), not the publish location. so the transform target is trying to copy files to the folder that doesn't exist ($(ProjectDir)\_PublishedWebsites\$(ProjectName)\Web.config).
If I manually specify the /p:PublishDir to a STATIC location on disk, the build succeeds. How can I tell the MSBuild where the PublishDir actually is?
I am happy to edit the workflow to assign a variable or something, I just don't see how its possible.
I also tried specifying relative paths (e.g. /p:PublishDir="..\..\bin\\") and the team build agent variables (same as we have configured for build paths, e.g. /p:PublishDir="D:\$(BuildAgentId)\$(BuildDefinitionId)\bin\\") to no avail - it seems that the variables are not unwrapped and relative path doesn't work.
Add something like this before the AfterBuild target:
<PropertyGroup>
<PublishDir>PATH_TO_YOUR_PUBLISH_DIR</PublishDir>
</PropertyGroup>
You should be able to use relative paths without problem:
<PropertyGroup>
<PublishDir>..\..\bin\</PublishDir>
</PropertyGroup>
Remember that the path will be relative to the current project file.
If you want to override it from the command line you can add a condition like this:
<PropertyGroup>
<PublishDir Condition="'$(PublishDir)'==''">PATH_TO_YOUR_PUBLISH_DIR</PublishDir> <!-- This will be the default value -->
</PropertyGroup>
Then call MsBuild with /p:PublishDir="Your_New_Path" to override the default value.
If you have a VS solution you could use something like this:
<PropertyGroup>
<PublishDir Condition="'$(PublishDir)'==''">$(SolutionDir)PublishDir</PublishDir> <!-- This will be the default value -->
</PropertyGroup>
This will use the directory where the solution is placed as root and will create a "PublishDir" folder there.
This will work only if you have a solution, off course you could define a default value in case there's no solution:
<PropertyGroup>
<SolutionDir Condition="'$(SolutionDir)'=='' Or '$(SolutionDir)'=='*Undefined*'">Your_Default_SolutionDir</SolutionDir>
<PublishDir Condition="'$(PublishDir)'==''">$(SolutionDir)PublishDir</PublishDir> <!-- This will be the default value -->
</PropertyGroup>
I'm working on an ASP.NET 4 WebAPI project and am including a wpp.targets file. I need to use MSBuild.ExtensionPack.Xml.XmlFile to replace a value in one of my configuration XML files.
The problem is that I don't want to install MSBuild.ExtensionPack on all the machines so I packaged it up with the project. On my local build, the path to the MSBuild.ExtensionPack.dll resolves correctly. On my build machine though, I keep getting this error: The "MSBuild.ExtensionPack.Xml.XmlFile" task could not be loaded from the assembly C:\Program Files (x86)\MSBuild\ExtensionPack\4.0\MSBuild.ExtensionPack.dll.
It seems to be resolving to the default install location of the package.
Here's what's in my wpp.targets file:
<?xml version="1.0" encoding="utf-8"?>
<!-- Sets the assembly which will run the transformation on Web.config (Should be installed on Dev machines) -->
<UsingTask TaskName="TransformXml"
AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v11.0\Web\Microsoft.Web.Publishing.Tasks.dll"/>
<!-- Get the path to the MSBuild.Extension.Pack -->
<PropertyGroup>
<TPath>$(MSBuildProjectDirectory)\..\packages\MSBuild.Extension.Pack.1.3.0\tools\net40\MSBuild.ExtensionPack.tasks</TPath>
<TPath Condition="Exists('$(MSBuildProjectDirectory)\..\packages\MSBuild.Extension.Pack.1.3.0\tools\net40\MSBuild.ExtensionPack.tasks')">$(MSBuildProjectDirectory)\..\packages\MSBuild.Extension.Pack.1.3.0\tools\net40\MSBuild.ExtensionPack.tasks</TPath>
</PropertyGroup>
<!--Import the MSBuild.Extension.Pack package -->
<Import Project="$(TPath)"/>
<!-- Make sure web.config and transformation files exist -->
<Target Name="ConfigurationTransform" BeforeTargets="BeforeBuild" Condition="Exists('Web.config')" />
<Target Name="ConfigurationTransform" BeforeTargets="BeforeBuild" Condition="Exists('Web.$(Configuration).config')" />
<!-- Make sure web.config will be there even for package/publish -->
<Target Name="CopyWebConfig" BeforeTargets="Build;Rebuild">
<Copy SourceFiles="Web.Base.config"
DestinationFiles="Web.config"
OverwriteReadOnlyFiles="true"
SkipUnchangedFiles="false" />
</Target>
<!-- Run Web.Config transformation on a build as well (not just a publish) -->
<Target Name="CustomTransformWebConfigOnBuild" AfterTargets="CopyWebConfig" >
<Message Text="Transforming: Web.$(Configuration).config" Importance="high" />
<TransformXml Source="Web.Base.config"
Transform="Web.$(Configuration).config"
Destination="Web.config" />
</Target>
<!-- Update Web.Config's config attribute -->
<Target Name="UpdateConfigAttribute" AfterTargets="CustomTransformWebConfigOnBuild" Condition="$(Configuration) != 'Release'">
<Message Text="Transforming: Web.config" Importance="high" />
<MSBuild.ExtensionPack.Xml.XmlFile TaskAction="UpdateAttribute"
File="Web.config"
XPath="/configuration/appSettings/add[#key='config_url']"
Key="value"
Value="www.randomurl.com"/>
</Target>
When you have more than 1 option for the source location for a helper-dll...I like to do it like with the style below.
Something like this (Obviously, you'll have to put real locations in for "PossibleLocationOne" and "PossibleLocationTwo").
<PropertyGroup>
<MyFoundMSBuildExtensionPackLocation Condition="Exists('..\PossibleLocationOne\MSBuild.ExtensionPack.dll')">..\PossibleLocationOne\MSBuild.ExtensionPack.dll</MyFoundMSBuildExtensionPackLocation>
<MyFoundMSBuildExtensionPackLocation Condition="Exists('..\PossibleLocationTwo\MSBuild.ExtensionPack.dll')">..\PossibleLocationTwo\MSBuild.ExtensionPack.dll</MyFoundMSBuildExtensionPackLocation>
<!--Now check to see if either of the two above resolved , if not , add something to the path so you can at least -->
<MyFoundMSBuildExtensionPackLocation Condition="'$(MyFoundMSBuildExtensionPackLocation)'==''">DID_NOT_FIND_A_PATH_FOR_MSBUILDEXENSIONPACK\MSBuild.ExtensionPack.dll</MyFoundMSBuildExtensionPackLocation>
</PropertyGroup>
<UsingTask AssemblyFile="$(MyFoundMSBuildExtensionPackLocation)" TaskName="TransformXml"/>
Add all options for possible locations..and one extra for "I didn't find a match"....
Then use the "UsingTask".
"UsingTask" is ~~after~~ the MyFoundMSBuildExtensionPackLocation(PropertyGroup).......so the $(MyFoundMSBuildExtensionPackLocation) resolves before the UsingTask is called.