Created
October 2, 2016 16:14
-
-
Save angelovstanton/59203fe4a1b28e68d70b1e0940ca9bfd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ExecutionEngineBehaviorObserver : BaseTestBehaviorObserver | |
{ | |
private readonly IUnityContainer unityContainer; | |
private ExecutionEngineType executionEngineType; | |
private Browsers executionBrowserType; | |
private IDriver driver; | |
public ExecutionEngineBehaviorObserver(IUnityContainer unityContainer) | |
{ | |
this.unityContainer = unityContainer; | |
} | |
protected override void PostTestCleanup(object sender, TestExecutionEventArgs e) | |
{ | |
this.driver.Quit(); | |
} | |
protected override void PreTestInit(object sender, TestExecutionEventArgs e) | |
{ | |
this.executionBrowserType = this.ConfigureTestExecutionBrowser(e.MemberInfo); | |
this.executionEngineType = this.GetExecutionEngineType(e.MemberInfo); | |
this.ResolveAllDriverDependencies(); | |
} | |
private ExecutionEngineType GetExecutionEngineTypeByMethodInfo(MemberInfo memberInfo) | |
{ | |
if (memberInfo == null) | |
{ | |
throw new ArgumentNullException("The test method's info cannot be null."); | |
} | |
var executionEngineTypeMethodAttribute = memberInfo.GetCustomAttribute<ExecutionEngineAttribute>(); | |
if (executionEngineTypeMethodAttribute != null) | |
{ | |
return executionEngineTypeMethodAttribute.ExecutionEngineType; | |
} | |
return ExecutionEngineType.NotSpecified; | |
} | |
private ExecutionEngineType GetExecutionEngineType(Type currentType) | |
{ | |
if (currentType == null) | |
{ | |
throw new ArgumentNullException("The test method's type cannot be null."); | |
} | |
var executionEngineClassAttribute = currentType.GetCustomAttribute<ExecutionEngineAttribute>(true); | |
if (executionEngineClassAttribute != null) | |
{ | |
return executionEngineClassAttribute.ExecutionEngineType; | |
} | |
return ExecutionEngineType.NotSpecified; | |
} | |
private ExecutionEngineType GetExecutionEngineType(MemberInfo memberInfo) | |
{ | |
var executionEngineType = ExecutionEngineType.TestStudio; | |
ExecutionEngineType methodExecutionEngineType = this.GetExecutionEngineTypeByMethodInfo(memberInfo); | |
ExecutionEngineType classExecutionEngineType = this.GetExecutionEngineType(memberInfo.DeclaringType); | |
if (methodExecutionEngineType != ExecutionEngineType.NotSpecified) | |
{ | |
executionEngineType = methodExecutionEngineType; | |
} | |
else if (classExecutionEngineType != ExecutionEngineType.NotSpecified) | |
{ | |
executionEngineType = classExecutionEngineType; | |
} | |
return executionEngineType; | |
} | |
private void ResolveAllDriverDependencies() | |
{ | |
var browserSettings = new BrowserSettings(this.executionBrowserType); | |
if (this.executionEngineType.Equals(ExecutionEngineType.TestStudio)) | |
{ | |
#region Default Registration | |
this.unityContainer.RegisterType<IDriver, TestingFrameworkDriver>( | |
new InjectionFactory(x => new TestingFrameworkDriver(this.unityContainer, browserSettings))); | |
#endregion | |
this.driver = this.unityContainer.Resolve<IDriver>(); | |
this.unityContainer.RegisterType<IButton, TestingFrameworkControls.Button>(); | |
this.unityContainer.RegisterType<ITextBox, TestingFrameworkControls.TextBox>(); | |
this.unityContainer.RegisterType<IDiv, TestingFrameworkControls.Div>(); | |
this.unityContainer.RegisterType<ISearch, TestingFrameworkControls.Search>(); | |
this.unityContainer.RegisterType<IInputSubmit, TestingFrameworkControls.InputSubmit>(); | |
} | |
else if (this.executionEngineType.Equals(ExecutionEngineType.WebDriver)) | |
{ | |
this.unityContainer.RegisterType<IDriver, SeleniumDriver>( | |
new InjectionFactory(x => new SeleniumDriver(this.unityContainer, browserSettings))); | |
this.driver = this.unityContainer.Resolve<IDriver>(); | |
this.unityContainer.RegisterType<IButton, SeleniumControls.Button>(); | |
this.unityContainer.RegisterType<ITextBox, SeleniumControls.TextBox>(); | |
this.unityContainer.RegisterType<IDiv, SeleniumControls.Div>(); | |
this.unityContainer.RegisterType<ISearch, SeleniumControls.Search>(); | |
this.unityContainer.RegisterType<IInputSubmit, SeleniumControls.InputSubmit>(); | |
} | |
this.unityContainer.RegisterInstance<IDriver>(this.driver); | |
this.unityContainer.RegisterInstance<IBrowser>(this.driver); | |
this.unityContainer.RegisterInstance<ICookieService>(this.driver); | |
this.unityContainer.RegisterInstance<IDialogService>(this.driver); | |
this.unityContainer.RegisterInstance<IJavaScriptInvoker>(this.driver); | |
this.unityContainer.RegisterInstance<INavigationService>(this.driver); | |
this.unityContainer.RegisterInstance<IElementFinder>(this.driver); | |
# region 11. Failed Tests Аnalysis - Decorator Design Pattern | |
this.unityContainer.RegisterType<IEnumerable<IExceptionAnalysationHandler>, IExceptionAnalysationHandler[]>(); | |
this.unityContainer.RegisterType<IUiExceptionAnalyser, UiExceptionAnalyser>(); | |
this.unityContainer.RegisterType<IElementFinder, ExceptionAnalyzedElementFinder>( | |
new InjectionFactory(x => new ExceptionAnalyzedElementFinder(this.driver, this.unityContainer.Resolve<IUiExceptionAnalyser>()))); | |
this.unityContainer.RegisterType<INavigationService, ExceptionAnalyzedNavigationService>( | |
new InjectionFactory(x => new ExceptionAnalyzedNavigationService(this.driver, this.unityContainer.Resolve<IUiExceptionAnalyser>()))); | |
#endregion | |
} | |
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