Skip to content

Commit

Permalink
Add missing query object tests (#2623)
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok committed May 6, 2024
1 parent cca33d3 commit 32c35f8
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions lib/PuppeteerSharp.Tests/QueryObjectsTests/QueryObjectsTests.cs
Expand Up @@ -2,7 +2,7 @@
using NUnit.Framework;
using PuppeteerSharp.Nunit;

namespace PuppeteerSharp.Tests.QueryObjectTests
namespace PuppeteerSharp.Tests.QueryObjectsTests
{
public class QueryObjectsTests : PuppeteerPageBaseTest
{
Expand Down Expand Up @@ -37,14 +37,46 @@ 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<int>(@"objects => {
return objects.length;
}", objectsHandle));

// Check that instances.
Assert.True(await Page.EvaluateFunctionAsync<bool>(@"objects => {
return objects[0] === self.customClass;
}", objectsHandle));
}

[Test, Retry(2), PuppeteerTest("queryObjects.spec", "page.queryObjects", "should fail for disposed handles")]
public async Task ShouldFailForDisposedHandles()
{
var prototypeHandle = await Page.EvaluateExpressionHandleAsync("HTMLBodyElement.prototype");
await prototypeHandle.DisposeAsync();
var exception = Assert.ThrowsAsync<PuppeteerException>(()
=> 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")]
Expand All @@ -53,7 +85,7 @@ public async Task ShouldFailPrimitiveValuesAsPrototypes()
var prototypeHandle = await Page.EvaluateExpressionHandleAsync("42");
var exception = Assert.ThrowsAsync<PuppeteerException>(()
=> 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);
}
}
}

0 comments on commit 32c35f8

Please sign in to comment.