Skip to content

Commit

Permalink
Optional frame in requests (#1452)
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok authored Apr 18, 2020
1 parent b9b3225 commit a91803c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 5 deletions.
51 changes: 51 additions & 0 deletions lib/PuppeteerSharp.Tests/Issues/Issue1447.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Linq;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

namespace PuppeteerSharp.Tests.Issues
{
[Collection(TestConstants.TestFixtureCollectionName)]
public class Issue1447 : PuppeteerPageBaseTest
{
public Issue1447(ITestOutputHelper output) : base(output) { }

[Fact(Skip = "It's an example")]
public async Task Example()
{
var opts = new LaunchOptions
{
Headless = false,
DefaultViewport = null,
IgnoredDefaultArgs = new[] { "--enable-automation" }
};

using (var browser = await new Launcher().LaunchAsync(opts))
{
var pages = await browser.PagesAsync();

var page = pages.ElementAt(0);

for (int i = 0; i < 20; i++)
{
await Navigate(page, "https://distilnetworks.com");
await Navigate(page, "https://mail.com");
await Navigate(page, "https://distilnetworks.com");
await Navigate(page, "https://vk.com");
await Navigate(page, "https://distilnetworks.com");
await Navigate(page, "https://mail.com");
await Navigate(page, "https://distilnetworks.com");
await Navigate(page, "https://mail.com");
await Navigate(page, "about:blank");
}
}
}

public Task<Response> Navigate(Page page, string url)
{
return page.MainFrame.GoToAsync(
url,
new NavigationOptions { Timeout = 0, WaitUntil = new[] { WaitUntilNavigation.DOMContentLoaded } });
}
}
}
2 changes: 2 additions & 0 deletions lib/PuppeteerSharp/FrameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,8 @@ private async Task EnsureIsolatedWorldAsync(string name)

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

internal Task<Frame> TryGetFrameAsync(string frameId) => _asyncFrames.TryGetItemAsync(frameId);

#endregion
}
}
16 changes: 15 additions & 1 deletion lib/PuppeteerSharp/Helpers/AsyncDictionaryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,21 @@ internal async Task<TValue> GetItemAsync(TKey key)
}

return await tcs.Task.WithTimeout(new Action(() =>
throw new PuppeteerException(string.Format(_timeoutMessage, key)))).ConfigureAwait(false);
throw new PuppeteerException(string.Format(_timeoutMessage, key))), 1000).ConfigureAwait(false);
}

internal async Task<TValue> TryGetItemAsync(TKey key)
{
var tcs = new TaskCompletionSource<TValue>(TaskCreationOptions.RunContinuationsAsynchronously);
_pendingRequests.Add(key, tcs);

if (_dictionary.TryGetValue(key, out var item))
{
_pendingRequests.Delete(key, tcs);
return item;
}

return await tcs.Task.WithTimeout(() => { }, 1000).ConfigureAwait(false);
}

internal void AddItem(TKey key, TValue value)
Expand Down
8 changes: 4 additions & 4 deletions lib/PuppeteerSharp/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ internal class NetworkManager
private Dictionary<string, string> _extraHTTPHeaders;
private bool _offine;
private Credentials _credentials;
private List<string> _attemptedAuthentications = new List<string>();
private readonly List<string> _attemptedAuthentications = new List<string>();
private bool _userRequestInterceptionEnabled;
private bool _protocolRequestInterceptionEnabled;
private bool _ignoreHTTPSErrors;
private readonly bool _ignoreHTTPSErrors;
private bool _userCacheDisabled;
#endregion

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

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

0 comments on commit a91803c

Please sign in to comment.