Skip to content

Commit f07f043

Browse files
authored
First bidi function evaluation implementation (#2831)
* Some progress * First passing tests * MVP * undo temp code * Ignore more tests * ignore another test
1 parent 0516b73 commit f07f043

21 files changed

+1061
-24
lines changed

lib/PuppeteerSharp.Nunit/TestExpectations/TestExpectations.local.json

Lines changed: 196 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,202 @@
527527
},
528528
{
529529
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
530-
"testIdPattern": "[evaluation.spec] *",
530+
"testIdPattern": "[evaluation.spec] *should accept element handle as an argument*",
531+
"platforms": [
532+
"darwin",
533+
"linux",
534+
"win32"
535+
],
536+
"parameters": [
537+
"webDriverBiDi"
538+
],
539+
"expectations": [
540+
"FAIL"
541+
]
542+
},
543+
{
544+
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
545+
"testIdPattern": "[evaluation.spec] *tricky*",
546+
"platforms": [
547+
"darwin",
548+
"linux",
549+
"win32"
550+
],
551+
"parameters": [
552+
"webDriverBiDi"
553+
],
554+
"expectations": [
555+
"FAIL"
556+
]
557+
},
558+
{
559+
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
560+
"testIdPattern": "[evaluation.spec] *circular*",
561+
"platforms": [
562+
"darwin",
563+
"linux",
564+
"win32"
565+
],
566+
"parameters": [
567+
"webDriverBiDi"
568+
],
569+
"expectations": [
570+
"FAIL"
571+
]
572+
},
573+
{
574+
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
575+
"testIdPattern": "[evaluation.spec] *should throw*",
576+
"platforms": [
577+
"darwin",
578+
"linux",
579+
"win32"
580+
],
581+
"parameters": [
582+
"webDriverBiDi"
583+
],
584+
"expectations": [
585+
"FAIL"
586+
]
587+
},
588+
{
589+
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
590+
"testIdPattern": "[evaluation.spec] *objects*",
591+
"platforms": [
592+
"darwin",
593+
"linux",
594+
"win32"
595+
],
596+
"parameters": [
597+
"webDriverBiDi"
598+
],
599+
"expectations": [
600+
"FAIL"
601+
]
602+
},
603+
{
604+
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
605+
"testIdPattern": "[evaluation.spec] *exposed*",
606+
"platforms": [
607+
"darwin",
608+
"linux",
609+
"win32"
610+
],
611+
"parameters": [
612+
"webDriverBiDi"
613+
],
614+
"expectations": [
615+
"FAIL"
616+
]
617+
},
618+
{
619+
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
620+
"testIdPattern": "[evaluation.spec] *should properly serialize null fields*",
621+
"platforms": [
622+
"darwin",
623+
"linux",
624+
"win32"
625+
],
626+
"parameters": [
627+
"webDriverBiDi"
628+
],
629+
"expectations": [
630+
"FAIL"
631+
]
632+
},
633+
{
634+
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
635+
"testIdPattern": "[evaluation.spec] *promise*",
636+
"platforms": [
637+
"darwin",
638+
"linux",
639+
"win32"
640+
],
641+
"parameters": [
642+
"webDriverBiDi"
643+
],
644+
"expectations": [
645+
"FAIL"
646+
]
647+
},
648+
{
649+
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
650+
"testIdPattern": "[evaluation.spec] *error messages*",
651+
"platforms": [
652+
"darwin",
653+
"linux",
654+
"win32"
655+
],
656+
"parameters": [
657+
"webDriverBiDi"
658+
],
659+
"expectations": [
660+
"FAIL"
661+
]
662+
},
663+
{
664+
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
665+
"testIdPattern": "[evaluation.spec] *should work right after framenavigated*",
666+
"platforms": [
667+
"darwin",
668+
"linux",
669+
"win32"
670+
],
671+
"parameters": [
672+
"webDriverBiDi"
673+
],
674+
"expectations": [
675+
"FAIL"
676+
]
677+
},
678+
{
679+
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
680+
"testIdPattern": "[evaluation.spec] *should work with unicode chars*",
681+
"platforms": [
682+
"darwin",
683+
"linux",
684+
"win32"
685+
],
686+
"parameters": [
687+
"webDriverBiDi"
688+
],
689+
"expectations": [
690+
"FAIL"
691+
]
692+
},
693+
{
694+
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
695+
"testIdPattern": "[evaluation.spec] *execution context*",
696+
"platforms": [
697+
"darwin",
698+
"linux",
699+
"win32"
700+
],
701+
"parameters": [
702+
"webDriverBiDi"
703+
],
704+
"expectations": [
705+
"FAIL"
706+
]
707+
},
708+
{
709+
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
710+
"testIdPattern": "[evaluation.spec] *Evaluation specs Page.evaluateOnNewDocument*",
711+
"platforms": [
712+
"darwin",
713+
"linux",
714+
"win32"
715+
],
716+
"parameters": [
717+
"webDriverBiDi"
718+
],
719+
"expectations": [
720+
"FAIL"
721+
]
722+
},
723+
{
724+
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
725+
"testIdPattern": "[evaluation.spec] *Page.removeScriptToEvaluateOnNewDocument*",
531726
"platforms": [
532727
"darwin",
533728
"linux",

lib/PuppeteerSharp.Tests/EvaluationTests/PageEvaluateTests.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ namespace PuppeteerSharp.Tests.EvaluationTests
1010
{
1111
public class EvaluateTests : PuppeteerPageBaseTest
1212
{
13-
public EvaluateTests() : base()
14-
{
15-
}
16-
1713
[Test]
1814
[Retry(2)]
1915
[PuppeteerTest("evaluation.spec", "Evaluation specs Page.evaluate", "should work")]
@@ -191,6 +187,7 @@ public async Task BasicEvaluationTest(string script, object expected)
191187
public async Task ShouldAcceptNullAsOneOfMultipleParameters()
192188
{
193189
var result = await Page.EvaluateFunctionAsync<bool>(
190+
194191
"(a, b) => Object.is(a, null) && Object.is(b, 'foo')",
195192
null,
196193
"foo");
@@ -236,7 +233,7 @@ public async Task ShouldBeAbleToThrowATrickyError()
236233
[TestCase("1 + 2;", 3)]
237234
[TestCase("1 + 5;", 6)]
238235
[TestCase("2 + 5\n// do some math!'", 7)]
239-
public async Task BasicIntExressionEvaluationTest(string script, object expected)
236+
public async Task BasicIntExpressionEvaluationTest(string script, object expected)
240237
{
241238
var result = await Page.EvaluateExpressionAsync<int>(script);
242239
Assert.That(result, Is.EqualTo(expected));

lib/PuppeteerSharp.Tests/NavigationTests/PageWaitForNavigationTests.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@
44
using PuppeteerSharp.Helpers;
55
using PuppeteerSharp.Nunit;
66

7-
namespace PuppeteerSharp.Tests.PageTests
7+
namespace PuppeteerSharp.Tests.NavigationTests
88
{
99
public class PageWaitForNavigationTests : PuppeteerPageBaseTest
1010
{
11-
public PageWaitForNavigationTests() : base()
12-
{
13-
}
14-
1511
[Test, Retry(2), PuppeteerTest("navigation.spec", "navigation Page.waitForNavigation", "should work")]
1612
public async Task ShouldWork()
1713
{
@@ -30,10 +26,7 @@ await Task.WhenAll(
3026
public async Task ShouldWorkWithBothDomcontentloadedAndLoad()
3127
{
3228
var responseCompleted = new TaskCompletionSource<bool>();
33-
Server.SetRoute("/one-style.css", _ =>
34-
{
35-
return responseCompleted.Task;
36-
});
29+
Server.SetRoute("/one-style.css", _ => responseCompleted.Task);
3730

3831
var waitForRequestTask = Server.WaitForRequest("/one-style.css");
3932
var navigationTask = Page.GoToAsync(TestConstants.ServerUrl + "/one-style.html");

lib/PuppeteerSharp/Bidi/BidiFrame.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,18 @@ namespace PuppeteerSharp.Bidi;
3434
public class BidiFrame : Frame
3535
{
3636
private readonly ConcurrentDictionary<BrowsingContext, BidiFrame> _frames = new();
37+
private readonly Realms _realms;
3738

3839
internal BidiFrame(BidiPage parentPage, BidiFrame parentFrame, BrowsingContext browsingContext)
3940
{
4041
ParentPage = parentPage;
4142
ParentFrame = parentFrame;
4243
BrowsingContext = browsingContext;
44+
_realms = new Realms(
45+
#pragma warning disable CA2000
46+
BidiFrameRealm.From(browsingContext.DefaultRealm, this),
47+
BidiFrameRealm.From(browsingContext.CreateWindowRealm($"__puppeteer_internal_{new Random().Next(0, 10000)}"), this));
48+
#pragma warning restore CA2000
4349
}
4450

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

63+
/// <inheritdoc />
64+
internal override Realm MainRealm => _realms.Default;
65+
66+
internal override Realm IsolatedRealm => _realms.Internal;
67+
5768
internal BidiPage BidiPage
5869
{
5970
get
@@ -286,5 +297,12 @@ private void CreateFrameTarget(BrowsingContext browsingContext)
286297
_frames.TryRemove(browsingContext, out var _);
287298
};
288299
}
300+
301+
private class Realms(BidiFrameRealm defaultRealm, BidiFrameRealm internalRealm)
302+
{
303+
public BidiFrameRealm Default { get; } = defaultRealm;
304+
305+
public BidiFrameRealm Internal { get; } = internalRealm;
306+
}
289307
}
290308

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// * MIT License
2+
// *
3+
// * Copyright (c) Darío Kondratiuk
4+
// *
5+
// * Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// * of this software and associated documentation files (the "Software"), to deal
7+
// * in the Software without restriction, including without limitation the rights
8+
// * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// * copies of the Software, and to permit persons to whom the Software is
10+
// * furnished to do so, subject to the following conditions:
11+
// *
12+
// * The above copyright notice and this permission notice shall be included in all
13+
// * copies or substantial portions of the Software.
14+
// *
15+
// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// * SOFTWARE.
22+
23+
using System.Threading.Tasks;
24+
25+
namespace PuppeteerSharp.Bidi;
26+
27+
internal class BidiFrameRealm(WindowRealm realm, BidiFrame frame) : BidiRealm(realm, frame.TimeoutSettings)
28+
{
29+
private readonly WindowRealm _realm = realm;
30+
private bool _bindingsInstalled;
31+
32+
public static BidiFrameRealm From(WindowRealm realm, BidiFrame frame)
33+
{
34+
var frameRealm = new BidiFrameRealm(realm, frame);
35+
frameRealm.Initialize();
36+
return frameRealm;
37+
}
38+
39+
public override async Task<IJSHandle> GetPuppeteerUtilAsync()
40+
{
41+
var installTcs = new TaskCompletionSource<bool>();
42+
43+
if (!_bindingsInstalled)
44+
{
45+
// TODO: Implement
46+
installTcs.TrySetResult(true);
47+
_bindingsInstalled = true;
48+
}
49+
50+
await installTcs.Task.ConfigureAwait(false);
51+
return await base.GetPuppeteerUtilAsync().ConfigureAwait(false);
52+
}
53+
54+
protected override void Initialize()
55+
{
56+
base.Initialize();
57+
58+
_realm.Updated += (_, __) =>
59+
{
60+
(Environment as Frame)?.ClearDocumentHandle();
61+
_bindingsInstalled = false;
62+
};
63+
}
64+
}

0 commit comments

Comments
 (0)