Skip to content

Commit

Permalink
First bidi function evaluation implementation (#2831)
Browse files Browse the repository at this point in the history
* Some progress

* First passing tests

* MVP

* undo temp code

* Ignore more tests

* ignore another test
  • Loading branch information
kblok authored Dec 3, 2024
1 parent 0516b73 commit f07f043
Show file tree
Hide file tree
Showing 21 changed files with 1,061 additions and 24 deletions.
197 changes: 196 additions & 1 deletion lib/PuppeteerSharp.Nunit/TestExpectations/TestExpectations.local.json
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,202 @@
},
{
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
"testIdPattern": "[evaluation.spec] *",
"testIdPattern": "[evaluation.spec] *should accept element handle as an argument*",
"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": "[evaluation.spec] *tricky*",
"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": "[evaluation.spec] *circular*",
"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": "[evaluation.spec] *should throw*",
"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": "[evaluation.spec] *objects*",
"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": "[evaluation.spec] *exposed*",
"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": "[evaluation.spec] *should properly serialize null fields*",
"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": "[evaluation.spec] *promise*",
"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": "[evaluation.spec] *error messages*",
"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": "[evaluation.spec] *should work right after framenavigated*",
"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": "[evaluation.spec] *should work with unicode chars*",
"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": "[evaluation.spec] *execution context*",
"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": "[evaluation.spec] *Evaluation specs Page.evaluateOnNewDocument*",
"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": "[evaluation.spec] *Page.removeScriptToEvaluateOnNewDocument*",
"platforms": [
"darwin",
"linux",
Expand Down
7 changes: 2 additions & 5 deletions lib/PuppeteerSharp.Tests/EvaluationTests/PageEvaluateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ namespace PuppeteerSharp.Tests.EvaluationTests
{
public class EvaluateTests : PuppeteerPageBaseTest
{
public EvaluateTests() : base()
{
}

[Test]
[Retry(2)]
[PuppeteerTest("evaluation.spec", "Evaluation specs Page.evaluate", "should work")]
Expand Down Expand Up @@ -191,6 +187,7 @@ public async Task BasicEvaluationTest(string script, object expected)
public async Task ShouldAcceptNullAsOneOfMultipleParameters()
{
var result = await Page.EvaluateFunctionAsync<bool>(

"(a, b) => Object.is(a, null) && Object.is(b, 'foo')",
null,
"foo");
Expand Down Expand Up @@ -236,7 +233,7 @@ public async Task ShouldBeAbleToThrowATrickyError()
[TestCase("1 + 2;", 3)]
[TestCase("1 + 5;", 6)]
[TestCase("2 + 5\n// do some math!'", 7)]
public async Task BasicIntExressionEvaluationTest(string script, object expected)
public async Task BasicIntExpressionEvaluationTest(string script, object expected)
{
var result = await Page.EvaluateExpressionAsync<int>(script);
Assert.That(result, Is.EqualTo(expected));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@
using PuppeteerSharp.Helpers;
using PuppeteerSharp.Nunit;

namespace PuppeteerSharp.Tests.PageTests
namespace PuppeteerSharp.Tests.NavigationTests
{
public class PageWaitForNavigationTests : PuppeteerPageBaseTest
{
public PageWaitForNavigationTests() : base()
{
}

[Test, Retry(2), PuppeteerTest("navigation.spec", "navigation Page.waitForNavigation", "should work")]
public async Task ShouldWork()
{
Expand All @@ -30,10 +26,7 @@ await Task.WhenAll(
public async Task ShouldWorkWithBothDomcontentloadedAndLoad()
{
var responseCompleted = new TaskCompletionSource<bool>();
Server.SetRoute("/one-style.css", _ =>
{
return responseCompleted.Task;
});
Server.SetRoute("/one-style.css", _ => responseCompleted.Task);

var waitForRequestTask = Server.WaitForRequest("/one-style.css");
var navigationTask = Page.GoToAsync(TestConstants.ServerUrl + "/one-style.html");
Expand Down
18 changes: 18 additions & 0 deletions lib/PuppeteerSharp/Bidi/BidiFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,18 @@ namespace PuppeteerSharp.Bidi;
public class BidiFrame : Frame
{
private readonly ConcurrentDictionary<BrowsingContext, BidiFrame> _frames = new();
private readonly Realms _realms;

internal BidiFrame(BidiPage parentPage, BidiFrame parentFrame, BrowsingContext browsingContext)
{
ParentPage = parentPage;
ParentFrame = parentFrame;
BrowsingContext = browsingContext;
_realms = new Realms(
#pragma warning disable CA2000
BidiFrameRealm.From(browsingContext.DefaultRealm, this),
BidiFrameRealm.From(browsingContext.CreateWindowRealm($"__puppeteer_internal_{new Random().Next(0, 10000)}"), this));
#pragma warning restore CA2000
}

/// <inheritdoc />
Expand All @@ -54,6 +60,11 @@ internal BidiFrame(BidiPage parentPage, BidiFrame parentFrame, BrowsingContext b
/// <inheritdoc />
public override CDPSession Client { get; protected set; }

/// <inheritdoc />
internal override Realm MainRealm => _realms.Default;

internal override Realm IsolatedRealm => _realms.Internal;

internal BidiPage BidiPage
{
get
Expand Down Expand Up @@ -286,5 +297,12 @@ private void CreateFrameTarget(BrowsingContext browsingContext)
_frames.TryRemove(browsingContext, out var _);
};
}

private class Realms(BidiFrameRealm defaultRealm, BidiFrameRealm internalRealm)
{
public BidiFrameRealm Default { get; } = defaultRealm;

public BidiFrameRealm Internal { get; } = internalRealm;
}
}

64 changes: 64 additions & 0 deletions lib/PuppeteerSharp/Bidi/BidiFrameRealm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// * 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.Threading.Tasks;

namespace PuppeteerSharp.Bidi;

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

public static BidiFrameRealm From(WindowRealm realm, BidiFrame frame)
{
var frameRealm = new BidiFrameRealm(realm, frame);
frameRealm.Initialize();
return frameRealm;
}

public override async Task<IJSHandle> GetPuppeteerUtilAsync()
{
var installTcs = new TaskCompletionSource<bool>();

if (!_bindingsInstalled)
{
// TODO: Implement
installTcs.TrySetResult(true);
_bindingsInstalled = true;
}

await installTcs.Task.ConfigureAwait(false);
return await base.GetPuppeteerUtilAsync().ConfigureAwait(false);
}

protected override void Initialize()
{
base.Initialize();

_realm.Updated += (_, __) =>
{
(Environment as Frame)?.ClearDocumentHandle();
_bindingsInstalled = false;
};
}
}
Loading

0 comments on commit f07f043

Please sign in to comment.