string
selectedBrowser = TestContext.Properties[
"__Tfs_TestConfigurationName__"
].ToString();
I'm on holiday and without the access to my pc. Still ill try to explain it further. Just create a ms unit test class in your project (the one decorated with unit test attribute), then add a method and set on it AssemblyInitialize attribute. Due to the ms test internals it will be always triggered before any test run, and context will be populated.
If I'm not clear I'll post a full example in two weeks time.
Cheers
--
You received this message because you are subscribed to a topic in the Google Groups "SpecFlow" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/specflow/WwOOPig_2_4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to specflow+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
using MtmContext.Generator.SpecflowPlugin;
using TechTalk.SpecFlow.Infrastructure;
[assembly: GeneratorPlugin(typeof(MyGeneratorPlugin))]
namespace MtmContext.Generator.SpecflowPlugin
{
using BoDi;
using System.CodeDom;
using TechTalk.SpecFlow.Generator;
using TechTalk.SpecFlow.Generator.Configuration;
using TechTalk.SpecFlow.Generator.Plugins;
using TechTalk.SpecFlow.Generator.UnitTestProvider;
using TechTalk.SpecFlow.Utils;
public class MsTestCustomGeneratorProvider : MsTest2010GeneratorProvider
{
/// <summary>
/// Initializes a new instance of the <see cref="MsTestCustomGeneratorProvider"/> class.
/// </summary>
/// <param name="codeDomHelper">
/// The code dom helper.
/// </param>
public MsTestCustomGeneratorProvider(CodeDomHelper codeDomHelper)
: base(codeDomHelper)
{
}
/// <summary>
/// The finalize test class.
/// </summary>
/// <param name="generationContext">
/// The generation context.
/// </param>
public override void FinalizeTestClass(TestClassGenerationContext generationContext)
{
base.FinalizeTestClass(generationContext);
var msTestContextGeneration =
new CodeExpressionStatement(
new CodeMethodInvokeExpression(
new CodeTypeReferenceExpression("testRunner.ScenarioContext"),
"Add",
new CodeExpression[]
{
new CodePrimitiveExpression("MSTestContext"),
new CodeArgumentReferenceExpression("TestContext")
}));
generationContext.ScenarioInitializeMethod.Statements.Add(msTestContextGeneration);
var msTestAssignment =
new CodeAssignStatement(
new CodeVariableReferenceExpression("myTestContext"),
new CodeVariableReferenceExpression("testContext"));
generationContext.TestClassInitializeMethod.Statements.Add(msTestAssignment);
var mstestContextFiled = new CodeMemberField
{
Attributes = MemberAttributes.Private | MemberAttributes.Static | MemberAttributes.Final,
Name = "myTestContext",
Type = new CodeTypeReference(TESTCONTEXT_TYPE),
};
generationContext.TestClass.Members.Add(mstestContextFiled);
var mstestContextProperty = new CodeMemberProperty();
mstestContextProperty.Name = "TestContext";
mstestContextProperty.Type = new CodeTypeReference(TESTCONTEXT_TYPE);
mstestContextProperty.Attributes =
(mstestContextProperty.Attributes & ~MemberAttributes.AccessMask) |
MemberAttributes.Public;
mstestContextProperty.GetStatements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("myTestContext")));
mstestContextProperty.SetStatements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("myTestContext"), new CodePropertySetValueReferenceExpression()));
generationContext.TestClass.Members.Add(mstestContextProperty);
}
}
public class MyGeneratorPlugin : IGeneratorPlugin
{
public void RegisterDependencies(ObjectContainer container)
{
}
public void RegisterCustomizations(ObjectContainer container, SpecFlowProjectConfiguration generatorConfiguration)
{
container.RegisterTypeAs<MsTestCustomGeneratorProvider, IUnitTestGeneratorProvider>();
}
public void RegisterConfigurationDefaults(SpecFlowProjectConfiguration specFlowConfiguration)
{
}
}
}
<add name="MtmContext" type="Generator" path=".\SpecFlow\Plugins"/>
private static Microsoft.VisualStudio.TestTools.UnitTesting.TestContext myTestContext;
myTestContext = testContext;
testRunner.ScenarioContext.Add("MSTestContext", TestContext);
TestContext testContext = (TestContext)ScenarioContext.Current["MSTestContext"];
public class MyGeneratorPlugin : IGeneratorPlugin {
public void Initialize(GeneratorPluginEvents generatorPluginEvents, GeneratorPluginParameters generatorPluginParameters) { generatorPluginEvents.CustomizeDependencies += this.CustomizeDependencies; }
public void CustomizeDependencies(object sender, CustomizeDependenciesEventArgs eventArgs) { eventArgs.ObjectContainer.RegisterTypeAs<MsTestCustomGeneratorProvider, IUnitTestGeneratorProvider>(); } }