/// <summary> /// Saves a screenshot's thumbnail to a local path. /// </summary> /// <param name="screenshot">The screenshot.</param> /// <param name="path">The path where the image will be saved to.</param> /// <param name="filename"> /// The name of the file where the image will be saved to. The extension will be added automatically according to the image path. /// If no filename is supplied then the original filename from the BrowserStack url will be used. /// </param> /// <param name="overwrite">If set to true then the file will be overwritten, otherwise no attempt will be made to retrieve the file.</param> /// <returns> /// The <see cref="Task" />. /// </returns> public async Task SaveThumbnailToFileAsync(Screenshot screenshot, string path, string filename = null, bool overwrite = false) { Contract.Requires(screenshot != null); Contract.Requires(path != null); using (var httpClient = this.httpClientFactory.Invoke()) { var fullPath = string.IsNullOrEmpty(filename) ? Path.Combine(path, Path.GetFileName(new Uri(screenshot.ThumbnailUrl).AbsolutePath)) : Path.Combine(path, filename + Path.GetExtension(new Uri(screenshot.ThumbnailUrl).AbsolutePath)); // If there's no reason to perform the call then don't if (!overwrite && File.Exists(fullPath)) { return; } else { var imageResponse = await httpClient.GetAsync(screenshot.ThumbnailUrl); imageResponse.EnsureSuccessStatusCode(); await this.ReadAsFileAsync(imageResponse.Content, fullPath, true); } } }
/// <summary> /// Initializes a new instance of the <see cref="ScreenshotEventArgs" /> class. /// </summary> /// <param name="screenshot">The screenshot.</param> /// <param name="imageFilePath">The image file path.</param> /// <param name="thumbnailFilePath">The thumbnail file path.</param> public ScreenshotEventArgs(Screenshot screenshot, string imageFilePath = null, string thumbnailFilePath = null) { this.ThumbnailFilePath = thumbnailFilePath; this.ImageFilePath = imageFilePath; this.Screenshot = screenshot; this.State = screenshot.State; }
/// <summary> /// Initializes a new instance of the <see cref="ScreenshotFailedEventArgs" /> class. /// </summary> /// <param name="screenshot">The screenshot.</param> /// <param name="exception">The exception.</param> /// <param name="imageName">The name of the image that could not be saved.</param> public ScreenshotFailedEventArgs(Screenshot screenshot, Exception exception, string imageName = null) { this.ImageName = imageName; this.Screenshot = screenshot; this.Exception = exception; this.State = screenshot.State; }
/// <summary> /// The save screenshot async. /// </summary> /// <param name="screenshot">The screenshot.</param> /// <param name="jobId">The job identifier.</param> /// <param name="jobInfo">The job info.</param> /// <param name="rootPath">The root path.</param> /// <returns> /// The <see cref="Task" />. /// </returns> private async Task SaveScreenshotAsync(Screenshot screenshot, string jobId, Job.JobInfo jobInfo, string rootPath) { // Create the folder for the screenshot var directory = this.CreateScreenshotDirectory(jobInfo, screenshot.Browser, rootPath); var filename = this.jobsToJobsToRun[jobId.ToLower()].Filename; try { if (screenshot.State == Screenshot.States.Done) { var tasksToWaitFor = new List<Task>(); tasksToWaitFor.Add(this.screenshotsApi.SaveScreenshotToFileAsync(screenshot, directory.FullName, filename, false)); if (this.captureThumbnails) { tasksToWaitFor.Add(this.screenshotsApi.SaveThumbnailToFileAsync(screenshot, directory.FullName, filename + "_thumbnail", false)); } await Task.WhenAll(tasksToWaitFor.ToArray()); } } catch (Exception ex) { this.OnScreenshotFailed(new ScreenshotFailedEventArgs(screenshot, ex, filename)); } this.ScreenhotsCompleted.Add(screenshot); this.OnScreenshotCompleted(new ScreenshotEventArgs(screenshot)); }