Skip to content

Commit

Permalink
Fix query objects tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok committed Apr 15, 2023
1 parent 347a69f commit 296cb79
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions lib/PuppeteerSharp.Tests/PageTests/QueryObjectsTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Runtime.Remoting;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.RazorPages;
using PuppeteerSharp.Tests.Attributes;
using PuppeteerSharp.Xunit;
using Xunit;
Expand All @@ -17,27 +19,33 @@ public QueryObjectsTests(ITestOutputHelper output) : base(output)
[SkipBrowserFact(skipFirefox: true)]
public async Task ShouldWork()
{
// Instantiate an object
await Page.EvaluateExpressionAsync("window.set = new Set(['hello', 'world'])");
var prototypeHandle = await Page.EvaluateExpressionHandleAsync("Set.prototype");
var objectsHandle = await Page.QueryObjectsAsync(prototypeHandle);
var count = await Page.EvaluateFunctionAsync<int>("objects => objects.length", objectsHandle);
Assert.Equal(1, count);
var values = await Page.EvaluateFunctionAsync<string[]>("objects => Array.from(objects[0].values())", objectsHandle);
Assert.Equal(new[] { "hello", "world" }, values);
}
// 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);

[PuppeteerTest("page.spec.ts", "ExecutionContext.queryObjects", "should work for non-blank page")]
[SkipBrowserFact(skipFirefox: true)]
public async Task ShouldWorkForNonBlankPage()
{
// Instantiate an object
await Page.GoToAsync(TestConstants.EmptyPage);
await Page.EvaluateFunctionAsync("() => window.set = new Set(['hello', 'world'])");
var prototypeHandle = await Page.EvaluateFunctionHandleAsync("() => Set.prototype");
var objectsHandle = await Page.QueryObjectsAsync(prototypeHandle);
var count = await Page.EvaluateFunctionAsync<int>("objects => objects.length", objectsHandle);
Assert.Equal(1, count);
Assert.Equal(
1,
await Page.EvaluateFunctionAsync(@"objects => {
return objects.length;
}", objectsHandle));


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

[PuppeteerTest("page.spec.ts", "ExecutionContext.queryObjects", "should fail for disposed handles")]
Expand Down

0 comments on commit 296cb79

Please sign in to comment.