/// <summary> /// This method scrolls element into view if needed, and then uses <seealso cref="Page.ScreenshotDataAsync(ScreenshotOptions)"/> to take a screenshot of the element. /// If the element is detached from DOM, the method throws an error. /// </summary> /// <returns>The task</returns> /// <param name="file">The file path to save the image to. The screenshot type will be inferred from file extension. /// If path is a relative path, then it is resolved relative to current working directory. If no path is provided, /// the image won't be saved to the disk.</param> /// <param name="options">Screenshot options.</param> public async Task ScreenshotAsync(string file, ScreenshotOptions options) { if (!options.Type.HasValue) { options.Type = ScreenshotOptions.GetScreenshotTypeFromFile(file); } var data = await ScreenshotDataAsync(options).ConfigureAwait(false); using (var fs = AsyncFileHelper.CreateStream(file, FileMode.Create)) { await fs.WriteAsync(data, 0, data.Length).ConfigureAwait(false); } }
internal static async Task <string> ReadProtocolStreamStringAsync(ChromiumSession client, string handle, string path) { var result = new StringBuilder(); var fs = !string.IsNullOrEmpty(path) ? AsyncFileHelper.CreateStream(path, FileMode.Create) : null; try { bool eof = false; while (!eof) { var response = await client.SendAsync(new IOReadRequest { Handle = handle, }).ConfigureAwait(false); eof = response.Eof.Value; result.Append(response.Data); if (fs != null) { var data = Encoding.UTF8.GetBytes(response.Data); await fs.WriteAsync(data, 0, data.Length).ConfigureAwait(false); } } await client.SendAsync(new IOCloseRequest { Handle = handle, }).ConfigureAwait(false); return(result.ToString()); } finally { fs?.Dispose(); } }
internal static async Task <byte[]> ReadProtocolStreamByteAsync(ChromiumSession client, string handle, string path) { IEnumerable <byte> result = null; bool eof = false; var fs = !string.IsNullOrEmpty(path) ? AsyncFileHelper.CreateStream(path, FileMode.Create) : null; try { while (!eof) { var response = await client.SendAsync(new IOReadRequest { Handle = handle, }).ConfigureAwait(false); eof = response.Eof.Value; var data = Convert.FromBase64String(response.Data); result = result == null ? data : result.Concat(data); if (fs != null) { await fs.WriteAsync(data, 0, data.Length).ConfigureAwait(false); } } await client.SendAsync(new IOCloseRequest { Handle = handle, }).ConfigureAwait(false); return(result.ToArray()); } finally { fs?.Dispose(); } }