Skip to content
Merged
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 @@ -1747,21 +1747,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 @@ -1806,20 +1791,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 @@ -3468,11 +3468,20 @@
"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)"
},
{
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
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
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
52 changes: 52 additions & 0 deletions lib/PuppeteerSharp/Bidi/BidiRealm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,16 @@ private T DeserializeResult<T>(object result)
return (T)(object)Convert.ToBoolean(result, CultureInfo.InvariantCulture);
}

if (typeof(T) == typeof(decimal))
{
return (T)(object)Convert.ToDecimal(result, CultureInfo.InvariantCulture);
}

if (result is RemoteValueDictionary remoteValueDictionary)
{
return DeserializeRemoteValueDictionary<T>(remoteValueDictionary);
}

if (typeof(T).IsArray)
{
// Get the element type of the array
Expand Down Expand Up @@ -282,6 +292,45 @@ private T DeserializeResult<T>(object result)
return (T)result;
}

private T DeserializeRemoteValueDictionary<T>(RemoteValueDictionary remoteValueDictionary)
{
// Create an instance of T
var instance = Activator.CreateInstance<T>();
var type = typeof(T);

// Iterate through the dictionary and populate properties
foreach (var entry in remoteValueDictionary)
{
var propertyName = entry.Key?.ToString();
if (string.IsNullOrEmpty(propertyName))
{
continue;
}

var remoteValue = entry.Value;

// Find the property on the type (case-insensitive)
var property = type.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);

if (property != null && property.CanWrite)
{
// Get the value from RemoteValue
var value = remoteValue.Value;

// Recursively deserialize the value to the property type
var deserializedValue = typeof(BidiRealm)
.GetMethod(nameof(DeserializeResult), BindingFlags.Instance | BindingFlags.NonPublic)
?.MakeGenericMethod(property.PropertyType)
.Invoke(this, [value]);

// Set the property value
property.SetValue(instance, deserializedValue);
}
}

return instance;
}

private async Task<ArgumentValue> FormatArgumentAsync(object arg)
{
if (arg is TaskCompletionSource<object> tcs)
Expand All @@ -307,6 +356,9 @@ private async Task<ArgumentValue> FormatArgumentAsync(object arg)
case int integer when integer == -0:
return LocalValue.NegativeZero;

case decimal decimalValue:
return LocalValue.Number(decimalValue);

case double d:
if (double.IsPositiveInfinity(d))
{
Expand Down
8 changes: 8 additions & 0 deletions lib/PuppeteerSharp/Bidi/Core/BrowsingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using WebDriverBiDi.BrowsingContext;
using WebDriverBiDi.Input;

namespace PuppeteerSharp.Bidi.Core;

Expand Down Expand Up @@ -162,6 +163,13 @@ internal async Task SetViewportAsync(SetViewportOptions options = null)
await Session.Driver.BrowsingContext.SetViewportAsync(parameters).ConfigureAwait(false);
}

internal async Task PerformActionsAsync(SourceActions[] actions)
{
var param = new PerformActionsCommandParameters(Id);
param.Actions.AddRange(actions);
await Session.Driver.Input.PerformActionsAsync(param).ConfigureAwait(false);
}

internal WindowRealm CreateWindowRealm(string sandbox = null)
{
var realm = WindowRealm.From(this, sandbox);
Expand Down
1 change: 1 addition & 0 deletions lib/PuppeteerSharp/Bidi/Core/Realm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// * SOFTWARE.

using System;
using System.Linq;
using System.Threading.Tasks;
using WebDriverBiDi.Script;

Expand Down
31 changes: 16 additions & 15 deletions lib/PuppeteerSharp/ElementHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal ElementHandle(JSHandle handle)
/// <summary>
/// Base handle.
/// </summary>
protected JSHandle Handle { get; }
internal JSHandle Handle { get; }

/// <summary>
/// Element's page.
Expand Down Expand Up @@ -451,8 +451,9 @@ public Task<bool> IsIntersectingViewportAsync(decimal threshold)
=> BindIsolatedHandleAsync<bool, ElementHandle>(async handle =>
{
await handle.AssertConnectedElementAsync().ConfigureAwait(false);
var svgHandle = await AsSVGElementHandleAsync(this).ConfigureAwait(false);
var target = svgHandle == null ? handle : await svgHandle.GetOwnerSVGElementAsync().ConfigureAwait(false);
var svgHandle = await AsSVGElementHandleAsync(handle).ConfigureAwait(false);
var ownerSVGElement = svgHandle == null ? null : await svgHandle.GetOwnerSVGElementAsync().ConfigureAwait(false);
var target = ownerSVGElement ?? handle;

return await target.Realm.EvaluateFunctionAsync<bool>(
@"async (element, threshold) => {
Expand Down Expand Up @@ -662,18 +663,6 @@ public override Task<Dictionary<string, IJSHandle>> GetPropertiesAsync()
public override Task<T> JsonValueAsync<T>()
=> BindIsolatedHandleAsync<T, ElementHandle>(element => element.Handle.JsonValueAsync<T>());

/// <inheritdoc/>
public virtual Task ScrollIntoViewAsync()
=> BindIsolatedHandleAsync<JsonElement?, ElementHandle>(handle
=> handle.EvaluateFunctionAsync(
@"element => {
element.scrollIntoView({
block: 'center',
inline: 'center',
behavior: 'instant',
});
}"));

/// <inheritdoc />
public override async ValueTask DisposeAsync()
{
Expand All @@ -688,6 +677,18 @@ public override async ValueTask DisposeAsync()
GC.SuppressFinalize(this);
}

/// <inheritdoc/>
public virtual Task ScrollIntoViewAsync()
=> BindIsolatedHandleAsync<JsonElement?, ElementHandle>(handle
=> handle.EvaluateFunctionAsync(
@"element => {
element.scrollIntoView({
block: 'center',
inline: 'center',
behavior: 'instant',
});
}"));

/// <summary>
/// Checks whether the element is still connected to the browser.
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions lib/PuppeteerSharp/Helpers/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.Json;
using PuppeteerSharp.Helpers.Json;

namespace PuppeteerSharp.Helpers
{
Expand Down
Loading