22using NUnit . Framework ;
33using PuppeteerSharp . Nunit ;
44
5- namespace PuppeteerSharp . Tests . QueryObjectTests
5+ namespace PuppeteerSharp . Tests . QueryObjectsTests
66{
77 public class QueryObjectsTests : PuppeteerPageBaseTest
88 {
@@ -37,14 +37,46 @@ await Page.EvaluateFunctionAsync<int>(@"objects => {
3737 }" , objectsHandle ) ) ;
3838 }
3939
40+ [ Test , Retry ( 2 ) , PuppeteerTest ( "queryObjects.spec" , "page.queryObjects" , "should work for non-trivial page" ) ]
41+ public async Task ShouldWorkForNonTrivialPage ( )
42+ {
43+ await Page . GoToAsync ( TestConstants . EmptyPage ) ;
44+ // Create a custom class
45+ var classHandle = await Page . EvaluateFunctionHandleAsync ( @"() => {
46+ return class CustomClass { };
47+ }" ) ;
48+
49+ // Create an instance.
50+ await Page . EvaluateFunctionAsync ( @"CustomClass => {
51+ self.customClass = new CustomClass();
52+ }" , classHandle ) ;
53+
54+ // Validate only one has been added.
55+ var prototypeHandle = await Page . EvaluateFunctionHandleAsync ( @"CustomClass => {
56+ return CustomClass.prototype;
57+ }" , classHandle ) ;
58+
59+ var objectsHandle = await Page . QueryObjectsAsync ( prototypeHandle ) ;
60+ Assert . AreEqual (
61+ 1 ,
62+ await Page . EvaluateFunctionAsync < int > ( @"objects => {
63+ return objects.length;
64+ }" , objectsHandle ) ) ;
65+
66+ // Check that instances.
67+ Assert . True ( await Page . EvaluateFunctionAsync < bool > ( @"objects => {
68+ return objects[0] === self.customClass;
69+ }" , objectsHandle ) ) ;
70+ }
71+
4072 [ Test , Retry ( 2 ) , PuppeteerTest ( "queryObjects.spec" , "page.queryObjects" , "should fail for disposed handles" ) ]
4173 public async Task ShouldFailForDisposedHandles ( )
4274 {
4375 var prototypeHandle = await Page . EvaluateExpressionHandleAsync ( "HTMLBodyElement.prototype" ) ;
4476 await prototypeHandle . DisposeAsync ( ) ;
4577 var exception = Assert . ThrowsAsync < PuppeteerException > ( ( )
4678 => Page . QueryObjectsAsync ( prototypeHandle ) ) ;
47- Assert . AreEqual ( "Prototype JSHandle is disposed!" , exception . Message ) ;
79+ Assert . AreEqual ( "Prototype JSHandle is disposed!" , exception ! . Message ) ;
4880 }
4981
5082 [ Test , Retry ( 2 ) , PuppeteerTest ( "queryObjects.spec" , "page.queryObjects" , "should fail primitive values as prototypes" ) ]
@@ -53,7 +85,7 @@ public async Task ShouldFailPrimitiveValuesAsPrototypes()
5385 var prototypeHandle = await Page . EvaluateExpressionHandleAsync ( "42" ) ;
5486 var exception = Assert . ThrowsAsync < PuppeteerException > ( ( )
5587 => Page . QueryObjectsAsync ( prototypeHandle ) ) ;
56- Assert . AreEqual ( "Prototype JSHandle must not be referencing primitive value" , exception . Message ) ;
88+ Assert . AreEqual ( "Prototype JSHandle must not be referencing primitive value" , exception ! . Message ) ;
5789 }
5890 }
5991}
0 commit comments