Skip to content

Commit 76533e2

Browse files
authored
Bidi: SetCacheEnabled support (#3017)
1 parent 87f8d67 commit 76533e2

File tree

6 files changed

+67
-59
lines changed

6 files changed

+67
-59
lines changed

lib/.claude/commands/implement-bidi-feature.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ In order to do that you will need to:
1111
* You are going to look for tests that might match that `testIdPattern` but looking at the test attribute.
1212
- For instance if the `testIdPattern` is `Chromium-Specific *`, `[Test, PuppeteerTest("chromiumonly.spec", "Chromium-Specific Page Tests", "Page.setRequestInterception should work with intervention headers")]` will be a valid match.
1313
* Once you see what fail in those tests you will need to modify the code to make the test pass. You should not edit the tests themselves.
14-
* You will find the code in typescript implementing the feature at ../../puppeteer/puppeteer and you will need to port that code to .NET.
15-
* You will also find BidiDriver code at ../../puppeteer/bidi-client you need to check what you need to use from there to make the tests pass.
14+
* You will find the code in typescript implementing the feature at ../../../puppeteer/puppeteer and you will need to port that code to .NET.
15+
* You will also find BidiDriver code at ../../../webdriverbidi-net/webdriverbidi-net you need to check what you need to use from there to make the tests pass.
1616
* You will have to implement the code as close as possible to the original code, but adapted to .NET idioms and practices.
1717
* As part of the task you will need to generate a document explaining the changes you made, and how they relate to the original PR.
1818
* You need to run related tests to ensure everything is working as expected.

lib/PuppeteerSharp.Nunit/TestExpectations/TestExpectations.local.json

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -982,36 +982,6 @@
982982
"FAIL"
983983
]
984984
},
985-
{
986-
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
987-
"testIdPattern": "[page.spec] *Page.Events.error*",
988-
"platforms": [
989-
"darwin",
990-
"linux",
991-
"win32"
992-
],
993-
"parameters": [
994-
"webDriverBiDi"
995-
],
996-
"expectations": [
997-
"FAIL"
998-
]
999-
},
1000-
{
1001-
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
1002-
"testIdPattern": "[page.spec] *Page.Events.Load*",
1003-
"platforms": [
1004-
"darwin",
1005-
"linux",
1006-
"win32"
1007-
],
1008-
"parameters": [
1009-
"webDriverBiDi"
1010-
],
1011-
"expectations": [
1012-
"FAIL"
1013-
]
1014-
},
1015985
{
1016986
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
1017987
"testIdPattern": "[page.spec] *Page.removeExposedFunction*",
@@ -1042,21 +1012,6 @@
10421012
"FAIL"
10431013
]
10441014
},
1045-
{
1046-
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
1047-
"testIdPattern": "[page.spec] *Page.setCacheEnabled*",
1048-
"platforms": [
1049-
"darwin",
1050-
"linux",
1051-
"win32"
1052-
],
1053-
"parameters": [
1054-
"webDriverBiDi"
1055-
],
1056-
"expectations": [
1057-
"FAIL"
1058-
]
1059-
},
10601015
{
10611016
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
10621017
"testIdPattern": "[page.spec] *Page.setJavaScriptEnabled*",

lib/PuppeteerSharp.Tests/PageTests/PageEventsErrorTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public async Task ShouldThrowWhenPageCrashes()
1515
{
1616
string error = null;
1717
Page.Error += (_, e) => error = e.Error;
18-
var gotoTask = Page.GoToAsync("chrome://crash");
18+
var crashUrl = TestConstants.IsChrome ? "chrome://crash" : "about:crashcontent";
19+
var gotoTask = Page.GoToAsync(crashUrl);
1920

2021
await WaitForError();
2122
Assert.That(error, Is.EqualTo("Page crashed!"));

lib/PuppeteerSharp/Bidi/BidiPage.cs

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
using PuppeteerSharp.Helpers;
3131
using PuppeteerSharp.Media;
3232
using WebDriverBiDi.BrowsingContext;
33+
using WebDriverBiDi.Network;
3334

3435
namespace PuppeteerSharp.Bidi;
3536

@@ -40,8 +41,9 @@ public class BidiPage : Page
4041
private readonly CdpEmulationManager _cdpEmulationManager;
4142
private InternalNetworkConditions _emulatedNetworkConditions;
4243
private TaskCompletionSource<bool> _closedTcs;
44+
private string _requestInterception;
4345

44-
internal BidiPage(BidiBrowserContext browserContext, BrowsingContext browsingContext) : base(browserContext.ScreenshotTaskQueue)
46+
private BidiPage(BidiBrowserContext browserContext, BrowsingContext browsingContext) : base(browserContext.ScreenshotTaskQueue)
4547
{
4648
BrowserContext = browserContext;
4749
Browser = browserContext.Browser;
@@ -267,7 +269,16 @@ public override async Task SetGeolocationAsync(GeolocationOption options)
267269
public override Task SetBypassCSPAsync(bool enabled) => throw new NotImplementedException();
268270

269271
/// <inheritdoc />
270-
public override Task SetCacheEnabledAsync(bool enabled = true) => throw new NotImplementedException();
272+
public override async Task SetCacheEnabledAsync(bool enabled = true)
273+
{
274+
var commandParameters = new SetCacheBehaviorCommandParameters
275+
{
276+
CacheBehavior = enabled ? CacheBehavior.Default : CacheBehavior.Bypass,
277+
Contexts = [BidiMainFrame.BrowsingContext.Id],
278+
};
279+
280+
await BidiMainFrame.BrowsingContext.Session.Driver.Network.SetCacheBehaviorAsync(commandParameters).ConfigureAwait(false);
281+
}
271282

272283
/// <inheritdoc />
273284
public override Task EmulateMediaTypeAsync(MediaType type) => throw new NotImplementedException();
@@ -291,14 +302,14 @@ public override async Task SetViewportAsync(ViewPortOptions viewport)
291302
await BidiMainFrame.BrowsingContext.SetViewportAsync(
292303
new SetViewportOptions()
293304
{
294-
Viewport = viewport?.Width > 0 && viewport?.Height > 0
295-
? new WebDriverBiDi.BrowsingContext.Viewport()
305+
Viewport = viewport is { Width: > 0, Height: > 0 }
306+
? new Viewport()
296307
{
297308
Width = (ulong)viewport.Width,
298309
Height = (ulong)viewport.Height,
299310
}
300311
: null,
301-
DevicePixelRatio = viewport?.DeviceScaleFactor,
312+
DevicePixelRatio = viewport.DeviceScaleFactor,
302313
}).ConfigureAwait(false);
303314
Viewport = viewport;
304315
return;
@@ -398,7 +409,7 @@ public override async Task CloseAsync(PageCloseOptions options = null)
398409
}
399410
catch (Exception)
400411
{
401-
return;
412+
// Swallow.
402413
}
403414
}
404415

@@ -464,7 +475,13 @@ await BidiBrowser.Driver.Storage.DeleteCookiesAsync(new WebDriverBiDi.Storage.De
464475
public override Task<IJSHandle> QueryObjectsAsync(IJSHandle prototypeHandle) => throw new NotImplementedException();
465476

466477
/// <inheritdoc />
467-
public override Task SetRequestInterceptionAsync(bool value) => throw new NotImplementedException();
478+
public override async Task SetRequestInterceptionAsync(bool value)
479+
{
480+
_requestInterception = await ToggleInterceptionAsync(
481+
[InterceptPhase.BeforeRequestSent],
482+
_requestInterception,
483+
value).ConfigureAwait(false);
484+
}
468485

469486
/// <inheritdoc />
470487
public override async Task SetOfflineModeAsync(bool value)
@@ -675,6 +692,30 @@ await BidiMainFrame.Client.SendAsync(
675692
}).ConfigureAwait(false);
676693
}
677694

