From 32c35f8aeacb5c54f07c384f5124d4927fc23dc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Kondratiuk?= Date: Mon, 6 May 2024 13:15:08 -0300 Subject: [PATCH] Add missing query object tests (#2623) --- .../QueryObjectsTests/QueryObjectsTests.cs | 38 +++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/lib/PuppeteerSharp.Tests/QueryObjectsTests/QueryObjectsTests.cs b/lib/PuppeteerSharp.Tests/QueryObjectsTests/QueryObjectsTests.cs index 7623c8777..83f208eda 100644 --- a/lib/PuppeteerSharp.Tests/QueryObjectsTests/QueryObjectsTests.cs +++ b/lib/PuppeteerSharp.Tests/QueryObjectsTests/QueryObjectsTests.cs @@ -2,7 +2,7 @@ using NUnit.Framework; using PuppeteerSharp.Nunit; -namespace PuppeteerSharp.Tests.QueryObjectTests +namespace PuppeteerSharp.Tests.QueryObjectsTests { public class QueryObjectsTests : PuppeteerPageBaseTest { @@ -37,6 +37,38 @@ public async Task ShouldWork() }", objectsHandle)); } + [Test, Retry(2), PuppeteerTest("queryObjects.spec", "page.queryObjects", "should work for non-trivial page")] + public async Task ShouldWorkForNonTrivialPage() + { + await Page.GoToAsync(TestConstants.EmptyPage); + // Create a custom class + var classHandle = await Page.EvaluateFunctionHandleAsync(@"() => { + return class CustomClass { }; + }"); + + // Create an instance. + await Page.EvaluateFunctionAsync(@"CustomClass => { + self.customClass = new CustomClass(); + }", classHandle); + + // Validate only one has been added. + var prototypeHandle = await Page.EvaluateFunctionHandleAsync(@"CustomClass => { + return CustomClass.prototype; + }", classHandle); + + var objectsHandle = await Page.QueryObjectsAsync(prototypeHandle); + Assert.AreEqual( + 1, + await Page.EvaluateFunctionAsync(@"objects => { + return objects.length; + }", objectsHandle)); + + // Check that instances. + Assert.True(await Page.EvaluateFunctionAsync(@"objects => { + return objects[0] === self.customClass; + }", objectsHandle)); + } + [Test, Retry(2), PuppeteerTest("queryObjects.spec", "page.queryObjects", "should fail for disposed handles")] public async Task ShouldFailForDisposedHandles() { @@ -44,7 +76,7 @@ public async Task ShouldFailForDisposedHandles() await prototypeHandle.DisposeAsync(); var exception = Assert.ThrowsAsync(() => Page.QueryObjectsAsync(prototypeHandle)); - Assert.AreEqual("Prototype JSHandle is disposed!", exception.Message); + Assert.AreEqual("Prototype JSHandle is disposed!", exception!.Message); } [Test, Retry(2), PuppeteerTest("queryObjects.spec", "page.queryObjects", "should fail primitive values as prototypes")] @@ -53,7 +85,7 @@ public async Task ShouldFailPrimitiveValuesAsPrototypes() var prototypeHandle = await Page.EvaluateExpressionHandleAsync("42"); var exception = Assert.ThrowsAsync(() => Page.QueryObjectsAsync(prototypeHandle)); - Assert.AreEqual("Prototype JSHandle must not be referencing primitive value", exception.Message); + Assert.AreEqual("Prototype JSHandle must not be referencing primitive value", exception!.Message); } } }