Best Puppeteer-sharp code snippet using PuppeteerSharp.Response.BufferAsync
AnimepaheExtractor.cs
Source:AnimepaheExtractor.cs
...223 response = await _tmpPage.ReloadAsync();224 }225 } while (title != string.Empty);226 // Get response's buffer, encodes as string227 byte[] buffer = await response.BufferAsync();228 // Temp page closes229 await _tmpPage.CloseAsync();230 return Encoding.UTF8.GetString(buffer);231 }232 public static async Task<BitmapImage> GetImage(string url)233 {234 // Makes sure Puppeteer is OK235 InitializePuppeteer();236 Page _tmpPage = await DefaultBrowser.NewPageAsync();237 Response response = await _tmpPage.GoToAsync(url);238 // The image title should contain one of these239 IList<string> fileTypes = new List<string> { ".jpg", ".png", ".bmp", ".gif" };240 // This block waits until the title matches the condition241 string title = string.Empty;242 do243 {244 Thread.Sleep(500);245 try246 {247 title = await _tmpPage.GetTitleAsync();248 }249#if DEBUG250 catch (Exception e)251 {252 Debug.WriteLine(e.Message);253#else254 catch255 {256#endif257 response = await _tmpPage.ReloadAsync();258 }259 } while (!fileTypes.Any(s => title.Contains(s)));260 // Gets response's buffer, encodes as bitmap (No tested yet)261 byte[] buffer = await response.BufferAsync();262 BitmapImage _bitmap = new BitmapImage();263 _bitmap.BeginInit();264 using (var mStream = new MemoryStream(buffer))265 {266 _bitmap.StreamSource = mStream;267 }268 _bitmap.EndInit();269 // Temp page closes270 await _tmpPage.CloseAsync();271 return _bitmap;272 }273 public static string GetRequest(string uri, int timeoutSecs = 10)274 {275 try { ...
Methods.cs
Source:Methods.cs
...26 };27 var response = await page.GoToAsync(url, options);28 data.ADDRESS = response.Url;29 data.SOURCE = await response.TextAsync();30 data.RAWSOURCE = await response.BufferAsync();31 SwitchToMainFramePrivate(data);32 data.Logger.Log($"Navigated to {url}", LogColors.DarkSalmon);33 }34 [Block("Waits for navigation to complete", name = "Wait for Navigation")]35 public static async Task PuppeteerWaitForNavigation(BotData data,36 WaitUntilNavigation loadedEvent = WaitUntilNavigation.Load, int timeout = 30000)37 {38 data.Logger.LogHeader();39 var page = GetPage(data);40 var options = new NavigationOptions41 {42 Timeout = timeout,43 WaitUntil = new WaitUntilNavigation[] { loadedEvent }44 };45 await page.WaitForNavigationAsync(options);46 data.ADDRESS = page.Url;47 data.SOURCE = await page.GetContentAsync();48 data.RAWSOURCE = Encoding.UTF8.GetBytes(data.SOURCE);49 SwitchToMainFramePrivate(data);50 data.Logger.Log($"Waited for navigation to complete", LogColors.DarkSalmon);51 }52 [Block("Clears cookies in the page stored for a specific website", name = "Clear Cookies")]53 public static async Task PuppeteerClearCookies(BotData data, string website)54 {55 data.Logger.LogHeader();56 var page = GetPage(data);57 var cookies = await page.GetCookiesAsync(website);58 await page.DeleteCookieAsync(cookies);59 data.Logger.Log($"Cookies cleared for site {website}", LogColors.DarkSalmon);60 }61 [Block("Sends keystrokes to the browser page", name = "Type in Page")]62 public static async Task PuppeteerPageType(BotData data, string text)63 {64 data.Logger.LogHeader();65 66 var page = GetPage(data);67 await page.Keyboard.TypeAsync(text);68 data.Logger.Log($"Typed {text}", LogColors.DarkSalmon);69 }70 [Block("Presses and releases a key in the browser page", name = "Key Press in Page", 71 extraInfo = "Full list of keys here: https://github.com/puppeteer/puppeteer/blob/v1.14.0/lib/USKeyboardLayout.js")]72 public static async Task PuppeteerPageKeyPress(BotData data, string key)73 {74 data.Logger.LogHeader();75 76 var page = GetPage(data);77 await page.Keyboard.PressAsync(key);78 data.Logger.Log($"Pressed and released {key}", LogColors.DarkSalmon);79 // Full list of keys: https://github.com/puppeteer/puppeteer/blob/v1.14.0/lib/USKeyboardLayout.js80 }81 [Block("Presses a key in the browser page without releasing it", name = "Key Down in Page",82 extraInfo = "Full list of keys here: https://github.com/puppeteer/puppeteer/blob/v1.14.0/lib/USKeyboardLayout.js")]83 public static async Task PuppeteerPageKeyDown(BotData data, string key)84 {85 data.Logger.LogHeader();86 87 var page = GetPage(data);88 await page.Keyboard.DownAsync(key);89 data.Logger.Log($"Pressed (and holding down) {key}", LogColors.DarkSalmon);90 // Full list of keys: https://github.com/puppeteer/puppeteer/blob/v1.14.0/lib/USKeyboardLayout.js91 }92 [Block("Releases a key that was previously pressed in the browser page", name = "Key Up in Page",93 extraInfo = "Full list of keys here: https://github.com/puppeteer/puppeteer/blob/v1.14.0/lib/USKeyboardLayout.js")]94 public static async Task PuppeteerKeyUp(BotData data, string key)95 {96 data.Logger.LogHeader();97 98 var page = GetPage(data);99 await page.Keyboard.UpAsync(key);100 data.Logger.Log($"Released {key}", LogColors.DarkSalmon);101 // Full list of keys: https://github.com/puppeteer/puppeteer/blob/v1.14.0/lib/USKeyboardLayout.js102 }103 [Block("Takes a screenshot of the entire browser page and saves it to an output file", name = "Screenshot Page")]104 public static async Task PuppeteerScreenshotPage(BotData data, string file, bool fullPage = false, bool omitBackground = false)105 {106 data.Logger.LogHeader();107 108 var page = GetPage(data);109 var options = new ScreenshotOptions110 {111 FullPage = fullPage,112 OmitBackground = omitBackground,113 Type = omitBackground ? ScreenshotType.Png : ScreenshotType.Jpeg,114 Quality = omitBackground ? null : 100115 };116 await page.ScreenshotAsync(file, options);117 data.Logger.Log($"Took a screenshot of the {(fullPage ? "full" : "visible")} page and saved it to {file}", LogColors.DarkSalmon);118 }119 [Block("Takes a screenshot of the entire browser page and converts it to a base64 string", name = "Screenshot Page Base64")]120 public static async Task<string> PuppeteerScreenshotPageBase64(BotData data, bool fullPage = false, bool omitBackground = false)121 {122 data.Logger.LogHeader();123 124 var page = GetPage(data);125 var options = new ScreenshotOptions126 {127 FullPage = fullPage,128 OmitBackground = omitBackground,129 Type = omitBackground ? ScreenshotType.Png : ScreenshotType.Jpeg,130 Quality = omitBackground ? null : 100131 };132 var base64 = await page.ScreenshotBase64Async(options);133 data.Logger.Log($"Took a screenshot of the {(fullPage ? "full" : "visible")} page as base64", LogColors.DarkSalmon);134 return base64;135 }136 [Block("Scrolls to the top of the page", name = "Scroll to Top")]137 public static async Task PuppeteerScrollToTop(BotData data)138 {139 data.Logger.LogHeader();140 141 var page = GetPage(data);142 await page.EvaluateExpressionAsync("window.scrollTo(0, 0);");143 data.Logger.Log($"Scrolled to the top of the page", LogColors.DarkSalmon);144 }145 [Block("Scrolls to the bottom of the page", name = "Scroll to Bottom")]146 public static async Task PuppeteerScrollToBottom(BotData data)147 {148 data.Logger.LogHeader();149 150 var page = GetPage(data);151 await page.EvaluateExpressionAsync("window.scrollTo(0, document.body.scrollHeight);");152 data.Logger.Log($"Scrolled to the bottom of the page", LogColors.DarkSalmon);153 }154 [Block("Scrolls the page by a certain amount horizontally and vertically", name = "Scroll by")]155 public static async Task PuppeteerScrollBy(BotData data, int horizontalScroll, int verticalScroll)156 {157 data.Logger.LogHeader();158 159 var page = GetPage(data);160 await page.EvaluateExpressionAsync($"window.scrollBy({horizontalScroll}, {verticalScroll});");161 data.Logger.Log($"Scrolled by ({horizontalScroll}, {verticalScroll})", LogColors.DarkSalmon);162 }163 [Block("Sets the viewport dimensions and options", name = "Set Viewport")]164 public static async Task PuppeteerSetViewport(BotData data, int width, int height, bool isMobile = false, bool isLandscape = false, float scaleFactor = 1f)165 {166 data.Logger.LogHeader();167 var page = GetPage(data);168 var options = new ViewPortOptions169 {170 Width = width,171 Height = height,172 IsMobile = isMobile,173 IsLandscape = isLandscape,174 DeviceScaleFactor = scaleFactor175 };176 await page.SetViewportAsync(options);177 data.Logger.Log($"Set the viewport size to {width}x{height}", LogColors.DarkSalmon);178 }179 [Block("Gets the full DOM of the page", name = "Get DOM")]180 public static async Task<string> PuppeteerGetDOM(BotData data)181 {182 data.Logger.LogHeader();183 var page = GetPage(data);184 var dom = await page.EvaluateExpressionAsync<string>("document.body.innerHTML");185 data.Logger.Log($"Got the full page DOM", LogColors.DarkSalmon);186 data.Logger.Log(dom, LogColors.DarkSalmon, true);187 return dom;188 }189 [Block("Gets the cookies for a given domain from the browser", name = "Get Cookies")]190 public static async Task<Dictionary<string, string>> PuppeteerGetCookies(BotData data, string domain)191 {192 data.Logger.LogHeader();193 var page = GetPage(data);194 var cookies = await page.GetCookiesAsync(domain);195 data.Logger.Log($"Got {cookies.Length} cookies for {domain}", LogColors.DarkSalmon);196 return cookies.ToDictionary(c => c.Name, c => c.Value);197 }198 [Block("Sets the cookies for a given domain in the browser page", name = "Set Cookies")]199 public static async Task PuppeteerSetCookies(BotData data, string domain, Dictionary<string, string> cookies)200 {201 data.Logger.LogHeader();202 var page = GetPage(data);203 await page.SetCookieAsync(cookies.Select(c => new CookieParam { Domain = domain, Name = c.Key, Value = c.Value }).ToArray());204 data.Logger.Log($"Set {cookies.Count} cookies for {domain}", LogColors.DarkSalmon);205 }206 [Block("Sets the User Agent of the browser page", name = "Set User-Agent")]207 public static async Task PuppeteerSetUserAgent(BotData data, string userAgent)208 {209 data.Logger.LogHeader();210 var page = GetPage(data);211 await page.SetUserAgentAsync(userAgent);212 data.Logger.Log($"User Agent set to {userAgent}", LogColors.DarkSalmon);213 }214 [Block("Switches to the main frame of the page", name = "Switch to Main Frame")]215 public static void PuppeteerSwitchToMainFrame(BotData data)216 {217 data.Logger.LogHeader();218 SwitchToMainFramePrivate(data);219 data.Logger.Log($"Switched to main frame", LogColors.DarkSalmon);220 }221 [Block("Evaluates a js expression in the current page and returns a json response", name = "Execute JS")]222 public static async Task<string> PuppeteerExecuteJs(BotData data, string expression)223 {224 data.Logger.LogHeader();225 var page = GetPage(data);226 var response = await page.EvaluateExpressionAsync(expression);227 var json = response != null ? response.ToString() : "undefined";228 data.Logger.Log($"Evaluated {expression}", LogColors.DarkSalmon);229 data.Logger.Log($"Got result: {json}", LogColors.DarkSalmon);230 return json;231 }232 [Block("Captures the response from the given URL", name = "Wait for Response")]233 public static async Task PuppeteerWaitForResponse(BotData data, string url, int timeoutMilliseconds = 60000)234 {235 data.Logger.LogHeader();236 var page = GetPage(data);237 var options = new WaitForOptions238 {239 Timeout = timeoutMilliseconds240 };241 var response = await page.WaitForResponseAsync(url, options);242 data.ADDRESS = response.Url;243 data.RESPONSECODE = (int)response.Status;244 data.HEADERS = response.Headers;245 // On 3xx puppeteer returns a body missing exception246 if (((int)response.Status) / 100 != 3)247 {248 data.SOURCE = await response.TextAsync();249 data.RAWSOURCE = await response.BufferAsync();250 }251 data.Logger.Log($"Address: {data.ADDRESS}", LogColors.DodgerBlue);252 data.Logger.Log($"Response code: {data.RESPONSECODE}", LogColors.Citrine);253 data.Logger.Log("Received Headers:", LogColors.MediumPurple);254 data.Logger.Log(data.HEADERS.Select(h => $"{h.Key}: {h.Value}"), LogColors.Violet);255 data.Logger.Log("Received Payload:", LogColors.ForestGreen);256 data.Logger.Log(data.SOURCE, LogColors.GreenYellow, true);257 }258 private static PuppeteerSharp.Page GetPage(BotData data)259 => data.TryGetObject<PuppeteerSharp.Page>("puppeteerPage") ?? throw new Exception("No pages open!");260 private static void SwitchToMainFramePrivate(BotData data)261 => data.SetObject("puppeteerFrame", GetPage(data).MainFrame);262 }263}...
Response.cs
Source:Response.cs
...107 /// <summary>108 /// Returns a Task which resolves to a buffer with response body109 /// </summary>110 /// <returns>A Task which resolves to a buffer with response body</returns>111 public async Task<byte[]> BufferAsync()112 {113 if (_buffer == null)114 {115 await BodyLoadedTaskWrapper.Task.ConfigureAwait(false);116 try117 {118 var response = await _client.SendAsync<NetworkGetResponseBodyResponse>("Network.getResponseBody", new NetworkGetResponseBodyRequest119 {120 RequestId = Request.RequestId121 }).ConfigureAwait(false);122 _buffer = response.Base64Encoded123 ? Convert.FromBase64String(response.Body)124 : Encoding.UTF8.GetBytes(response.Body);125 }126 catch (Exception ex)127 {128 throw new BufferException("Unable to get response body", ex);129 }130 }131 return _buffer;132 }133 /// <summary>134 /// Returns a Task which resolves to a text representation of response body135 /// </summary>136 /// <returns>A Task which resolves to a text representation of response body</returns>137 public async Task<string> TextAsync() => Encoding.UTF8.GetString(await BufferAsync().ConfigureAwait(false));138 /// <summary>139 /// Returns a Task which resolves to a <see cref="JObject"/> representation of response body140 /// </summary>141 /// <seealso cref="JsonAsync{T}"/>142 /// <returns>A Task which resolves to a <see cref="JObject"/> representation of response body</returns>143 public async Task<JObject> JsonAsync() => JObject.Parse(await TextAsync().ConfigureAwait(false));144 /// <summary>145 /// Returns a Task which resolves to a <typeparamref name="T"/> representation of response body146 /// </summary>147 /// <typeparam name="T">The type of the response</typeparam>148 /// <seealso cref="JsonAsync"/>149 /// <returns>A Task which resolves to a <typeparamref name="T"/> representation of response body</returns>150 public async Task<T> JsonAsync<T>() => (await JsonAsync().ConfigureAwait(false)).ToObject<T>(true);151 #endregion...
DocumentsController.cs
Source:DocumentsController.cs
...60 Dictionary<string, string> headers = new Dictionary<string, string>();61 headers.Add("Accept-Charset", "utf-8");62 headers.Add("Content-Type", "text/html; charset=utf-8");63 var response = await page.GoToAsync("data:text/html," + documentData.PdfBodyHTML, WaitUntilNavigation.Networkidle0);64 await page.SetContentAsync(Encoding.UTF8.GetString(await response.BufferAsync()));65 await page.AddStyleTagAsync(new AddTagOptions { Url = documentData.PdfCssUrl });66 67 await page.SetExtraHttpHeadersAsync(headers);68 await page.EvaluateExpressionAsync("window.scrollBy(0, window.innerHeight);");69 MarginOptions marginOption;70 if (documentData.DocumentMargin == null)71 {72 marginOption = new MarginOptions73 {74 Top = _configuration.GetSection("AppSettings:Margin:Top").Value,75 Bottom = _configuration.GetSection("AppSettings:Margin:Bottom").Value,76 Left = _configuration.GetSection("AppSettings:Margin:Left").Value,77 Right = _configuration.GetSection("AppSettings:Margin:Right").Value78 };...
Program.cs
Source:Program.cs
...51 {52 //Console.WriteLine(e.Response.Url);53 if(e.Response.Url.Contains("playlist.m3u8"))54 {55 var buffer = await e.Response.BufferAsync();56 var stringContent = Encoding.ASCII.GetString(buffer).Split("\n");57 var links = stringContent.Where(line => line.StartsWith("https://")).ToList();58 linksList.AddRange(links);59 /* links.ForEach(async(link) =>60 {61 var client = new WebClient();62 var content = client.DownloadData(link);63 files.Add(content);64 });6566 byte[] result = new byte[files.Sum(a => a.Length)];67 using (var stream = new MemoryStream(result))68 {69 foreach (byte[] bytes in files)
...
ResponseBufferTests.cs
Source:ResponseBufferTests.cs
...18 public async Task ShouldWork()19 {20 var response = await Page.GoToAsync(TestConstants.ServerUrl + "/pptr.png");21 var imageBuffer = File.ReadAllBytes("./Assets/pptr.png");22 Assert.Equal(imageBuffer, await response.BufferAsync());23 }24 [PuppeteerTest("network.spec.ts", "Response.buffer", "should work with compression")]25 [SkipBrowserFact(skipFirefox: true)]26 public async Task ShouldWorkWithCompression()27 {28 Server.EnableGzip("/pptr.png");29 var response = await Page.GoToAsync(TestConstants.ServerUrl + "/pptr.png");30 var imageBuffer = File.ReadAllBytes("./Assets/pptr.png");31 Assert.Equal(imageBuffer, await response.BufferAsync());32 }33 }34}...
Issue0764.cs
Source:Issue0764.cs
...10 public Issue0764(ITestOutputHelper output) : base(output)11 {12 }13 [SkipBrowserFact(skipFirefox: true)]14 public async Task BufferAsyncShouldWorkWithBinaries()15 {16 var tcs = new TaskCompletionSource<byte[]>();17 Page.Response += async (_, e) =>18 {19 if (e.Response.Url.Contains("digits/0.png"))20 {21 tcs.TrySetResult(await e.Response.BufferAsync());22 }23 };24 await Task.WhenAll(25 Page.GoToAsync(TestConstants.ServerUrl + "/grid.html"),26 tcs.Task);27 Assert.True(ScreenshotHelper.PixelMatch("0.png", await tcs.Task));28 }29 }30}...
BufferAsync
Using AI Code Generation
1var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });2var page = await browser.NewPageAsync();3var buffer = await response.BufferAsync();4await browser.CloseAsync();5var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });6var page = await browser.NewPageAsync();7var buffer = await response.BufferAsync();8await browser.CloseAsync();9var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });10var page = await browser.NewPageAsync();11var buffer = await response.BufferAsync();12await browser.CloseAsync();13var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });14var page = await browser.NewPageAsync();15var buffer = await response.BufferAsync();16await browser.CloseAsync();17var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });18var page = await browser.NewPageAsync();19var buffer = await response.BufferAsync();20await browser.CloseAsync();21var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });
BufferAsync
Using AI Code Generation
1var page = await browser.NewPageAsync();2var buffer = await response.BufferAsync();3Console.WriteLine(buffer.Length);4var page = await browser.NewPageAsync();5var buffer = await page.BufferAsync();6Console.WriteLine(buffer.Length);7var page = await browser.NewPageAsync();8var buffer = await page.ScreenshotDataAsync();9Console.WriteLine(buffer.Length);10var page = await browser.NewPageAsync();11var buffer = await page.PdfDataAsync();12Console.WriteLine(buffer.Length);13var page = await browser.NewPageAsync();14var buffer = await page.ScreenshotStreamAsync();15Console.WriteLine(buffer.Length);16var page = await browser.NewPageAsync();17var buffer = await page.PdfStreamAsync();18Console.WriteLine(buffer.Length);19var page = await browser.NewPageAsync();20var buffer = await page.ScreenshotAsync();21Console.WriteLine(buffer.Length);22var page = await browser.NewPageAsync();23var buffer = await page.PdfAsync();24Console.WriteLine(buffer.Length);25var page = await browser.NewPageAsync();26var buffer = await page.ScreenshotDataAsync();
BufferAsync
Using AI Code Generation
1var buffer = await response.BufferAsync();2var json = await response.JsonAsync();3var text = await response.TextAsync();4var text = await response.TextAsync();5var text = await response.TextAsync();6var text = await response.TextAsync();7await page.GoToAsync("
BufferAsync
Using AI Code Generation
1var buffer = await response.BufferAsync();2var file = File.Create("google.png");3await file.WriteAsync(buffer, 0, buffer.Length);4await file.FlushAsync();5file.Close();6var buffer = response.Buffer();7var file = File.Create("google.png");8await file.WriteAsync(buffer, 0, buffer.Length);9await file.FlushAsync();10file.Close();11var buffer = await response.BodyAsync();12var file = File.Create("google.png");13await file.WriteAsync(buffer, 0, buffer.Length);14await file.FlushAsync();15file.Close();16var buffer = response.Body();17var file = File.Create("google.png");18await file.WriteAsync(buffer, 0, buffer.Length);19await file.FlushAsync();20file.Close();21var body = await response.TextAsync();22var file = File.Create("google.html");23var buffer = Encoding.UTF8.GetBytes(body);24await file.WriteAsync(buffer, 0, buffer.Length);25await file.FlushAsync();26file.Close();27var body = response.Text();28var file = File.Create("google.html");29var buffer = Encoding.UTF8.GetBytes(body);30await file.WriteAsync(buffer, 0, buffer.Length
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!