Skip to content

Commit

Permalink
Bidi: Pass wait for navigation (#2848)
Browse files Browse the repository at this point in the history
* Bidi: Pass wait for navigation

* markdown uses net8

* some fixes in the context

* Ignore one test
  • Loading branch information
kblok authored Dec 26, 2024
1 parent e76e23c commit 7dca812
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/on-push-do-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.x
dotnet-version: 8.0.x
- name: Run MarkdownSnippets
run: |
dotnet tool install --global MarkdownSnippets.Tool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
},
{
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
"testIdPattern": "[navigation.spec] navigation Page.waitForNavigation*",
"testIdPattern": "[navigation.spec] navigation Page.waitForNavigation*should work with clicking on anchor links",
"platforms": [
"darwin",
"linux",
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
2 changes: 1 addition & 1 deletion lib/PuppeteerSharp/Bidi/Core/BrowsingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private void Initialize()
return;
}

var browsingContext = From(UserContext, this, args.UserContextId, args.Url, args.OriginalOpener);
var browsingContext = From(UserContext, this, args.BrowsingContextId, args.Url, args.OriginalOpener);

_children.TryAdd(args.UserContextId, browsingContext);

Expand Down
8 changes: 4 additions & 4 deletions lib/PuppeteerSharp/Bidi/Core/DedicatedWorkerRealm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ internal class DedicatedWorkerRealm : Realm
{
private readonly ConcurrentSet<IDedicatedWorkerOwnerRealm> _owners = [];

private DedicatedWorkerRealm(IDedicatedWorkerOwnerRealm owner, string id, string origin)
: base(id, origin)
private DedicatedWorkerRealm(BrowsingContext context, IDedicatedWorkerOwnerRealm owner, string id, string origin)
: base(context, id, origin)
{
_owners.Add(owner);
}

public override Session Session => _owners.FirstOrDefault()?.Session;

public static DedicatedWorkerRealm From(IDedicatedWorkerOwnerRealm owner, string id, string origin)
public static DedicatedWorkerRealm From(BrowsingContext context, IDedicatedWorkerOwnerRealm owner, string id, string origin)
{
var realm = new DedicatedWorkerRealm(owner, id, origin);
var realm = new DedicatedWorkerRealm(context, owner, id, origin);
realm.Initialize();
return realm;
}
Expand Down
7 changes: 5 additions & 2 deletions lib/PuppeteerSharp/Bidi/Core/Realm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

namespace PuppeteerSharp.Bidi.Core;

internal abstract class Realm(string id, string origin) : IDisposable
internal abstract class Realm(BrowsingContext context, string id, string origin) : IDisposable
{
private string _reason;

Expand All @@ -40,10 +40,13 @@ internal abstract class Realm(string id, string origin) : IDisposable

public bool Disposed { get; private set; }

public WebDriverBiDi.Script.Target Target => new RealmTarget(Id);
public virtual ContextTarget Target => new ContextTarget(Id);

public abstract Session Session { get; }

// Just for testing purposes
internal BrowsingContext Context => context;

public void Dispose(string reason)
{
_reason = reason;
Expand Down
16 changes: 9 additions & 7 deletions lib/PuppeteerSharp/Bidi/WindowRealm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,22 @@

using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using PuppeteerSharp.Bidi.Core;
using PuppeteerSharp.Helpers;
using WebDriverBiDi.Script;

namespace PuppeteerSharp.Bidi;

internal class WindowRealm(BrowsingContext browsingContext, string sandbox = null) : Core.Realm(string.Empty, string.Empty), IDedicatedWorkerOwnerRealm
internal class WindowRealm(BrowsingContext browsingContext, string sandbox = null) : Core.Realm(browsingContext, string.Empty, string.Empty), IDedicatedWorkerOwnerRealm
{
private readonly string _sandbox = sandbox;
private readonly ConcurrentDictionary<string, DedicatedWorkerRealm> _workers = [];

public event EventHandler<WorkerRealmEventArgs> Worker;

public override Session Session => browsingContext.UserContext.Browser.Session;
public override Session Session => Context.UserContext.Browser.Session;

public override ContextTarget Target => new(Context.Id); // TODO: Add sandbox

public string ExecutionContextId { get; set; }

Expand All @@ -48,7 +50,7 @@ public static WindowRealm From(BrowsingContext context, string sandbox = null)

public override void Dispose()
{
browsingContext.Dispose();
Context.Dispose();
Session.Dispose();

foreach (var worker in _workers.Values)
Expand All @@ -61,7 +63,7 @@ public override void Dispose()

private void Initialize()
{
browsingContext.Closed += (sender, args) => Dispose(args.Reason);
Context.Closed += (sender, args) => Dispose(args.Reason);
Session.Driver.Script.OnRealmCreated.AddObserver(OnWindowRealmCreated);
Session.Driver.Script.OnRealmCreated.AddObserver(OnDedicatedRealmCreated);
}
Expand All @@ -80,7 +82,7 @@ private void OnWindowRealmCreated(RealmCreatedEventArgs args)
return;
}

var realm = DedicatedWorkerRealm.From(this, dedicatedWorkerInfo.RealmId, dedicatedWorkerInfo.Origin);
var realm = DedicatedWorkerRealm.From(Context, this, dedicatedWorkerInfo.RealmId, dedicatedWorkerInfo.Origin);
_workers.TryAdd(realm.Id, realm);
realm.Destroyed += (sender, args) => _workers.TryRemove(realm.Id, out _);
OnWorker(realm);
Expand All @@ -91,7 +93,7 @@ private void OnWindowRealmCreated(RealmCreatedEventArgs args)
private void OnDedicatedRealmCreated(RealmCreatedEventArgs args)
{
if (args.Type != RealmType.Window ||
args.As<WindowRealmInfo>().BrowsingContext != browsingContext.Id ||
args.As<WindowRealmInfo>().BrowsingContext != Context.Id ||
args.As<WindowRealmInfo>().Sandbox != _sandbox)
{
return;
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 7dca812

Please sign in to comment.