private async Task ScreenshotPageAsDeviceAsync( Screenshotter ss, Uri url, DeviceScreenshots result, KeyValuePair <Device, int> size) { var device = size.Key; var width = size.Value; var baseName = Utils.SanitizeFilename(url.ToString()); var filename = $"{baseName}.{device}{Screenshotter.ImageExtension}"; var imgPath = Path.Combine(_screenshotDir, filename); NavigationTiming TakeScreenshot() => ss.TakeScreenshot(url.ToString(), imgPath, width); try { result.RequestTiming = await Task.Run(TakeScreenshot); result.Paths.Add(device, filename); } catch (Exception ex) { result.Error += ex.Message + Environment.NewLine; } }
public async Task TakeScreenshotsAsync(CancellationToken?token = null, IProgress <TaskProgress> progress = null) { CreateEmptyDir(); var results = new ScreenshotResults(_creationTimestamp); using (var ss = new Screenshotter(_project)) { int i = 0; foreach (var uri in PageUris) { if (token?.IsCancellationRequested == true) { throw new TaskCanceledException("The screenshotting task was canceled."); } var currentProgress = new TaskProgress(++i, PageUris.Count, uri.AbsoluteUri); progress?.Report(currentProgress); var result = new DeviceScreenshots(uri); await ScreenshotPageAsAllDevices(ss, uri, result); results.Screenshots.Add(result); } } string sessionLabel = Path.GetFileName(_screenshotDir); Store.SaveScreenshotManifest(sessionLabel, results); var completedProgress = new TaskProgress(PageUris.Count, PageUris.Count, "Completed"); progress?.Report(completedProgress); }
private async Task ScreenshotPageAsAllDevices( Screenshotter ss, Uri url, DeviceScreenshots result) { var sizes = Options.DeviceOptions .Where(x => x.Value.Enabled && x.Value.DisplayWidthInPixels > 0) .Select(x => new KeyValuePair <Device, int>( x.Key, x.Value.DisplayWidthInPixels)); foreach (var size in sizes) { await ScreenshotPageAsDeviceAsync(ss, url, result, size); } }