Skip to content

Commit a91803c

Browse files
authored
Optional frame in requests (#1452)
1 parent b9b3225 commit a91803c

File tree

4 files changed

+72
-5
lines changed

4 files changed

+72
-5
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.Linq;
2+
using System.Threading.Tasks;
3+
using Xunit;
4+
using Xunit.Abstractions;
5+
6+
namespace PuppeteerSharp.Tests.Issues
7+
{
8+
[Collection(TestConstants.TestFixtureCollectionName)]
9+
public class Issue1447 : PuppeteerPageBaseTest
10+
{
11+
public Issue1447(ITestOutputHelper output) : base(output) { }
12+
13+
[Fact(Skip = "It's an example")]
14+
public async Task Example()
15+
{
16+
var opts = new LaunchOptions
17+
{
18+
Headless = false,
19+
DefaultViewport = null,
20+
IgnoredDefaultArgs = new[] { "--enable-automation" }
21+
};
22+
23+
using (var browser = await new Launcher().LaunchAsync(opts))
24+
{
25+
var pages = await browser.PagesAsync();
26+
27+
var page = pages.ElementAt(0);
28+
29+
for (int i = 0; i < 20; i++)
30+
{
31+
await Navigate(page, "https://distilnetworks.com");
32+
await Navigate(page, "https://mail.com");
33+
await Navigate(page, "https://distilnetworks.com");
34+
await Navigate(page, "https://vk.com");
35+
await Navigate(page, "https://distilnetworks.com");
36+
await Navigate(page, "https://mail.com");
37+
await Navigate(page, "https://distilnetworks.com");
38+
await Navigate(page, "https://mail.com");
39+
await Navigate(page, "about:blank");
40+
}
41+
}
42+
}
43+
44+
public Task<Response> Navigate(Page page, string url)
45+
{
46+
return page.MainFrame.GoToAsync(
47+
url,
48+
new NavigationOptions { Timeout = 0, WaitUntil = new[] { WaitUntilNavigation.DOMContentLoaded } });
49+
}
50+
}
51+
}

lib/PuppeteerSharp/FrameManager.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,8 @@ private async Task EnsureIsolatedWorldAsync(string name)
431431

432432
internal Task<Frame> GetFrameAsync(string frameId) => _asyncFrames.GetItemAsync(frameId);
433433

434+
internal Task<Frame> TryGetFrameAsync(string frameId) => _asyncFrames.TryGetItemAsync(frameId);
435+
434436
#endregion
435437
}
436438
}

lib/PuppeteerSharp/Helpers/AsyncDictionaryHelper.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,21 @@ internal async Task<TValue> GetItemAsync(TKey key)
2929
}
3030

3131
return await tcs.Task.WithTimeout(new Action(() =>
32-
throw new PuppeteerException(string.Format(_timeoutMessage, key)))).ConfigureAwait(false);
32+
throw new PuppeteerException(string.Format(_timeoutMessage, key))), 1000).ConfigureAwait(false);
33+
}
34+
35+
internal async Task<TValue> TryGetItemAsync(TKey key)
36+
{
37+
var tcs = new TaskCompletionSource<TValue>(TaskCreationOptions.RunContinuationsAsynchronously);
38+
_pendingRequests.Add(key, tcs);
39+
40+
if (_dictionary.TryGetValue(key, out var item))
41+
{
42+
_pendingRequests.Delete(key, tcs);
43+
return item;
44+
}
45+
46+
return await tcs.Task.WithTimeout(() => { }, 1000).ConfigureAwait(false);
3347
}
3448

3549
internal void AddItem(TKey key, TValue value)

lib/PuppeteerSharp/NetworkManager.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ internal class NetworkManager
2222
private Dictionary<string, string> _extraHTTPHeaders;
2323
private bool _offine;
2424
private Credentials _credentials;
25-
private List<string> _attemptedAuthentications = new List<string>();
25+
private readonly List<string> _attemptedAuthentications = new List<string>();
2626
private bool _userRequestInterceptionEnabled;
2727
private bool _protocolRequestInterceptionEnabled;
28-
private bool _ignoreHTTPSErrors;
28+
private readonly bool _ignoreHTTPSErrors;
2929
private bool _userCacheDisabled;
3030
#endregion
3131

@@ -306,7 +306,7 @@ private async Task OnRequestAsync(RequestWillBeSentPayload e, string interceptio
306306
if (!_requestIdToRequest.TryGetValue(e.RequestId, out var currentRequest) ||
307307
currentRequest.Frame == null)
308308
{
309-
var frame = await FrameManager.GetFrameAsync(e.FrameId).ConfigureAwait(false);
309+
var frame = await FrameManager.TryGetFrameAsync(e.FrameId).ConfigureAwait(false);
310310

311311
request = new Request(
312312
_client,
@@ -371,7 +371,7 @@ private async Task OnRequestWillBeSentAsync(RequestWillBeSentPayload e)
371371
// Request interception doesn't happen for data URLs with Network Service.
372372
if (_protocolRequestInterceptionEnabled && !e.Request.Url.StartsWith("data:", StringComparison.InvariantCultureIgnoreCase))
373373
{
374-
if (_requestIdToInterceptionId.TryRemove(e.RequestId, out var interceptionId))
374+
if (_requestIdToInterceptionId.TryRemove(e.RequestId, out string interceptionId))
375375
{
376376
await OnRequestAsync(e, interceptionId).ConfigureAwait(false);
377377
}

0 commit comments

Comments
 (0)