How to use Forget method of PuppeteerSharp.NetworkEventManager class

Best Puppeteer-sharp code snippet using PuppeteerSharp.NetworkEventManager.Forget

NetworkManager.cs

Source:NetworkManager.cs Github

copy

Full Screen

...171 if (queuedEvents.LoadingFailedEvent != null) {172 OnLoadingFailed(queuedEvents.LoadingFailedEvent);173 }174 // We need this in .NET to avoid race conditions175 _networkEventManager.ForgetQueuedEventGroup(e.RequestId);176 return;177 }178 // Wait until we get another event that can use this ExtraInfo event.179 _networkEventManager.ResponseExtraInfo(e.RequestId).Add(e);180 }181 private void OnLoadingFailed(LoadingFailedEventResponse e)182 {183 var queuedEvents = _networkEventManager.GetQueuedEventGroup(e.RequestId);184 if (queuedEvents != null)185 {186 queuedEvents.LoadingFailedEvent = e;187 }188 else189 {190 EmitLoadingFailed(e);191 }192 }193 private void EmitLoadingFailed(LoadingFailedEventResponse e)194 {195 var request = _networkEventManager.GetRequest(e.RequestId);196 if (request == null)197 {198 return;199 }200 request.Failure = e.ErrorText;201 request.Response?.BodyLoadedTaskWrapper.TrySetResult(true);202 ForgetRequest(request, true);203 RequestFailed?.Invoke(this, new RequestEventArgs204 {205 Request = request206 });207 }208 private void OnLoadingFinished(LoadingFinishedEventResponse e)209 {210 var queuedEvents = _networkEventManager.GetQueuedEventGroup(e.RequestId);211 if (queuedEvents != null)212 {213 queuedEvents.LoadingFinishedEvent = e;214 }215 else216 {217 EmitLoadingFinished(e);218 }219 }220 private void EmitLoadingFinished(LoadingFinishedEventResponse e)221 {222 var request = _networkEventManager.GetRequest(e.RequestId);223 if (request == null)224 {225 return;226 }227 request.Response?.BodyLoadedTaskWrapper.TrySetResult(true);228 ForgetRequest(request, true);229 RequestFinished?.Invoke(this, new RequestEventArgs230 {231 Request = request232 });233 }234 private void ForgetRequest(Request request, bool events)235 {236 _networkEventManager.ForgetRequest(request.RequestId);237 if (request.InterceptionId != null)238 {239 _attemptedAuthentications.Remove(request.InterceptionId);240 }241 if (events)242 {243 _networkEventManager.Forget(request.RequestId);244 }245 }246 private void OnResponseReceived(ResponseReceivedResponse e)247 {248 var request = _networkEventManager.GetRequest(e.RequestId);249 ResponseReceivedExtraInfoResponse extraInfo = null;250 if (request != null && !request.FromMemoryCache && e.HasExtraInfo)251 {252 extraInfo = _networkEventManager.ShiftResponseExtraInfo(e.RequestId);253 if (extraInfo == null)254 {255 _networkEventManager.QueuedEventGroup(e.RequestId, new()256 {257 ResponseReceivedEvent = e258 });259 return;260 }261 }262 EmitResponseEvent(e, extraInfo);263 }264 private void EmitResponseEvent(ResponseReceivedResponse e, ResponseReceivedExtraInfoResponse extraInfo)265 {266 var request = _networkEventManager.GetRequest(e.RequestId);267 // FileUpload sends a response without a matching request.268 if (request == null)269 {270 return;271 }272 var response = new Response(273 _client,274 request,275 e.Response,276 extraInfo);277 request.Response = response;278 Response?.Invoke(this, new ResponseCreatedEventArgs279 {280 Response = response281 });282 }283 private async Task OnAuthRequiredAsync(FetchAuthRequiredResponse e)284 {285 var response = "Default";286 if (_attemptedAuthentications.Contains(e.RequestId))287 {288 response = "CancelAuth";289 }290 else if (_credentials != null)291 {292 response = "ProvideCredentials";293 _attemptedAuthentications.Add(e.RequestId);294 }295 var credentials = _credentials ?? new Credentials();296 try297 {298 await _client.SendAsync("Fetch.continueWithAuth", new ContinueWithAuthRequest299 {300 RequestId = e.RequestId,301 AuthChallengeResponse = new ContinueWithAuthRequestChallengeResponse302 {303 Response = response,304 Username = credentials.Username,305 Password = credentials.Password306 }307 }).ConfigureAwait(false);308 }309 catch (PuppeteerException ex)310 {311 _logger.LogError(ex.ToString());312 }313 }314 private async Task OnRequestPausedAsync(FetchRequestPausedResponse e)315 {316 if (!_userRequestInterceptionEnabled && _protocolRequestInterceptionEnabled)317 {318 try319 {320 await _client.SendAsync("Fetch.continueRequest", new FetchContinueRequestRequest321 {322 RequestId = e.RequestId323 }).ConfigureAwait(false);324 }325 catch (PuppeteerException ex)326 {327 _logger.LogError(ex.ToString());328 }329 }330 if (string.IsNullOrEmpty(e.NetworkId))331 {332 return;333 }334 var requestWillBeSentEvent =335 _networkEventManager.GetRequestWillBeSent(e.NetworkId);336 // redirect requests have the same `requestId`,337 if (338 requestWillBeSentEvent != null &&339 (requestWillBeSentEvent.Request.Url != e.Request.Url ||340 requestWillBeSentEvent.Request.Method != e.Request.Method))341 {342 _networkEventManager.ForgetRequestWillBeSent(e.NetworkId);343 requestWillBeSentEvent = null;344 }345 if (requestWillBeSentEvent != null)346 {347 PatchRequestEventHeaders(requestWillBeSentEvent, e);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 {447 foreach (var kv in requestPausedEvent.Request.Headers)448 {449 requestWillBeSentEvent.Request.Headers[kv.Key] = kv.Value;450 }451 }452 private async Task UpdateProtocolRequestInterceptionAsync()453 {...

