Skip to content

Commit 3b32805

Browse files
committed
some progress
1 parent 456f872 commit 3b32805

20 files changed

Lines changed: 225 additions & 90 deletions

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -157,21 +157,6 @@
157157
"FAIL"
158158
]
159159
},
160-
{
161-
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
162-
"testIdPattern": "[navigation.spec] navigation Frame.goto*",
163-
"platforms": [
164-
"darwin",
165-
"linux",
166-
"win32"
167-
],
168-
"parameters": [
169-
"webDriverBiDi"
170-
],
171-
"expectations": [
172-
"FAIL"
173-
]
174-
},
175160
{
176161
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
177162
"testIdPattern": "[navigation.spec] *should wait for network idle to succeed navigation*",

lib/PuppeteerSharp.Tests/NavigationTests/FrameGoToTests.cs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,22 @@ namespace PuppeteerSharp.Tests.NavigationTests
99
{
1010
public class FrameGoToTests : PuppeteerPageBaseTest
1111
{
12-
public FrameGoToTests() : base()
13-
{
14-
}
15-
1612
[Test, Retry(2), PuppeteerTest("navigation.spec", "navigation Frame.goto", "should navigate subframes")]
1713
public async Task ShouldNavigateSubFrames()
1814
{
1915
await Page.GoToAsync(TestConstants.ServerUrl + "/frames/one-frame.html");
20-
Assert.That(Page.Frames.Where(f => f.Url.Contains("/frames/one-frame.html")), Has.Exactly(1).Items);
21-
Assert.That(Page.Frames.Where(f => f.Url.Contains("/frames/frame.html")), Has.Exactly(1).Items);
16+
Assert.Multiple(() =>
17+
{
18+
Assert.That(Page.Frames.Where(f => f.Url.Contains("/frames/one-frame.html")), Has.Exactly(1).Items);
19+
Assert.That(Page.Frames.Where(f => f.Url.Contains("/frames/frame.html")), Has.Exactly(1).Items);
20+
});
2221
var childFrame = Page.FirstChildFrame();
2322
var response = await childFrame.GoToAsync(TestConstants.EmptyPage);
24-
Assert.That(response.Status, Is.EqualTo(HttpStatusCode.OK));
25-
Assert.That(childFrame, Is.SameAs(response.Frame));
23+
Assert.Multiple(() =>
24+
{
25+
Assert.That(response.Status, Is.EqualTo(HttpStatusCode.OK));
26+
Assert.That(childFrame, Is.SameAs(response.Frame));
27+
});
2628
}
2729

2830
[Test, Retry(2), PuppeteerTest("navigation.spec", "navigation Frame.goto", "should reject when frame detaches")]
@@ -53,9 +55,9 @@ public async Task ShouldReturnMatchingResponses()
5355
// Attach three frames.
5456
var matchingData = new MatchingResponseData[]
5557
{
56-
new MatchingResponseData{ FrameTask = FrameUtils.AttachFrameAsync(Page, "frame1", TestConstants.EmptyPage)},
57-
new MatchingResponseData{ FrameTask = FrameUtils.AttachFrameAsync(Page, "frame2", TestConstants.EmptyPage)},
58-
new MatchingResponseData{ FrameTask = FrameUtils.AttachFrameAsync(Page, "frame3", TestConstants.EmptyPage)}
58+
new() { FrameTask = FrameUtils.AttachFrameAsync(Page, "frame1", TestConstants.EmptyPage)},
59+
new() { FrameTask = FrameUtils.AttachFrameAsync(Page, "frame2", TestConstants.EmptyPage)},
60+
new() { FrameTask = FrameUtils.AttachFrameAsync(Page, "frame3", TestConstants.EmptyPage)}
5961
};
6062

6163
await Task.WhenAll(matchingData.Select(m => m.FrameTask));
@@ -80,20 +82,23 @@ public async Task ShouldReturnMatchingResponses()
8082
await waitRequestTask;
8183
}
8284
// Respond from server out-of-order.
83-
var serverResponseTexts = new string[] { "AAA", "BBB", "CCC" };
85+
var serverResponseTexts = new[] { "AAA", "BBB", "CCC" };
8486
for (var i = 0; i < 3; ++i)
8587
{
8688
matchingData[i].ServerResponseTcs.TrySetResult(serverResponseTexts[i]);
8789
var response = await matchingData[i].NavigationTask;
88-
Assert.That(response.Frame, Is.SameAs(matchingData[i].FrameTask.Result));
89-
Assert.That(await response.TextAsync(), Is.EqualTo(serverResponseTexts[i]));
90+
await Assert.MultipleAsync(async () =>
91+
{
92+
Assert.That(response.Frame, Is.SameAs(matchingData[i].FrameTask.Result));
93+
Assert.That(await response.TextAsync(), Is.EqualTo(serverResponseTexts[i]));
94+
});
9095
}
9196
}
9297

9398
private sealed class MatchingResponseData
9499
{
95-
public Task<IFrame> FrameTask { get; internal set; }
96-
public TaskCompletionSource<string> ServerResponseTcs { get; internal set; } = new TaskCompletionSource<string>();
100+
public Task<IFrame> FrameTask { get; internal init; }
101+
public TaskCompletionSource<string> ServerResponseTcs { get; } = new();
97102
public Task<IResponse> NavigationTask { get; internal set; }
98103
}
99104
}

lib/PuppeteerSharp.Tests/PuppeteerSharp.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</PackageReference>
1919
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
2020
<PackageReference Include="System.Net.Http" Version="4.3.4" />
21-
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.5" />
21+
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.7" />
2222
<PackageReference Include="Microsoft.AspNetCore.Connections.Abstractions" Version="2.2.0" />
2323
</ItemGroup>
2424
<Import Project="../Common/CommonProps.props" />

lib/PuppeteerSharp/Bidi/BidiBrowserContext.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using System.Collections.Generic;
2525
using System.Linq;
2626
using System.Threading.Tasks;
27+
using Microsoft.Extensions.Logging;
2728
using PuppeteerSharp.Bidi.Core;
2829
using PuppeteerSharp.Helpers;
2930

@@ -39,9 +40,12 @@ private BidiBrowserContext(BidiBrowser browser, UserContext userContext, BidiBro
3940
{
4041
UserContext = userContext;
4142
Browser = browser;
43+
LoggerFactory = browser.LoggerFactory;
4244
DefaultViewport = options.DefaultViewport;
4345
}
4446

47+
internal ILoggerFactory LoggerFactory { get; }
48+
4549
internal ViewPortOptions DefaultViewport { get; set; }
4650

4751
internal TaskQueue ScreenshotTaskQueue => Browser.ScreenshotTaskQueue;

lib/PuppeteerSharp/Bidi/BidiElementHandle.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ public static IJSHandle From(RemoteValue value, BidiRealm realm)
4747
return new BidiElementHandle(value, realm);
4848
}
4949

50-
public override ValueTask DisposeAsync() => throw new System.NotImplementedException();
51-
5250
public override Task UploadFileAsync(bool resolveFilePaths, params string[] filePaths) => throw new System.NotImplementedException();
5351

5452
public override Task<IFrame> ContentFrameAsync() => throw new System.NotImplementedException();

lib/PuppeteerSharp/Bidi/BidiFrame.cs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
using System.Collections.Generic;
2626
using System.Linq;
2727
using System.Threading.Tasks;
28+
using Microsoft.Extensions.Logging;
2829
using PuppeteerSharp.Bidi.Core;
2930
using PuppeteerSharp.Cdp.Messaging;
3031
using PuppeteerSharp.Helpers;
@@ -37,9 +38,12 @@ public class BidiFrame : Frame
3738
private readonly ConcurrentDictionary<BrowsingContext, BidiFrame> _frames = new();
3839
private readonly Realms _realms;
3940

