Skip to content

Instantly share code, notes, and snippets.

@angelovstanton
Last active October 2, 2016 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angelovstanton/43b8e48d6c1fd66be6dd709d102b00b1 to your computer and use it in GitHub Desktop.
Save angelovstanton/43b8e48d6c1fd66be6dd709d102b00b1 to your computer and use it in GitHub Desktop.
public class ExecutionEngineBehaviorObserver : BaseTestBehaviorObserver
{
private readonly IUnityContainer unityContainer;
private Browsers executionBrowserType;
private readonly List<ITestExecutionEngine> testExecutionEngines;
public ExecutionEngineBehaviorObserver(IUnityContainer unityContainer)
{
this.unityContainer = unityContainer;
this.testExecutionEngines =
this.unityContainer.ResolveAll<ITestExecutionEngine>().ToList();
}
protected override void PostTestCleanup(object sender, TestExecutionEventArgs e)
{
foreach (var testExecutionEngine in this.testExecutionEngines)
{
if (testExecutionEngine != null)
{
testExecutionEngine.Dispose();
}
}
}
protected override void PreTestInit(object sender, TestExecutionEventArgs e)
{
this.executionBrowserType = this.ConfigureTestExecutionBrowser(e.MemberInfo);
foreach (var testExecutionEngine in this.testExecutionEngines)
{
if (testExecutionEngine.IsSelected(e.MemberInfo))
{
testExecutionEngine.RegisterDependencies(executionBrowserType);
break;
}
}
}
private Browsers ConfigureTestExecutionBrowser(MemberInfo memberInfo)
{
var currentExecutionBrowserType = Browsers.Firefox;
Browsers methodExecutionBrowser =
this.GetExecutionBrowser(memberInfo);
Browsers classExecutionBrowser =
this.GetExecutionBrowser(memberInfo.DeclaringType);
if (methodExecutionBrowser != Browsers.NotSet)
{
currentExecutionBrowserType = methodExecutionBrowser;
}
else
{
if (classExecutionBrowser != Browsers.NotSet)
{
currentExecutionBrowserType = classExecutionBrowser;
}
else
{
currentExecutionBrowserType = Browsers.InternetExplorer;
}
}
return currentExecutionBrowserType;
}
private Browsers GetExecutionBrowser(MemberInfo memberInfo)
{
if (memberInfo == null)
{
throw new ArgumentNullException("The test method's info cannot be null.");
}
var executionBrowserAttribute =
memberInfo.GetCustomAttribute<ExecutionEngineAttribute>(true);
if (executionBrowserAttribute != null)
{
return executionBrowserAttribute.Browser;
}
return Browsers.NotSet;
}
private Browsers GetExecutionBrowser(Type currentType)
{
if (currentType == null)
{
throw new ArgumentNullException("The test method's type cannot be null.");
}
var executionBrowserAttribute =
currentType.GetCustomAttribute<ExecutionEngineAttribute>(true);
if (executionBrowserAttribute != null)
{
return executionBrowserAttribute.Browser;
}
return Browsers.NotSet;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment