Skip to content

Unlock prerender #2928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: v21
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1762,21 +1762,6 @@
"FAIL"
]
},
{
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
"testIdPattern": "[DeviceRequestPrompt.test.ts] *",
"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": "[launcher.spec] PuppeteerSharp *",
Expand Down Expand Up @@ -1821,20 +1806,5 @@
"expectations": [
"FAIL"
]
},
{
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
"testIdPattern": "[prerender.spec] *",
"platforms": [
"darwin",
"linux",
"win32"
],
"parameters": [
"webDriverBiDi"
],
"expectations": [
"FAIL"
]
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -1568,6 +1568,7 @@
"expectations": ["SKIP"],
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
},
{
"testIdPattern": "[extensions.spec] extensions can evaluate in the service worker",
"platforms": ["win32"],
"parameters": ["cdp", "chrome"],
Expand Down Expand Up @@ -3467,13 +3468,22 @@
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
},
{

"testIdPattern": "[extensions.spec] extensions service_worker target type should be available",
"platforms": ["darwin", "linux", "win32"],
"parameters": ["cdp", "chrome", "chrome-headless-shell"],
"expectations": ["SKIP"],
"platforms": [
"darwin",
"linux",
"win32"
],
"parameters": [
"cdp",
"chrome",
"chrome-headless-shell"
],
"expectations": [
"SKIP"
],
"comment": "TODO: add a comment explaining why this expectation is required (include links to issues)"
],
},
{
"testIdPattern": "[mouse.spec] Mouse should select the text with mouse",
"platforms": ["win32"],
Expand Down
2 changes: 1 addition & 1 deletion lib/PuppeteerSharp.Tests/NavigationTests/PageGotoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ await Task.WhenAll(
Server.WaitForRequest("/digits/1.png", r => referer2 = r.Headers["Referer"]),
Page.GoToAsync(TestConstants.ServerUrl + "/grid.html", new NavigationOptions
{
ReferrerPolicy = "no-referer"
ReferrerPolicy = "no-referrer"
})
);

Expand Down
1 change: 0 additions & 1 deletion lib/PuppeteerSharp.Tests/PrerenderTests/PrerenderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public async Task CanNavigateToAPrerenderedPageViaPuppeteer()
var button = await Page.WaitForSelectorAsync("button");
await button.ClickAsync();


await Page.GoToAsync(TestConstants.ServerUrl + "/prerender/target.html");
Assert.That(await Page.EvaluateExpressionAsync<string>("document.body.innerText"), Is.EqualTo("target"));
}
Expand Down
7 changes: 3 additions & 4 deletions lib/PuppeteerSharp/Bidi/BidiElementHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
// * SOFTWARE.

using System.Threading.Tasks;
using PuppeteerSharp.Cdp.Messaging;
using PuppeteerSharp.QueryHandlers;
using WebDriverBiDi.Script;

Expand All @@ -40,15 +39,15 @@ internal class BidiElementHandle(RemoteValue value, BidiRealm realm) : ElementHa

internal override CustomQuerySelectorRegistry CustomQuerySelectorRegistry { get; } = new();

protected override Page Page { get; }
internal BidiFrame BidiFrame => realm.Environment as BidiFrame;

protected override Page Page => BidiFrame.BidiPage;

public static IJSHandle From(RemoteValue value, BidiRealm realm)
{
return new BidiElementHandle(value, realm);
}

public override ValueTask DisposeAsync() => throw new System.NotImplementedException();

public override Task UploadFileAsync(bool resolveFilePaths, params string[] filePaths) => throw new System.NotImplementedException();

public override Task<IFrame> ContentFrameAsync() => throw new System.NotImplementedException();
Expand Down
37 changes: 27 additions & 10 deletions lib/PuppeteerSharp/Bidi/BidiFrameRealm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,22 @@
// * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// * SOFTWARE.

using System;
using System.Threading.Tasks;
using PuppeteerSharp.Helpers;
using PuppeteerSharp.QueryHandlers;

namespace PuppeteerSharp.Bidi;

internal class BidiFrameRealm(WindowRealm realm, BidiFrame frame) : BidiRealm(realm, frame.TimeoutSettings)
{
private readonly WindowRealm _realm = realm;
private bool _bindingsInstalled;
private readonly TaskQueue _puppeteerUtilQueue = new();
private IJSHandle _puppeteerUtil;

internal override IEnvironment Environment => frame;

internal BidiFrame Frame => frame;

public static BidiFrameRealm From(WindowRealm realm, BidiFrame frame)
{
Expand All @@ -38,17 +46,26 @@ public static BidiFrameRealm From(WindowRealm realm, BidiFrame frame)

public override async Task<IJSHandle> GetPuppeteerUtilAsync()
{
var installTcs = new TaskCompletionSource<bool>();
var scriptInjector = frame.BrowsingContext.Session.ScriptInjector;

if (!_bindingsInstalled)
await _puppeteerUtilQueue.Enqueue(async () =>
{
// TODO: Implement
installTcs.TrySetResult(true);
_bindingsInstalled = true;
}
if (_puppeteerUtil == null)
{
await scriptInjector.InjectAsync(
async (script) =>
{
if (_puppeteerUtil != null)
{
await _puppeteerUtil.DisposeAsync().ConfigureAwait(false);
}

await installTcs.Task.ConfigureAwait(false);
return await base.GetPuppeteerUtilAsync().ConfigureAwait(false);
_puppeteerUtil = await EvaluateExpressionHandleAsync(script).ConfigureAwait(false);
},
_puppeteerUtil == null).ConfigureAwait(false);
}
}).ConfigureAwait(false);
return _puppeteerUtil;
}

protected override void Initialize()
Expand All @@ -58,7 +75,7 @@ protected override void Initialize()
_realm.Updated += (_, __) =>
{
(Environment as Frame)?.ClearDocumentHandle();
_bindingsInstalled = false;
_puppeteerUtil = null;
};
}
}
2 changes: 1 addition & 1 deletion lib/PuppeteerSharp/Bidi/BidiHttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private BidiHttpRequest(Request request, BidiFrame frame, BidiHttpRequest redire
{
_request = request;
Frame = frame;
RedirectChainList = new List<IRequest>();
RedirectChainList = [];
Requests.AddItem(request, this);
}

Expand Down
15 changes: 14 additions & 1 deletion lib/PuppeteerSharp/Bidi/BidiJSHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace PuppeteerSharp.Bidi;

internal class BidiJSHandle(RemoteValue value, BidiRealm realm) : JSHandle
{
private bool _disposed;

public RemoteValue RemoteValue { get; } = value;

public bool IsPrimitiveValue
Expand All @@ -42,6 +44,8 @@ public bool IsPrimitiveValue
}
}

internal string Id => RemoteValue.Handle;

internal override Realm Realm { get; } = realm;

public static BidiJSHandle From(RemoteValue value, BidiRealm realm)
Expand All @@ -51,7 +55,16 @@ public static BidiJSHandle From(RemoteValue value, BidiRealm realm)

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

public override ValueTask DisposeAsync() => throw new System.NotImplementedException();
public override async ValueTask DisposeAsync()
{
if (_disposed)
{
return;
}

_disposed = true;
await realm.DestroyHandlesAsync(this).ConfigureAwait(false);
}

/// <inheritdoc/>
public override string ToString()
Expand Down
106 changes: 106 additions & 0 deletions lib/PuppeteerSharp/Bidi/BidiMouse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// * MIT License
// *
// * Copyright (c) Darío Kondratiuk
// *
// * Permission is hereby granted, free of charge, to any person obtaining a copy
// * of this software and associated documentation files (the "Software"), to deal
// * in the Software without restriction, including without limitation the rights
// * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// * copies of the Software, and to permit persons to whom the Software is
// * furnished to do so, subject to the following conditions:
// *
// * The above copyright notice and this permission notice shall be included in all
// * copies or substantial portions of the Software.
// *
// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// * SOFTWARE.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using PuppeteerSharp.Input;
using WebDriverBiDi.Input;

namespace PuppeteerSharp.Bidi;

internal class BidiMouse(BidiPage page) : Mouse
{
public override Task DropAsync(decimal x, decimal y, DragData data) => throw new NotImplementedException();

public override Task DragAndDropAsync(decimal startX, decimal startY, decimal endX, decimal endY, int delay = 0) => throw new NotImplementedException();

public override Task ResetAsync() => throw new NotImplementedException();

public override Task MoveAsync(decimal x, decimal y, MoveOptions options = null) => throw new NotImplementedException();

public override Task UpAsync(ClickOptions options = null) => throw new NotImplementedException();

public override Task WheelAsync(decimal deltaX, decimal deltaY) => throw new NotImplementedException();

public override Task<DragData> DragAsync(decimal startX, decimal startY, decimal endX, decimal endY) => throw new NotImplementedException();

public override Task DragEnterAsync(decimal x, decimal y, DragData data) => throw new NotImplementedException();

public override Task DragOverAsync(decimal x, decimal y, DragData data) => throw new NotImplementedException();

public override Task DownAsync(ClickOptions options = null) => throw new NotImplementedException();

public override async Task ClickAsync(decimal x, decimal y, ClickOptions options = null)
{
var actions = new List<IPointerSourceAction>
{
new PointerMoveAction()
{
X = (long)Math.Round(x),
Y = (long)Math.Round(y),
},
};

var pointerDownAction = new PointerDownAction(GetBidiButton(options?.Button ?? MouseButton.Left));
var pointerUpAction = new PointerUpAction(GetBidiButton(options?.Button ?? MouseButton.Left));

for (var i = 1; i < (options?.Count ?? 1); ++i)
{
actions.Add(pointerDownAction);
actions.Add(pointerUpAction);
}

actions.Add(pointerDownAction);

if (options?.Delay is > 0)
{
actions.Add(new PauseAction()
{
Duration = TimeSpan.FromMilliseconds(options.Delay),
});
}

actions.Add(pointerUpAction);

var finalSource = new PointerSourceActions();
finalSource.Actions.AddRange(actions);

await page.BidiMainFrame.BrowsingContext.PerformActionsAsync([finalSource]).ConfigureAwait(false);
}

protected override void Dispose(bool disposing) => throw new NotImplementedException();

private long GetBidiButton(MouseButton optionsButton)
{
return optionsButton switch
{
MouseButton.Left => 0,
MouseButton.Middle => 1,
MouseButton.Right => 2,
MouseButton.Back => 3,
MouseButton.Forward => 4,
_ => throw new ArgumentOutOfRangeException(nameof(optionsButton), $"Unsupported mouse button: {optionsButton}"),
};
}
}

1 change: 1 addition & 0 deletions lib/PuppeteerSharp/Bidi/BidiPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ internal BidiPage(BidiBrowserContext browserContext, BrowsingContext browsingCon
Browser = browserContext.Browser;
BidiMainFrame = BidiFrame.From(this, null, browsingContext);
_cdpEmulationManager = new CdpEmulationManager(BidiMainFrame.Client);
Mouse = new BidiMouse(this);
}

/// <inheritdoc />
Expand Down
Loading
Loading