How to use ShouldFailPrimitiveValuesAsPrototypes method of PuppeteerSharp.Tests.PageTests.QueryObjectsTests class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Tests.PageTests.QueryObjectsTests.ShouldFailPrimitiveValuesAsPrototypes

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

QueryObjectsTests.cs

Source: QueryObjectsTests.cs Github

copy
1using System.Threading.Tasks;
2using PuppeteerSharp.Tests.Attributes;
3using PuppeteerSharp.Xunit;
4using Xunit;
5using Xunit.Abstractions;
6
7namespace PuppeteerSharp.Tests.PageTests
8{
9    [Collection(TestConstants.TestFixtureCollectionName)]
10    public class QueryObjectsTests : PuppeteerPageBaseTest
11    {
12        public QueryObjectsTests(ITestOutputHelper output) : base(output)
13        {
14        }
15
16        [PuppeteerTest("page.spec.ts", "ExecutionContext.queryObjects", "should work")]
17        [SkipBrowserFact(skipFirefox: true)]
18        public async Task ShouldWork()
19        {
20            // Instantiate an object
21            await Page.EvaluateExpressionAsync("window.set = new Set(['hello', 'world'])");
22            var prototypeHandle = await Page.EvaluateExpressionHandleAsync("Set.prototype");
23            var objectsHandle = await Page.QueryObjectsAsync(prototypeHandle);
24            var count = await Page.EvaluateFunctionAsync<int>("objects => objects.length", objectsHandle);
25            Assert.Equal(1, count);
26            var values = await Page.EvaluateFunctionAsync<string[]>("objects => Array.from(objects[0].values())", objectsHandle);
27            Assert.Equal(new[] { "hello", "world" }, values);
28        }
29
30        [PuppeteerTest("page.spec.ts", "ExecutionContext.queryObjects", "should work for non-blank page")]
31        [SkipBrowserFact(skipFirefox: true)]
32        public async Task ShouldWorkForNonBlankPage()
33        {
34            // Instantiate an object
35            await Page.GoToAsync(TestConstants.EmptyPage);
36            await Page.EvaluateFunctionAsync("() => window.set = new Set(['hello', 'world'])");
37            var prototypeHandle = await Page.EvaluateFunctionHandleAsync("() => Set.prototype");
38            var objectsHandle = await Page.QueryObjectsAsync(prototypeHandle);
39            var count = await Page.EvaluateFunctionAsync<int>("objects => objects.length", objectsHandle);
40            Assert.Equal(1, count);
41        }
42
43        [PuppeteerTest("page.spec.ts", "ExecutionContext.queryObjects", "should fail for disposed handles")]
44        [PuppeteerFact]
45        public async Task ShouldFailForDisposedHandles()
46        {
47            var prototypeHandle = await Page.EvaluateExpressionHandleAsync("HTMLBodyElement.prototype");
48            await prototypeHandle.DisposeAsync();
49            var exception = await Assert.ThrowsAsync<PuppeteerException>(()
50                => Page.QueryObjectsAsync(prototypeHandle));
51            Assert.Equal("Prototype JSHandle is disposed!", exception.Message);
52        }
53
54        [PuppeteerTest("page.spec.ts", "ExecutionContext.queryObjects", "should fail primitive values as prototypes")]
55        [PuppeteerFact]
56        public async Task ShouldFailPrimitiveValuesAsPrototypes()
57        {
58            var prototypeHandle = await Page.EvaluateExpressionHandleAsync("42");
59            var exception = await Assert.ThrowsAsync<PuppeteerException>(()
60                => Page.QueryObjectsAsync(prototypeHandle));
61            Assert.Equal("Prototype JSHandle must not be referencing primitive value", exception.Message);
62        }
63    }
64}
65
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger ShouldFailPrimitiveValuesAsPrototypes code on LambdaTest Cloud Grid

Execute automation tests with ShouldFailPrimitiveValuesAsPrototypes on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)