Full Screen

Full Screen

NetworkEventManager.cs

Source:NetworkEventManager.cs Github

copy

Full Screen

...14 private readonly ConcurrentDictionary<string, List<RedirectInfo>> _queuedRedirectInfoMap = new();15 private readonly ConcurrentDictionary<string, List<ResponseReceivedExtraInfoResponse>> _responseReceivedExtraInfoMap = new();16 public int NumRequestsInProgress17 => _httpRequestsMap.Values.Count(r => r.Response == null);18 internal void Forget(string requestId)19 {20 _requestWillBeSentMap.TryRemove(requestId, out _);21 _requestPausedMap.TryRemove(requestId, out _);22 _queuedEventGroupMap.TryRemove(requestId, out _);23 _queuedRedirectInfoMap.TryRemove(requestId, out _);24 _responseReceivedExtraInfoMap.TryRemove(requestId, out _);25 }26 internal List<ResponseReceivedExtraInfoResponse> ResponseExtraInfo(string networkRequestId)27 {28 if (!_responseReceivedExtraInfoMap.ContainsKey(networkRequestId))29 {30 _responseReceivedExtraInfoMap.AddOrUpdate(31 networkRequestId,32 new List<ResponseReceivedExtraInfoResponse>(),33 (_, __) => new List<ResponseReceivedExtraInfoResponse>());34 }35 _responseReceivedExtraInfoMap.TryGetValue(networkRequestId, out var result);36 return result;37 }38 private List<RedirectInfo> QueuedRedirectInfo(string fetchRequestId)39 {40 if (!_queuedRedirectInfoMap.ContainsKey(fetchRequestId))41 {42 _queuedRedirectInfoMap.TryAdd(fetchRequestId, new List<RedirectInfo>());43 }44 _queuedRedirectInfoMap.TryGetValue(fetchRequestId, out var result);45 return result;46 }47 internal void QueueRedirectInfo(string fetchRequestId, RedirectInfo redirectInfo)48 => QueuedRedirectInfo(fetchRequestId).Add(redirectInfo);49 internal RedirectInfo TakeQueuedRedirectInfo(string fetchRequestId)50 {51 var list = QueuedRedirectInfo(fetchRequestId);52 var result = list.FirstOrDefault();53 if (result != null)54 {55 list.Remove(result);56 }57 return result;58 }59 internal ResponseReceivedExtraInfoResponse ShiftResponseExtraInfo(string networkRequestId)60 {61 if (!_responseReceivedExtraInfoMap.ContainsKey(networkRequestId))62 {63 _responseReceivedExtraInfoMap.TryAdd(networkRequestId, new List<ResponseReceivedExtraInfoResponse>());64 }65 _responseReceivedExtraInfoMap.TryGetValue(networkRequestId, out var list);66 var result = list.FirstOrDefault();67 if (result != null)68 {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 }111}...