695+
private async Task<string> ToggleInterceptionAsync(
696+
InterceptPhase[] phases,
697+
string interception,
698+
bool expected)
699+
{
700+
if (expected && interception == null)
701+
{
702+
var options = new AddInterceptCommandParameters();
703+
foreach (var phase in phases)
704+
{
705+
options.Phases.Add(phase);
706+
}
707+
708+
return await BidiMainFrame.BrowsingContext.AddInterceptAsync(options).ConfigureAwait(false);
709+
}
710+
else if (!expected && interception != null)
711+
{
712+
await BidiMainFrame.BrowsingContext.UserContext.Browser.RemoveInterceptAsync(interception).ConfigureAwait(false);
713+
return null;
714+
}
715+
716+
return interception;
717+
}
718+
678719
private void Initialize()
679720
{
680721
BidiMainFrame.BrowsingContext.Closed += (_, _) =>
@@ -686,17 +727,15 @@ private void Initialize()
686727
// Track workers
687728
WorkerCreated += (_, e) =>
688729
{
689-
var worker = e.Worker as BidiWebWorker;
690-
if (worker != null)
730+
if (e.Worker is BidiWebWorker worker)
691731
{
692732
_workers[worker.RealmId] = worker;
693733
}
694734
};
695735

696736
WorkerDestroyed += (_, e) =>
697737
{
698-
var worker = e.Worker as BidiWebWorker;
699-
if (worker != null)
738+
if (e.Worker is BidiWebWorker worker)
700739
{
701740
_workers.TryRemove(worker.RealmId, out var _);
702741
}

lib/PuppeteerSharp/Bidi/Core/Browser.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ public async Task<UserContext> CreateUserContextAsync()
9999
return CreateUserContext(result.UserContextId);
100100
}
101101

102+
public async Task RemoveInterceptAsync(string intercept)
103+
{
104+
await Session.Driver.Network.RemoveInterceptAsync(new WebDriverBiDi.Network.RemoveInterceptCommandParameters(intercept)).ConfigureAwait(false);
105+
}
106+
102107
private void Dispose(string reason)
103108
{
104109
_reason = reason;

lib/PuppeteerSharp/Bidi/Core/BrowsingContext.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,14 @@ internal async Task TraverseHistoryAsync(int delta)
205205
internal async Task ReloadAsync()
206206
=> await Session.Driver.BrowsingContext.ReloadAsync(new ReloadCommandParameters(Id)).ConfigureAwait(false);
207207

208+
internal async Task<string> AddInterceptAsync(WebDriverBiDi.Network.AddInterceptCommandParameters options)
209+
{
210+
options.BrowsingContextIds ??= new List<string>();
211+
options.BrowsingContextIds.Add(Id);
212+
var result = await Session.Driver.Network.AddInterceptAsync(options).ConfigureAwait(false);
213+
return result.InterceptId;
214+
}
215+
208216
protected virtual void OnBrowsingContextCreated(BidiBrowsingContextEventArgs e) => BrowsingContextCreated?.Invoke(this, e);
209217

210218
private void Initialize()

0 commit comments

Comments
 (0)