using Microsoft.Win32; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Reflection; using OpenQA.Selenium; using System.IO; namespace Prevu { public class TestRegistrator { const string regKeyCurrentTest = "HKEY_CURRENT_USER\\Software\\TestSystem\\CurrentTest"; const string regKeyLastTest = "HKEY_CURRENT_USER\\Software\\TestSystem\\LastTest"; protected static Result result; protected string domain; public TestRegistrator(string domain) { this.domain = domain; } protected void UpdateResultSpecProperties(string typeName, string methodName, string assemblyLocation, string methodModuleName, string useCase, string scenario) { if ((result.Class == typeName) && (result.Name.IndexOf(methodName) > -1)) { result.AssemblyLocation = assemblyLocation; result.MethodModule = methodModuleName; result.UseCase = useCase; result.Scenario = scenario; } } protected void UpdateSpecProperties() { foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) { foreach (Type type in assembly.GetTypes()) { if (type.IsSubclassOf(typeof(BaseTest))) { //PROPERTIES /*foreach (System.Reflection.PropertyInfo property in type.GetProperties()) { if (property.CanRead) { //Response.Write("
" + an.ToString() + "." + type.ToString() + "." + property.Name); } }*/ //METHODS var methods = type.GetMethods(); foreach (System.Reflection.MethodInfo method in methods) { object[] attributes = method.GetCustomAttributes(typeof(TestPropertyAttribute), true); if (attributes.Length > 0) { TestPropertyAttribute attribute = (TestPropertyAttribute)attributes[0]; var attConfig = assembly.GetCustomAttributes(typeof(AssemblyConfigurationAttribute), false); //activity.UpdateSpecProperties(type.FullName, method.Name, assembly.Location, method.Module.Name, attribute.Name, attribute.Value); UpdateResultSpecProperties(type.FullName, method.Name, assembly.Location, method.Module.Name, attribute.Name, attribute.Value); } //Response.Write("
" + an.ToString() + "." + type.ToString() + "." + method.Name + ""); /*foreach (System.Reflection.ParameterInfo param in method.GetParameters()) { //Response.Write("
Param=" + param.Name.ToString()); //Response.Write("
Type=" + param.ParameterType.ToString()); //Response.Write("
Position=" + param.Position.ToString()); //Response.Write("
Optional=" + param.IsOptional.ToString() + "
"); }*/ } } } } } protected static string CorrectTestNameToFileName(string testName) { return testName.Replace("\\", "").Replace("/", "").Replace("(", "").Replace(")", "").Replace("#", "").Replace("@", "").Replace("*", "").Replace("!", "").Replace("$", "").Replace("%", "").Replace("^", "").Replace(":", "").Replace("?", "").Replace("+", "").Replace("=", "").Replace("|", "").Replace("{", "").Replace("}", "").Replace("[", "").Replace("]", "").Replace("'", "").Replace("\"", "").Replace(";", "").Replace("<", "").Replace(">", "").Replace(".", "").Replace(",", "").Replace("-", "").Replace(" ", "").Replace("_", ""); } protected static string GetBrowser(IWebDriver driver) { string browser = "Unknown"; string[] set = driver.GetType().FullName.Split('.'); if (set.Length > 0) { browser = set[set.Length - 1].Replace("Driver", ""); } return browser; } protected static void SaveScreenshot(IWebDriver driver, string pngFileName) { try { if (File.Exists(pngFileName)) { File.Delete(pngFileName); } Screenshot screenshot = ((ITakesScreenshot)driver).GetScreenshot(); screenshot.SaveAsFile(pngFileName, System.Drawing.Imaging.ImageFormat.Png); } catch { } } protected string GetFullTestName(TestContext testContext) { string testCase = ""; if ((testContext.DataRow != null) && (testContext.DataRow.ItemArray != null) && (testContext.DataRow.ItemArray.Length > 0)) { testCase = testContext.DataRow.ItemArray[0].ToString(); } if (!string.IsNullOrEmpty(testCase)) { testCase = string.Format("({0})", testCase); } return string.Format("{0}.{1}{2}", testContext.FullyQualifiedTestClassName, testContext.TestName, testCase); } public void RegisterStart(TestContext testContext, WebDriver driver) { string fullTestName = GetFullTestName(testContext); result = Result.Load(testContext.FullyQualifiedTestClassName, GetBrowser(driver.Driver), fullTestName); result.Started = DateTime.Now; Registry.SetValue(regKeyCurrentTest, "Name", fullTestName); Registry.SetValue(regKeyCurrentTest, "Started", DateTime.Now.ToString()); Console.WriteLine(fullTestName); } public void RegisterFinish(TestContext testContext, WebDriver driver) { result.Finished = DateTime.Now; result.Outcome = testContext.CurrentTestOutcome.ToString(); result.Duration = result.Finished.Subtract(result.Started).ToString(@"hh\:mm\:ss"); Registry.SetValue(regKeyCurrentTest, "Name", ""); Registry.SetValue(regKeyCurrentTest, "Started", ""); Registry.SetValue(regKeyLastTest, "Name", GetFullTestName(testContext)); Registry.SetValue(regKeyLastTest, "Started", result.Started); Registry.SetValue(regKeyLastTest, "Finished", result.Finished); Registry.SetValue(regKeyLastTest, "Duration", result.Duration); Registry.SetValue(regKeyLastTest, "Outcome", testContext.CurrentTestOutcome); result.Name = GetFullTestName(testContext); if (testContext.CurrentTestOutcome != UnitTestOutcome.Passed) { string testName = GetFullTestName(testContext); string screenShotsFolder = @"..\..\..\ScreenShots"; string screenShotsFile = string.Format("{0}.png", CorrectTestNameToFileName(testName)); screenShotsFile = Path.Combine(screenShotsFolder, screenShotsFile); SaveScreenshot(driver.Driver, screenShotsFile); result.ScreenShot = Path.GetFullPath(screenShotsFile); Console.WriteLine(string.Format("Test Failed. See screenshot {0}", screenShotsFile)); } else { result.ScreenShot = " "; Console.WriteLine("Test Passed."); } result.Domain = domain; UpdateSpecProperties(); result.Save(); } } }