Full Screen

Full Screen

Forget

Using AI Code Generation

copy

Full Screen

1var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false });2var page = await browser.NewPageAsync();3await page.WaitForSelectorAsync("input[name='q']");4await page.Keyboard.TypeAsync("puppeteer-sharp", new TypeOptions { Delay = 100 });5await page.Keyboard.PressAsync("Enter");6await page.WaitForNavigationAsync();7await page.WaitForSelectorAsync("#resultStats");8var networkManager = page.NetworkManager;9networkManager.Request += (sender, e) => Console.WriteLine(e.Request.Url);10networkManager.Response += (sender, e) => Console.WriteLine(e.Response.Url);11networkManager.RequestFailed += (sender, e) => Console.WriteLine(e.Request.Url);12networkManager.RequestFinished += (sender, e) => Console.WriteLine(e.Request.Url);13await page.ClickAsync("a[href='/intl/en/about/']");14await page.WaitForNavigationAsync();15await page.ClickAsync("a[href='/intl/en/policies/']");16await page.WaitForNavigationAsync();17await browser.CloseAsync();

Full Screen

Full Screen

Forget

Using AI Code Generation

copy

Full Screen

1var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false });2var page = await browser.NewPageAsync();3await page.WaitForNavigationAsync();4await page.EvaluateExpressionAsync("document.querySelector('input').value='PuppeteerSharp'");5await page.Keyboard.PressAsync("Enter");6await page.WaitForNavigationAsync();7await page.CloseAsync();8await browser.CloseAsync();9var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false });10var page = await browser.NewPageAsync();11await page.WaitForNavigationAsync();12await page.EvaluateExpressionAsync("document.querySelector('input').value='PuppeteerSharp'");13await page.Keyboard.PressAsync("Enter");14await page.WaitForNavigationAsync();15await page.CloseAsync();16await browser.CloseAsync();17var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false });18var page = await browser.NewPageAsync();19await page.WaitForNavigationAsync();20await page.EvaluateExpressionAsync("document.querySelector('input').value='PuppeteerSharp'");21await page.Keyboard.PressAsync("Enter");22await page.WaitForNavigationAsync();23await page.CloseAsync();24await browser.CloseAsync();25var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false });26var page = await browser.NewPageAsync();27await page.WaitForNavigationAsync();28await page.EvaluateExpressionAsync("document.querySelector('input').value='PuppeteerSharp'");29await page.Keyboard.PressAsync("Enter");30await page.WaitForNavigationAsync();31await page.CloseAsync();32await browser.CloseAsync();33var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false });34var page = await browser.NewPageAsync();35await page.WaitForNavigationAsync();36await page.EvaluateExpressionAsync("document.querySelector('input').value='PuppeteerSharp

Full Screen

Full Screen

Forget

Using AI Code Generation

copy

Full Screen

1var page = await browser.NewPageAsync();2await page.WaitForNavigationAsync();3await page.ForgetAsync("requestWillBeSent");4await page.ForgetAsync("responseReceived");5await page.ForgetAsync("loadingFinished");6await page.WaitForNavigationAsync();7var page = await browser.NewPageAsync();8await page.WaitForNavigationAsync();9await page.OnRequestAsync(request => request.ContinueAsync());10await page.OnResponseAsync(response => response.ContinueAsync());11await page.WaitForNavigationAsync();12var page = await browser.NewPageAsync();13await page.WaitForNavigationAsync();14await page.OnRequestFinishedAsync(request => request.ContinueAsync());15await page.OnResponseAsync(response => response.ContinueAsync());16await page.WaitForNavigationAsync();17var page = await browser.NewPageAsync();18await page.WaitForNavigationAsync();19await page.OnRequestFailedAsync(request => request.ContinueAsync());20await page.OnResponseAsync(response => response.ContinueAsync());21await page.WaitForNavigationAsync();22var page = await browser.NewPageAsync();23await page.WaitForNavigationAsync();24await page.OnResponseAsync(response => response.ContinueAsync());25await page.WaitForNavigationAsync();26var page = await browser.NewPageAsync();27await page.WaitForNavigationAsync();28await page.OnRequestAsync(request => request.ContinueAsync());29await page.GoToAsync("https

Full Screen

Full Screen

Forget

Using AI Code Generation

copy

Full Screen

1var options = new LaunchOptions { Headless = false };2var browser = await Puppeteer.LaunchAsync(options);3var page = await browser.NewPageAsync();4var client = await page.Target.CreateCDPSessionAsync();5var networkManager = client.NetworkManager;6await networkManager.ForgetAsync();7await page.ScreenshotAsync("google.png");8await browser.CloseAsync();9var options = new LaunchOptions { Headless = false };10var browser = await Puppeteer.LaunchAsync(options);11var page = await browser.NewPageAsync();12var client = await page.Target.CreateCDPSessionAsync();13var networkManager = client.NetworkManager;14await networkManager.ForgetAsync(request);15await page.ScreenshotAsync("google.png");16await browser.CloseAsync();17var options = new LaunchOptions { Headless = false };18var browser = await Puppeteer.LaunchAsync(options);19var page = await browser.NewPageAsync();20var client = await page.Target.CreateCDPSessionAsync();21var networkManager = client.NetworkManager;22await networkManager.ForgetAsync(new []{request});23await page.ScreenshotAsync("google.png");24await browser.CloseAsync();25var options = new LaunchOptions { Headless = false };26var browser = await Puppeteer.LaunchAsync(options);27var page = await browser.NewPageAsync();28var client = await page.Target.CreateCDPSessionAsync();29var networkManager = client.NetworkManager;30await networkManager.ForgetAsync(new []{request});31await page.ScreenshotAsync("google.png");32await browser.CloseAsync();33var options = new LaunchOptions { Headless = false };34var browser = await Puppeteer.LaunchAsync(options);

Full Screen

Full Screen

Forget

Using AI Code Generation

copy

Full Screen

1var page = await browser.NewPageAsync();2await page.WaitForNavigationAsync();3await page.EvaluateExpressionAsync("document.querySelector('input[type=text]').value = 'puppeteer-sharp'");4await page.Keyboard.PressAsync("Enter");5await page.WaitForNavigationAsync();6await page.ForgetAsync("request", (Request req) =>7{8 return req.Url.Contains("google");9});10await page.EvaluateExpressionAsync("document.querySelector('input[type=text]').value = 'puppeteer-sharp'");11await page.Keyboard.PressAsync("Enter");12await page.WaitForNavigationAsync();13await page.ForgetAsync("request", (Request req) =>14{15 return req.Url.Contains("google");16});17await page.CloseAsync();18var page = await browser.NewPageAsync();19await page.WaitForNavigationAsync();20await page.EvaluateExpressionAsync("document.querySelector('input[type=text]').value = 'puppeteer-sharp'");21await page.Keyboard.PressAsync("Enter");22await page.WaitForNavigationAsync();23await page.NetworkManager.ForgetAsync("request", (Request req) =>24{25 return req.Url.Contains("google");26});27await page.EvaluateExpressionAsync("document.querySelector('input[type=text]').value = 'puppeteer-sharp'");28await page.Keyboard.PressAsync("Enter");29await page.WaitForNavigationAsync();30await page.NetworkManager.ForgetAsync("request", (Request req) =>31{32 return req.Url.Contains("google");33});34await page.CloseAsync();35var page = await browser.NewPageAsync();36await page.WaitForNavigationAsync();37await page.EvaluateExpressionAsync("document.querySelector('input[type=text]').value = 'puppeteer-sharp'");38await page.Keyboard.PressAsync("Enter");39await page.WaitForNavigationAsync();40await page.ForgetAsync("request", (Request req) =>41{42 return req.Url.Contains("google");43});44await page.EvaluateExpressionAsync("document.querySelector('input[type=text]').value = 'puppeteer-sharp'");45await page.Keyboard.PressAsync("Enter");46await page.WaitForNavigationAsync();47await page.ForgetAsync("request", (Request req) =>48{49 return req.Url.Contains("google");50});

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful