Skip to content

Commit

Permalink
Bidi: Pass wait for navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok committed Dec 23, 2024
1 parent e76e23c commit 8f15fb1
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,6 @@
"FAIL"
]
},
{
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
"testIdPattern": "[navigation.spec] navigation Page.waitForNavigation*",
"platforms": [
"darwin",
"linux",
"win32"
],
"parameters": [
"webDriverBiDi"
],
"expectations": [
"FAIL"
]
},
{
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
"testIdPattern": "[navigation.spec] *should navigate to dataURL and fire dataURL requests*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,13 @@ public async Task ShouldWorkWhenSubframeIssuesWindowStop()
var frameAttachedTaskSource = new TaskCompletionSource<IFrame>();
Page.FrameAttached += (_, e) =>
{
frameAttachedTaskSource.SetResult(e.Frame);
};

var frame = await frameAttachedTaskSource.Task;
var frameNavigatedTaskSource = new TaskCompletionSource<bool>();
Page.FrameNavigated += (_, e) =>
{
if (e.Frame == frame)
if (e.Frame.ParentFrame != null)
{
frameNavigatedTaskSource.TrySetResult(true);
frameAttachedTaskSource.SetResult(e.Frame);
}
};
await frameNavigatedTaskSource.Task;

var frame = await frameAttachedTaskSource.Task;
await Task.WhenAll(
frame.EvaluateFunctionAsync("() => window.stop()"),
navigationTask
Expand Down
33 changes: 32 additions & 1 deletion lib/PuppeteerSharp/Bidi/BidiFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System.Linq;
using System.Threading.Tasks;
using PuppeteerSharp.Bidi.Core;
using PuppeteerSharp.Cdp.Messaging;
using PuppeteerSharp.Helpers;

namespace PuppeteerSharp.Bidi;
Expand All @@ -41,6 +42,7 @@ internal BidiFrame(BidiPage parentPage, BidiFrame parentFrame, BrowsingContext b
ParentPage = parentPage;
ParentFrame = parentFrame;
BrowsingContext = browsingContext;
Id = browsingContext.Id;
_realms = new Realms(
#pragma warning disable CA2000
BidiFrameRealm.From(browsingContext.DefaultRealm, this),
Expand Down Expand Up @@ -259,6 +261,11 @@ private void Initialize()
CreateFrameTarget(browsingContext);
}

BrowsingContext.BrowsingContextCreated += (sender, args) =>
{
CreateFrameTarget(args.BrowsingContext);
};

BrowsingContext.Closed += (sender, args) =>
{
foreach (var session in BidiCdpSession.Sessions)
Expand Down Expand Up @@ -288,11 +295,35 @@ private void Initialize()

_ = httpRequest.FinalizeInterceptionsAsync();
};

BrowsingContext.Navigation += (sender, args) =>
{
if (args.Navigation.FragmentReceived)
{
((Page)Page).OnFrameNavigated(new FrameNavigatedEventArgs(this, NavigationType.Navigation));
}

args.Navigation.Fragment += (o, eventArgs) =>
{
((Page)Page).OnFrameNavigated(new FrameNavigatedEventArgs(this, NavigationType.Navigation));
};
};

BrowsingContext.Load += (sender, args) =>
{
((Page)Page).OnLoad();
};

BrowsingContext.DomContentLoaded += (sender, args) =>
{
((Page)Page).OnDOMContentLoaded();
((Page)Page).OnFrameNavigated(new FrameNavigatedEventArgs(this, NavigationType.Navigation));
};
}

private void CreateFrameTarget(BrowsingContext browsingContext)
{
var frame = BidiFrame.From(null, this, browsingContext);
var frame = From(null, this, browsingContext);
_frames.TryAdd(browsingContext, frame);
((BidiPage)Page).OnFrameAttached(new FrameEventArgs(frame));

Expand Down
3 changes: 2 additions & 1 deletion lib/PuppeteerSharp/Bidi/BidiRealm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ internal override async Task<T> EvaluateExpressionAsync<T>(string script)
internal override async Task<T> EvaluateFunctionAsync<T>(string script, params object[] args)
=> DeserializeResult<T>((await EvaluateAsync(true, false, script, args).ConfigureAwait(false)).Result.Value);

internal override Task<JsonElement?> EvaluateFunctionAsync(string script, params object[] args) => throw new System.NotImplementedException();
internal override async Task<JsonElement?> EvaluateFunctionAsync(string script, params object[] args)
=> DeserializeResult<JsonElement?>((await EvaluateAsync(true, false, script, args).ConfigureAwait(false)).Result.Value);

protected virtual void Initialize()
{
Expand Down
28 changes: 14 additions & 14 deletions lib/PuppeteerSharp/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -905,30 +905,30 @@ internal void OnRequest(IRequest request)
}

/// <summary>
/// Dispose resources.
/// Raises the <see cref="DOMContentLoaded"/> event.
/// </summary>
/// <param name="disposing">Indicates whether disposal was initiated by <see cref="Dispose()"/> operation.</param>
protected virtual void Dispose(bool disposing)
{
Mouse.Dispose();
_ = DisposeAsync();
}
internal void OnDOMContentLoaded() => DOMContentLoaded?.Invoke(this, EventArgs.Empty);

/// <summary>
/// Raises the <see cref="FrameNavigated"/> event.
/// Raises the <see cref="Load"/> event.
/// </summary>
/// <param name="e">Event arguments.</param>
protected void OnFrameNavigated(FrameNavigatedEventArgs e) => FrameNavigated?.Invoke(this, e);
internal void OnLoad() => Load?.Invoke(this, EventArgs.Empty);

/// <summary>
/// Raises the <see cref="DOMContentLoaded"/> event.
/// Raises the <see cref="FrameNavigated"/> event.
/// </summary>
protected void OnDOMContentLoaded() => DOMContentLoaded?.Invoke(this, EventArgs.Empty);
/// <param name="e">Event arguments.</param>
internal void OnFrameNavigated(FrameNavigatedEventArgs e) => FrameNavigated?.Invoke(this, e);

/// <summary>
/// Raises the <see cref="Load"/> event.
/// Dispose resources.
/// </summary>
protected void OnLoad() => Load?.Invoke(this, EventArgs.Empty);
/// <param name="disposing">Indicates whether disposal was initiated by <see cref="Dispose()"/> operation.</param>
protected virtual void Dispose(bool disposing)
{
Mouse.Dispose();
_ = DisposeAsync();
}

/// <summary>
/// Raises the <see cref="WorkerDestroyed"/> event.
Expand Down

0 comments on commit 8f15fb1

Please sign in to comment.