//[AfterScenario] public static void AfterScenario(string[] reports) { Driver.StopBrowser(); if (StringOperations.ReturnString.StringIsPartOfArray(reports, "extent")) { extent.Flush(); var status = scenario.Status; if (status == Status.Fail || status == Status.Fatal) { Assert.Fail(); } } else { if (StringOperations.ReturnString.StringIsValueOfArray(reports, "tfs")) { var dictionary = DictionaryInteractions.ReadFromPropertiesFile(ReturnPath.ProjectFolderPath() + "ExtentReport/ReportProperties.txt"); if (dictionary["tfsReportStatus"].ToLower() == "fail") { Assert.Fail(); } } } }
//[BeforeScenario("Selenium")] public static void BeforeWebScenarioSelenium(string browserTypeFromConsole, string urlFromConsole, string authUser, string authPass, string authType) { var dictionary = DictionaryInteractions.ReadFromPropertiesFile(ReturnPath.ProjectFolderPath() + "ExtentReport/ReportProperties.txt"); DictionaryInteractions.WriteInTxtFileFromDictionary(ReturnPath.ProjectFolderPath() + "ExtentReport/ReportProperties.txt", dictionary, "tfsReportStatus", ""); var brType = browserTypeFromConsole; var urlToRun = urlFromConsole; switch (brType.ToLower()) { case "ie": Driver.StartBrowser(BrowserTypes.InternetExplorer); Driver.Browser.Manage().Window.Maximize(); UnityContainerFactory.GetContainer().RegisterInstance <IWebDriver>(Driver.Browser); browserType = "IE"; break; case "chrome": Driver.StartBrowser(BrowserTypes.Chrome); Driver.Browser.Manage().Window.Maximize(); UnityContainerFactory.GetContainer().RegisterInstance <IWebDriver>(Driver.Browser); browserType = "Chrome"; break; default: break; } //Driver.Browser.Navigate().GoToUrl(urlToRun); //if(authUser !=null & authPass !=null & authType != null) //{ // Authenticate(authUser, authPass, authType); //} }
public void ErrorMessageTFS(string errorMessage, string fatalORFailed) { Console.Write(fatalORFailed.ToUpper() + ": " + errorMessage + System.Environment.NewLine + "Screenshot: " + testName + dateFormatFileNameStr + ".png" + System.Environment.NewLine + "StackTrace:" + System.Environment.NewLine + ReturnString.FormatingStackTrace(Environment.StackTrace)); var dictionary = DictionaryInteractions.ReadFromPropertiesFile(ReturnPath.ProjectFolderPath() + "ExtentReport/ReportProperties.txt"); DictionaryInteractions.WriteInTxtFileFromDictionary(ReturnPath.ProjectFolderPath() + "ExtentReport/ReportProperties.txt", dictionary, "tfsReportStatus", "fail"); }
//[BeforeTestRun] public static void BeforeTestRun(string browserTypeFromConsole, string[] reports) { reportTypes = reports; var brType = browserTypeFromConsole; DateTime thisDay = DateTime.Now; var dictionary = DictionaryInteractions.ReadFromPropertiesFile(ReturnPath.ProjectFolderPath() + "ExtentReport/ReportProperties.txt"); DictionaryInteractions.WriteInTxtFileFromDictionary(ReturnPath.ProjectFolderPath() + "ExtentReport/ReportProperties.txt", dictionary, "Suffix", thisDay.ToString("yyyy.MM.dd HH:mm:ss", CultureInfo.InvariantCulture)); string[] tokens = thisDay.ToString("yyyy.MM.dd HH:mm:ss", CultureInfo.InvariantCulture).Split(':'); string pathString = System.IO.Path.Combine(ReturnPath.ProjectFolderPath() + "../TestResults/" + dictionary["projectName"] + "/", brType + "_TestReport-" + tokens[0] + "h " + tokens[1] + "min " + tokens[2] + "sec"); System.IO.Directory.CreateDirectory(pathString); DictionaryInteractions.WriteInTxtFileFromDictionary(ReturnPath.ProjectFolderPath() + "ExtentReport/ReportProperties.txt", dictionary, "ReportPath", pathString); if (StringOperations.ReturnString.StringIsValueOfArray(reports, "extentX")) { XmlDocument doc = new XmlDocument(); doc.Load(ReturnPath.ProjectFolderPath() + "ExtentReport/ExtentXReportConf.xml"); XmlNode confNode = doc.SelectSingleNode("/configuration"); confNode.FirstChild.FirstChild.Value = dictionary["projectName"] + "-" + tokens[0] + "h " + tokens[1] + "min " + tokens[2] + "sec-" + brType;//FeatureContext.Current.FeatureInfo.Title; doc.Save(ReturnPath.ProjectFolderPath() + "ExtentReport/ExtentXReportConf.xml"); extentX = new ExtentXReporter(); extentX.LoadConfig(ReturnPath.ProjectFolderPath() + "ExtentReport/ExtentXReportConf.xml"); if (StringOperations.ReturnString.StringIsValueOfArray(reports, "extent")) { extentHTML = new ExtentHtmlReporter(pathString + "/TestReport-" + tokens[0] + "h " + tokens[1] + "min " + tokens[2] + "sec-" + brType + ".html"); extent = new ExtentReports(); extent.AttachReporter(extentHTML, extentX); } else { extent.AttachReporter(extentX); } } else { if (StringOperations.ReturnString.StringIsValueOfArray(reports, "extent")) { extentHTML = new ExtentHtmlReporter(pathString + "/TestReport-" + tokens[0] + "h " + tokens[1] + "min " + tokens[2] + "sec-" + brType + ".html"); extent = new ExtentReports(); extent.AttachReporter(extentHTML); } } }
public static void AddScreenshots(string tfsURL, string tfsToken, string projectId, string buildDefinitionName, string projectName, string nameOfProjectWithTests) { var dictionary = DictionaryInteractions.ReadFromPropertiesFile(ReturnPath.SolutionFolderPath() + nameOfProjectWithTests + "/ExtentReport/ReportProperties.txt"); string[] fileArray = Directory.GetFiles(dictionary["ReportPath"]); var onlyScreenshots = fileArray.Where(s => s.IndexOf(".png") != -1); if (onlyScreenshots.ToList().Count != 0) { VssConnection vc = new VssConnection(new Uri(tfsURL), new VssBasicCredential(string.Empty, tfsToken)); BuildHttpClient bhc = vc.GetClient <BuildHttpClient>(); ProjectHttpClient ddd = vc.GetClient <ProjectHttpClient>(); //ITestManagementTeamProject ddd1 = ddd.GetProject("hgfyjh"); var buildsPerProject = bhc.GetBuildsAsync(projectId).GetAwaiter().GetResult(); var lastBuildId = buildsPerProject.Where <Build>(x => x.Definition.Name == buildDefinitionName).Max <Build>(z => z.Id); Build lastBuild = buildsPerProject.FirstOrDefault <Build>(x => x.Id == lastBuildId); Console.Write("Last Build Number: " + lastBuildId); TestManagementHttpClient ithc = vc.GetClient <TestManagementHttpClient>(); QueryModel qm = new QueryModel("Select * From TestRun Where BuildNumber Contains '" + lastBuild.BuildNumber + "'"); List <TestRun> testruns = ithc.GetTestRunsByQueryAsync(qm, projectName).Result; Console.Write("testruns.Count: " + testruns.Count); foreach (TestRun testrun in testruns) { List <TestCaseResult> testresults = ithc.GetTestResultsAsync(projectName, testrun.Id).Result; Console.Write("testresults.Count: " + testresults.Count); foreach (TestCaseResult tcr in testresults) { var testNamesFromScreents = onlyScreenshots.Select(s => s.ToLower().Split(new string[] { "sec\\" }, StringSplitOptions.None)[1].Split(new string[] { "20" }, StringSplitOptions.None)[0]).Distinct(); //var screentsShotPerTest = onlyScreenshots.Where(s => s.Split(new string[] { "sec\\" }, StringSplitOptions.None)[1].Split(new string[] { "20" }, StringSplitOptions.None)[0] == scenarioName.Replace(" ", string.Empty)); Console.WriteLine("tcr.Id: " + tcr.Id); if (testNamesFromScreents.ToList().Contains(tcr.AutomatedTestName.Split('.').Last().ToLower())) { Console.WriteLine("recognize Test: " + tcr.AutomatedTestName.Split('.').Last().ToLower()); using (var client = new HttpClient()) { string credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", tfsToken))); client.BaseAddress = new Uri(tfsURL); //url of our account client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials); var screentsShotPerTest = onlyScreenshots.Where(s => s.Split(new string[] { "sec\\" }, StringSplitOptions.None)[1].Split(new string[] { "20" }, StringSplitOptions.None)[0].ToLower() == tcr.AutomatedTestName.Split('.').Last().ToLower()); foreach (var screenshot in screentsShotPerTest) { Console.WriteLine("screentsShotPerTest: " + screenshot); var post = new PostImageTfs() { Stream = Convert.ToBase64String(File.ReadAllBytes(screenshot)), AttachmentType = "GeneralAttachment", Comment = "screenshot of error", FileName = screenshot.Split(new string[] { "sec\\" }, StringSplitOptions.None)[1] }; Console.WriteLine("tcr.TestRun.Id: " + tcr.TestRun.Id); Console.WriteLine("tcr.Id: " + tcr.Id); Console.WriteLine("screenshot.Split(new string[] { DateTime.Now.Year.ToString() }, StringSplitOptions.None)[2] " + screenshot.Split(new string[] { DateTime.Now.Year.ToString() }, StringSplitOptions.None)[2]); var test = new StringContent($"{{\"stream\": \"{post.Stream}\",\"fileName\": \"{post.FileName}\",\"comment\": \"{post.Comment}\",\"attachmentType\": \"{post.AttachmentType}\"}}", Encoding.UTF8, "application/json"); HttpResponseMessage response = client.PostAsync("/DefaultCollection/" + projectName + "/_apis/test/runs/" + tcr.TestRun.Id + "/results/" + tcr.Id + "/attachments?api-version=2.0-preview.1", test).Result; Console.WriteLine("response: " + response.StatusCode); } } } } } } }