40-
internal BidiFrame(BidiPage parentPage, BidiFrame parentFrame, BrowsingContext browsingContext)
41+
private readonly ILoggerFactory _loggerFactory;
42+
43+
internal BidiFrame(BidiPage parentPage, BidiFrame parentFrame, BrowsingContext browsingContext, ILoggerFactory loggerFactory)
4144
{
42-
Client = new BidiCdpSession(this, parentPage.BidiBrowser.LoggerFactory);
45+
Client = new BidiCdpSession(this, loggerFactory);
46+
_loggerFactory = loggerFactory;
4347
ParentPage = parentPage;
4448
ParentFrame = parentFrame;
4549
BrowsingContext = browsingContext;
@@ -52,7 +56,18 @@ internal BidiFrame(BidiPage parentPage, BidiFrame parentFrame, BrowsingContext b
5256
}
5357

5458
/// <inheritdoc />
55-
public override IReadOnlyCollection<IFrame> ChildFrames { get; }
59+
public override IReadOnlyCollection<IFrame> ChildFrames
60+
{
61+
get
62+
{
63+
return BrowsingContext.Children.Select(child =>
64+
{
65+
_frames.TryGetValue(child, out var frame);
66+
67+
return frame;
68+
}).Where(frame => frame != null).ToList();
69+
}
70+
}
5671

5772
/// <inheritdoc/>
5873
public override string Url => BrowsingContext.Url;
@@ -186,16 +201,16 @@ async Task<Navigation> WaitForEventNavigationAsync()
186201
return waitForResponseTask.Result;
187202
}
188203

189-
internal static BidiFrame From(BidiPage parentPage, BidiFrame parentFrame, BrowsingContext browsingContext)
204+
/// <inheritdoc />
205+
protected override DeviceRequestPromptManager GetDeviceRequestPromptManager() => throw new System.NotImplementedException();
206+
207+
internal static BidiFrame From(BidiPage parentPage, BidiFrame parentFrame, BrowsingContext browsingContext, ILoggerFactory loggerFactory)
190208
{
191-
parentFrame = new BidiFrame(parentPage, parentFrame, browsingContext);
209+
parentFrame = new BidiFrame(parentPage, parentFrame, browsingContext, loggerFactory);
192210
parentFrame.Initialize();
193211
return parentFrame;
194212
}
195213

196-
/// <inheritdoc />
197-
protected internal override DeviceRequestPromptManager GetDeviceRequestPromptManager() => throw new System.NotImplementedException();
198-
199214
private PuppeteerException RewriteNavigationError(Exception ex, string url, int timeoutSettingsNavigationTimeout)
200215
{
201216
return ex is TimeoutException
@@ -259,12 +274,12 @@ private void Initialize()
259274
{
260275
foreach (var browsingContext in BrowsingContext.Children)
261276
{
262-
CreateFrameTarget(browsingContext);
277+
CreateFrameTarget(browsingContext, _loggerFactory);
263278
}
264279

265280
BrowsingContext.BrowsingContextCreated += (sender, args) =>
266281
{
267-
CreateFrameTarget(args.BrowsingContext);
282+
CreateFrameTarget(args.BrowsingContext, _loggerFactory);
268283
};
269284

270285
BrowsingContext.Closed += (sender, args) =>
@@ -322,9 +337,9 @@ private void Initialize()
322337
};
323338
}
324339

325-
private void CreateFrameTarget(BrowsingContext browsingContext)
340+
private void CreateFrameTarget(BrowsingContext browsingContext, ILoggerFactory loggerFactory)
326341
{
327-
var frame = From(null, this, browsingContext);
342+
var frame = From(null, this, browsingContext, loggerFactory);
328343
_frames.TryAdd(browsingContext, frame);
329344
((BidiPage)Page).OnFrameAttached(new FrameEventArgs(frame));
330345

lib/PuppeteerSharp/Bidi/BidiFrameRealm.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ internal class BidiFrameRealm(WindowRealm realm, BidiFrame frame) : BidiRealm(re
2929
private readonly WindowRealm _realm = realm;
3030
private bool _bindingsInstalled;
3131

32+
internal override IEnvironment Environment => frame;
33+
3234
public static BidiFrameRealm From(WindowRealm realm, BidiFrame frame)
3335
{
3436
var frameRealm = new BidiFrameRealm(realm, frame);
@@ -51,6 +53,8 @@ public override async Task<IJSHandle> GetPuppeteerUtilAsync()
5153
return await base.GetPuppeteerUtilAsync().ConfigureAwait(false);
5254
}
5355

56+
internal override Task<IElementHandle> AdoptBackendNodeAsync(object backendNodeId) => throw new System.NotImplementedException();
57+
5458
protected override void Initialize()
5559
{
5660
base.Initialize();

lib/PuppeteerSharp/Bidi/BidiHttpResponse.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ private BidiHttpResponse(WebDriverBiDi.Network.ResponseData data, BidiHttpReques
3535
{
3636
_data = data;
3737
_request = request;
38+
Request = request;
3839
Status = (HttpStatusCode)data.Status;
3940
Url = data.Url;
4041
}

lib/PuppeteerSharp/Bidi/BidiJSHandle.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,25 @@ public bool IsPrimitiveValue
4444

4545
internal override Realm Realm { get; } = realm;
4646

47+
internal override string Id => RemoteValue.Handle;
48+
4749
public static BidiJSHandle From(RemoteValue value, BidiRealm realm)
4850
{
4951
return new BidiJSHandle(value, realm);
5052
}
5153

5254
public override Task<T> JsonValueAsync<T>() => throw new System.NotImplementedException();
5355

54-
public override ValueTask DisposeAsync() => throw new System.NotImplementedException();
56+
public override async ValueTask DisposeAsync()
57+
{
58+
if (Disposed)
59+
{
60+
return;
61+
}
62+
63+
Disposed = true;
64+
await realm.DestroyHandlesAsync(this).ConfigureAwait(false);
65+
}
5566

5667
/// <inheritdoc/>
5768
public override string ToString()

lib/PuppeteerSharp/Bidi/BidiPage.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ internal BidiPage(BidiBrowserContext browserContext, BrowsingContext browsingCon
4444
{
4545
BrowserContext = browserContext;
4646
Browser = browserContext.Browser;
47-
BidiMainFrame = BidiFrame.From(this, null, browsingContext);
47+
BidiMainFrame = BidiFrame.From(this, null, browsingContext, browserContext.LoggerFactory);
4848
_cdpEmulationManager = new CdpEmulationManager(BidiMainFrame.Client);
4949
}
5050

@@ -58,7 +58,15 @@ internal BidiPage(BidiBrowserContext browserContext, BrowsingContext browsingCon
5858
public override Target Target { get; }
5959

6060
/// <inheritdoc />
61-
public override IFrame[] Frames { get; }
61+
public override IFrame[] Frames
62+
{
63+
get
64+
{
65+
var frames = BidiMainFrame.ChildFrames.ToList();
66+
frames.Insert(0, BidiMainFrame);
67+
return [.. frames];
68+
}
69+
}
6270

6371
/// <inheritdoc />
6472
public override WebWorker[] Workers { get; }
@@ -206,7 +214,7 @@ await BidiMainFrame.BrowsingContext.SetViewportAsync(
206214
}
207215

208216
/// <inheritdoc />
209-
public override async Task SetCookieAsync(params CookieParam[] cookies)
217+
public async override Task SetCookieAsync(params CookieParam[] cookies)
210218
{
211219
if (cookies == null)
212220
{
@@ -251,6 +259,17 @@ public override async Task SetCookieAsync(params CookieParam[] cookies)
251259

252260
var bidiCookie = new PartialCookie(cookie.Name, BytesValue.FromString(cookie.Value), domain);
253261

262+
if (cookie.PartitionKey != null)
263+
{
264+
await BrowserContext.Us.setCookie(
265+
bidiCookie,
266+
cookie.partitionKey,
267+
);
268+
}
269+
else
270+
{
271+
await this.#frame.browsingContext.setCookie(bidiCookie);
272+
}
254273
}
255274
}
256275

0 commit comments

Comments
 (0)