/// <summary> /// Builds the path of the file without the extension. /// </summary> /// <param name="screenshotInfo">The screenshot information.</param> /// <returns>The file path without the extension.</returns> protected override string BuildFilePath(ScreenshotInfo screenshotInfo) { if (FilePathBuilder != null) { return(FilePathBuilder(screenshotInfo)); } string folderPath = FolderPathBuilder?.Invoke() ?? $@"Logs\{AtataContext.BuildStart:yyyy-MM-dd HH_mm_ss}\{AtataContext.Current.TestName}"; folderPath = folderPath.SanitizeForPath(); string fileName = FileNameBuilder?.Invoke(screenshotInfo) ?? $"{screenshotInfo.Number:D2} - {screenshotInfo.PageObjectFullName}{screenshotInfo.Title?.Prepend(" - ")}"; fileName = fileName.SanitizeForFileName(); return(Path.Combine(folderPath, fileName)); }
/// <summary> /// Formats the screenshot file path using path variables. /// </summary> /// <param name="format">The format.</param> /// <param name="screenshotInfo">The screenshot information.</param> /// <returns>The formatted file path format.</returns> public static string FormatPath(string format, ScreenshotInfo screenshotInfo) { if (format.Contains('{')) { var screenshotVariables = new Dictionary <string, object> { ["screenshot-number"] = screenshotInfo.Number, ["screenshot-title"] = screenshotInfo.Title, ["screenshot-pageobjectname"] = screenshotInfo.PageObjectName, ["screenshot-pageobjecttypename"] = screenshotInfo.PageObjectTypeName, ["screenshot-pageobjectfullname"] = screenshotInfo.PageObjectFullName }; return(AtataContext.Current.FillTemplateString(format, screenshotVariables)); } else { return(format); } }
/// <summary> /// Takes the specified screenshot. /// </summary> /// <param name="screenshotInfo">The screenshot information.</param> public void Take(ScreenshotInfo screenshotInfo) { string filePath = BuildFilePath(screenshotInfo); filePath = filePath.SanitizeForPath(); filePath += ImageFormat.GetExtension(); if (!Path.IsPathRooted(filePath)) { filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filePath); } string folderPath = Path.GetDirectoryName(filePath); if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } screenshotInfo.Screenshot.SaveAsFile(filePath, ImageFormat); AtataContext.Current.Log.Info($"Screenshot saved to file \"{filePath}\""); }
public void Screenshot(string title = null) { if (Driver == null || !screenshotConsumers.Any()) return; try { screenshotNumber++; string logMessage = $"Take screenshot #{screenshotNumber}"; if (!string.IsNullOrWhiteSpace(title)) logMessage += $" {title}"; Info(logMessage); ScreenshotInfo screenshotInfo = new ScreenshotInfo { Screenshot = Driver.TakeScreenshot(), Number = screenshotNumber, Title = title, PageObjectName = AtataContext.Current.PageObject.ComponentName, PageObjectFullName = AtataContext.Current.PageObject.ComponentFullName }; foreach (IScreenshotConsumer screenshotConsumer in screenshotConsumers) { try { screenshotConsumer.Take(screenshotInfo); } catch (Exception e) { Error("Screenshot failed", e); } } } catch (Exception e) { Error("Screenshot failed", e); } }
/// <summary> /// Builds the path of the file without the extension. /// </summary> /// <param name="screenshotInfo">The screenshot information.</param> /// <returns>The file path without the extension.</returns> protected abstract string BuildFilePath(ScreenshotInfo screenshotInfo);
protected virtual string BuildDefaultFileName(ScreenshotInfo screenshotInfo) => $"{screenshotInfo.Number:D2} - {screenshotInfo.PageObjectFullName}{screenshotInfo.Title?.Prepend(" - ")}";