Best Puppeteer-sharp code snippet using PuppeteerSharp.NetworkEventManager.StoreRequest
NetworkManager.cs
Source: NetworkManager.cs
...348 await OnRequestAsync(requestWillBeSentEvent, e.RequestId).ConfigureAwait(false);349 }350 else351 {352 _networkEventManager.StoreRequestPaused(e.NetworkId, e);353 }354 }355 private async Task OnRequestAsync(RequestWillBeSentPayload e, string fetchRequestId)356 {357 Request request;358 var redirectChain = new List<Request>();359 if (e.RedirectResponse != null)360 {361 ResponseReceivedExtraInfoResponse redirectResponseExtraInfo = null;362 if (e.RedirectHasExtraInfo)363 {364 redirectResponseExtraInfo = _networkEventManager.ShiftResponseExtraInfo(e.RequestId);365 if (redirectResponseExtraInfo == null)366 {367 _networkEventManager.QueueRedirectInfo(e.RequestId, new()368 {369 Event = e,370 FetchRequestId = fetchRequestId,371 });372 return;373 }374 }375 request = _networkEventManager.GetRequest(e.RequestId);376 // If we connect late to the target, we could have missed the requestWillBeSent event.377 if (request != null)378 {379 HandleRequestRedirect(request, e.RedirectResponse, redirectResponseExtraInfo);380 redirectChain = request.RedirectChainList;381 }382 }383 var frame = !string.IsNullOrEmpty(e.FrameId) ? await FrameManager.TryGetFrameAsync(e.FrameId).ConfigureAwait(false) : null;384 request = new Request(385 _client,386 frame,387 fetchRequestId,388 _userRequestInterceptionEnabled,389 e,390 redirectChain);391 _networkEventManager.StoreRequest(e.RequestId, request);392 Request?.Invoke(this, new RequestEventArgs393 {394 Request = request395 });396 }397 private void OnRequestServedFromCache(RequestServedFromCacheResponse response)398 {399 var request = _networkEventManager.GetRequest(response.RequestId);400 if (request != null)401 {402 request.FromMemoryCache = true;403 }404 RequestServedFromCache?.Invoke(this, new RequestEventArgs { Request = request });405 }406 private void HandleRequestRedirect(Request request, ResponsePayload responseMessage, ResponseReceivedExtraInfoResponse extraInfo)407 {408 var response = new Response(409 _client,410 request,411 responseMessage,412 extraInfo);413 request.Response = response;414 request.RedirectChainList.Add(request);415 response.BodyLoadedTaskWrapper.TrySetException(416 new PuppeteerException("Response body is unavailable for redirect responses"));417 ForgetRequest(request, false);418 Response?.Invoke(this, new ResponseCreatedEventArgs419 {420 Response = response421 });422 RequestFinished?.Invoke(this, new RequestEventArgs423 {424 Request = request425 });426 }427 private async Task OnRequestWillBeSentAsync(RequestWillBeSentPayload e)428 {429 // Request interception doesn't happen for data URLs with Network Service.430 if (_userRequestInterceptionEnabled && !e.Request.Url.StartsWith("data:", StringComparison.InvariantCultureIgnoreCase))431 {432 _networkEventManager.StoreRequestWillBeSent(e.RequestId, e);433 var requestPausedEvent = _networkEventManager.GetRequestPaused(e.RequestId);434 if (requestPausedEvent != null)435 {436 var fetchRequestId = requestPausedEvent.RequestId;437 PatchRequestEventHeaders(e, requestPausedEvent);438 await OnRequestAsync(e, fetchRequestId).ConfigureAwait(false);439 _networkEventManager.ForgetRequestPaused(e.RequestId);440 }441 return;442 }443 await OnRequestAsync(e, null).ConfigureAwait(false);444 }445 private void PatchRequestEventHeaders(RequestWillBeSentPayload requestWillBeSentEvent, FetchRequestPausedResponse requestPausedEvent)446 {...
NetworkEventManager.cs
Source: NetworkEventManager.cs
...69 list.Remove(result);70 }71 return result;72 }73 internal void StoreRequestWillBeSent(string networkRequestId, RequestWillBeSentPayload e)74 => _requestWillBeSentMap.AddOrUpdate(networkRequestId, e, (_, __) => e);75 internal RequestWillBeSentPayload GetRequestWillBeSent(string networkRequestId)76 {77 _requestWillBeSentMap.TryGetValue(networkRequestId, out var result);78 return result;79 }80 internal void ForgetRequestWillBeSent(string networkRequestId)81 => _requestWillBeSentMap.TryRemove(networkRequestId, out _);82 internal FetchRequestPausedResponse GetRequestPaused(string networkRequestId)83 {84 _requestPausedMap.TryGetValue(networkRequestId, out var result);85 return result;86 }87 internal void ForgetRequestPaused(string networkRequestId)88 => _requestPausedMap.TryRemove(networkRequestId, out _);89 internal void StoreRequestPaused(string networkRequestId, FetchRequestPausedResponse e)90 => _requestPausedMap.AddOrUpdate(networkRequestId, e, (_, __) => e);91 internal Request GetRequest(string networkRequestId)92 {93 _httpRequestsMap.TryGetValue(networkRequestId, out var result);94 return result;95 }96 internal void StoreRequest(string networkRequestId, Request request)97 => _httpRequestsMap.AddOrUpdate(networkRequestId, request, (_, __) => request);98 internal void ForgetRequest(string requestId)99 => _requestWillBeSentMap.TryRemove(requestId, out _);100 internal void QueuedEventGroup(string networkRequestId, QueuedEventGroup group)101 => _queuedEventGroupMap.AddOrUpdate(networkRequestId, group, (_, __) => group);102 internal QueuedEventGroup GetQueuedEventGroup(string networkRequestId)103 {104 _queuedEventGroupMap.TryGetValue(networkRequestId, out var result);105 return result;106 }107 // Puppeteer doesn't have this. but it seems that .NET needs this to avoid race conditions108 internal void ForgetQueuedEventGroup(string networkRequestId)109 => _queuedEventGroupMap.TryRemove(networkRequestId, out _);110 }...
StoreRequest
Using AI Code Generation
1using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions2 {3 }))4 using (var page = await browser.NewPageAsync())5 {6 page.NetworkManager.StoreRequest(new Request7 {8 });9 }
StoreRequest
Using AI Code Generation
1using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false }))2{3 var page = await browser.NewPageAsync();4 await page.WaitForSelectorAsync("input[title='Search']");5 page.NetworkManager.StoreRequest += (sender, args) =>6 {7 };8 await page.TypeAsync("input[title='Search']", "Puppeteer");9 await page.ClickAsync("input[value='Google Search']");10 await page.WaitForNavigationAsync();11 await page.ScreenshotAsync("1.png");12}13using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false }))14{15 var page = await browser.NewPageAsync();16 await page.WaitForSelectorAsync("input[title='Search']");17 page.NetworkManager.StoreResponse += (sender, args) =>18 {19 };20 await page.TypeAsync("input[title='Search']", "Puppeteer");21 await page.ClickAsync("input[value='Google Search']");22 await page.WaitForNavigationAsync();23 await page.ScreenshotAsync("2.png");24}25using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false }))26{27 var page = await browser.NewPageAsync();28 await page.WaitForSelectorAsync("input[title='Search']");29 page.NetworkManager.RequestFinished += (sender, args) =>30 {31 Console.WriteLine(args.Request.Url);32 };33 await page.TypeAsync("input[title='Search']", "Puppeteer");34 await page.ClickAsync("input[value='Google Search']");35 await page.WaitForNavigationAsync();36 await page.ScreenshotAsync("3.png");37}38using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false }))39{
StoreRequest
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4using System.IO;5{6 {7 static async Task Main(string[] args)8 {9 var options = new LaunchOptions { Headless = true };10 using (var browser = await Puppeteer.LaunchAsync(options))11 {12 using (var page = await browser.NewPageAsync())13 {14 page.Request += async (sender, e) =>15 {16 await e.Request.ContinueAsync();17 await e.Request.StoreRequestAsync();18 };19 await page.ClickAsync("a[href='/maps']");20 await page.WaitForNavigationAsync();21 await page.ClickAsync("a[href='/maps/place/']");22 await page.WaitForNavigationAsync();23 await page.ClickAsync("input[aria-label='Search Google Maps']");24 await page.Keyboard.TypeAsync("My Home");25 await page.Keyboard.PressAsync("Enter");26 await page.WaitForNavigationAsync();27 await page.ClickAsync("a[href='/maps/place/']");28 await page.WaitForNavigationAsync();29 await page.ClickAsync("input[aria-label='Search Google Maps']");30 await page.Keyboard.TypeAsync("My Office");31 await page.Keyboard.PressAsync("Enter");32 await page.WaitForNavigationAsync();33 await page.ClickAsync("a[href='/maps/place/']");34 await page.WaitForNavigationAsync();35 await page.ClickAsync("input[aria-label='Search Google Maps']");36 await page.Keyboard.TypeAsync("My School");37 await page.Keyboard.PressAsync("Enter");38 await page.WaitForNavigationAsync();39 await page.ClickAsync("a[href='/maps/place/']");40 await page.WaitForNavigationAsync();41 await page.ClickAsync("input[aria-label='Search Google Maps']");42 await page.Keyboard.TypeAsync("My Friend's Home");43 await page.Keyboard.PressAsync("Enter");44 await page.WaitForNavigationAsync();45 await page.ClickAsync("a[href='/maps/place/']");46 await page.WaitForNavigationAsync();47 await page.ClickAsync("input[aria-label='Search Google Maps']");48 await page.Keyboard.TypeAsync("My Friend's Office");49 await page.Keyboard.PressAsync("Enter");50 await page.WaitForNavigationAsync();51 await page.ClickAsync("a[href='/maps/place/']");52 await page.WaitForNavigationAsync();
StoreRequest
Using AI Code Generation
1var networkManager = page.NetworkManager;2await page.WaitForNavigationAsync();3await page.WaitForNavigationAsync();4await page.WaitForNavigationAsync();5await page.WaitForNavigationAsync();6await page.WaitForNavigationAsync();7await page.WaitForNavigationAsync();8await page.WaitForNavigationAsync();9await page.WaitForNavigationAsync();10await page.WaitForNavigationAsync();11await page.WaitForNavigationAsync();12await page.WaitForNavigationAsync();13await page.WaitForNavigationAsync();14await page.WaitForNavigationAsync();15await page.WaitForNavigationAsync();16await page.WaitForNavigationAsync();17await page.WaitForNavigationAsync();18await page.WaitForNavigationAsync();19await page.WaitForNavigationAsync();20await page.WaitForNavigationAsync();
Failed to launch Base! /app/.local-chromium/Linux-706915/chrome-linux/chrome: ... libX11.so.6
Puppeteer renders blank pages when not setting breakpoints
How can I get computed style property from element with Pupeetersharp
Launch Tor Browser with Puppeteer-sharp
How to fill a form inside an iframe with Puppeteer-Sharp
How can I get cookies from Puppeteer chrome session C#?
PuppeteerSharp Number of Chromium Instances
How does puppeter sharp get the value of an attribute?
Dynamic website scraping not picking items in C#
Puppeteer-sharp: page is crashed from browser.NewPageAsync()
Ok, turns out it was a mix of things. The dockerfile needed rearranging and works best without a sandbox. As you'll see, the original code was installing dependencies on the intermediate image rather then the final/base.
For some reason, I couldn't get it to work under a sandboxed user properly, so that was removed. As a result the implementation needed updating to enable the --no-sandbox argument.
Here's the updated code for anyone else struggling to get puppeteer-sharp working with .NET Core 3.1.
dockerfile
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
#####################
#PUPPETEER RECIPE
#####################
# Install latest chrome dev package and fonts to support major charsets (Chinese, Japanese, Arabic, Hebrew, Thai and a few others)
# Note: this installs the necessary libs to make the bundled version of Chromium that Puppeteer
# installs, work.
ARG CHROME_VERSION="81.0.4044.138-1"
RUN apt-get update && apt-get -f install && apt-get -y install wget gnupg2 apt-utils
RUN wget --no-verbose -O /tmp/chrome.deb http://dl.google.com/linux/chrome/deb/pool/main/g/google-chrome-stable/google-chrome-stable_${CHROME_VERSION}_amd64.deb \
&& apt-get update \
&& apt-get install -y /tmp/chrome.deb --no-install-recommends --allow-downgrades fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf \
&& rm /tmp/chrome.deb
ENV PUPPETEER_EXECUTABLE_PATH "/usr/bin/google-chrome"
#####################
#END PUPPETEER RECIPE
#####################
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["ProjectName.Core/ProjectName.Core.csproj", "ProjectName.Core/"]
RUN dotnet restore "ProjectName.Core/ProjectName.Core.csproj"
COPY . .
WORKDIR "/src/ProjectName.Core"
RUN dotnet build "ProjectName.Core.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "ProjectName.Core.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ProjectName.Core.dll"]
Implementation
public static async Task<Stream> GeneratePdfFromHtml(string html, List<string> styleUrls = null)
{
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
Args = new[] { "--no-sandbox" },
Headless = true
});
using (var page = await browser.NewPageAsync())
{
await page.SetContentAsync(html);
if(styleUrls != null)
foreach (var url in styleUrls)
{
await page.AddStyleTagAsync(new AddTagOptions { Path = url });
}
return await page.PdfStreamAsync();
}
}
It's probably worth noting that the docker implementation of puppeteer-sharp also seems to have a problem with finding stylesheets using a local url (e.g. localhost:1234/lib/bootsrap/bootsrap.min.css). It just hangs and spins forever, hence changing the AddStyleTagAsync()
to use filepaths instead. Don't forget, this is found under IHostingEnvironment.WebRootPath to save you a google search.
Hope this helps someone else out there. No doubt it'll cross my path again at some point in the not-so-distant future too!
Check out the latest blogs from LambdaTest on this topic:
In general, software testers have a challenging job. Software testing is frequently the final significant activity undertaken prior to actually delivering a product. Since the terms “software” and “late” are nearly synonymous, it is the testers that frequently catch the ire of the whole business as they try to test the software at the end. It is the testers who are under pressure to finish faster and deem the product “release candidate” before they have had enough opportunity to be comfortable. To make matters worse, if bugs are discovered in the product after it has been released, everyone looks to the testers and says, “Why didn’t you spot those bugs?” The testers did not cause the bugs, but they must bear some of the guilt for the bugs that were disclosed.
In my last blog, I investigated both the stateless and the stateful class of model-based testing. Both have some advantages and disadvantages. You can use them for different types of systems, depending on whether a stateful solution is required or a stateless one is enough. However, a better solution is to use an aggregate technique that is appropriate for each system. Currently, the only aggregate solution is action-state testing, introduced in the book Paradigm Shift in Software Testing. This method is implemented in Harmony.
Anyone who has worked in the software industry for a while can tell you stories about projects that were on the verge of failure. Many initiatives fail even before they reach clients, which is especially disheartening when the failure is fully avoidable.
API (Application Programming Interface) is a set of definitions and protocols for building and integrating applications. It’s occasionally referred to as a contract between an information provider and an information user establishing the content required from the consumer and the content needed by the producer.
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!!