How to use ShouldAcceptMultipleNestedHandles method of Microsoft.Playwright.Tests.PageEvaluateHandleTests class

Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.PageEvaluateHandleTests.ShouldAcceptMultipleNestedHandles

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

PageEvaluateHandleTests.cs

Source: PageEvaluateHandleTests.cs Github

copy
1/*
2 * MIT License
3 *
4 * Copyright (c) Microsoft Corporation.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25using System.Dynamic;
26using System.Linq;
27using System.Text.Json;
28using System.Threading.Tasks;
29using Microsoft.Playwright.NUnit;
30using NUnit.Framework;
31
32namespace Microsoft.Playwright.Tests
33{
34    public class PageEvaluateHandleTests : PageTestEx
35    {
36        [PlaywrightTest("page-evaluate-handle.spec.ts", "should work")]
37        public async Task ShouldWork()
38        {
39            var windowHandle = await Page.EvaluateHandleAsync("() => window");
40            Assert.NotNull(windowHandle);
41        }
42
43        [PlaywrightTest("page-evaluate-handle.spec.ts", "should accept object handle as an argument")]
44        public async Task ShouldAcceptObjectHandleAsAnArgument()
45        {
46            var navigatorHandle = await Page.EvaluateHandleAsync("() => navigator");
47            string text = await Page.EvaluateAsync<string>("e => e.userAgent", navigatorHandle);
48            StringAssert.Contains("Mozilla", text);
49        }
50
51        [PlaywrightTest("page-evaluate-handle.spec.ts", "should accept object handle to primitive types")]
52        public async Task ShouldAcceptObjectHandleToPrimitiveTypes()
53        {
54            var aHandle = await Page.EvaluateHandleAsync("() => 5");
55            bool isFive = await Page.EvaluateAsync<bool>("e => Object.is (e, 5)", aHandle);
56            Assert.True(isFive);
57        }
58
59        [PlaywrightTest("page-evaluate-handle.spec.ts", "should accept nested handle")]
60        public async Task ShouldAcceptNestedHandle()
61        {
62            var foo = await Page.EvaluateHandleAsync("() => ({x: 1, y: 'foo'})");
63            dynamic result = await Page.EvaluateAsync<ExpandoObject>("({ foo }) => foo", new { foo });
64
65            Assert.AreEqual(1, result.x);
66            Assert.AreEqual("foo", result.y);
67        }
68
69        [PlaywrightTest("page-evaluate-handle.spec.ts", "should accept nested window handle")]
70        public async Task ShouldAcceptNestedWindowHandle()
71        {
72            var foo = await Page.EvaluateHandleAsync("() => window");
73            Assert.True(await Page.EvaluateAsync<bool>("({ foo }) => foo === window", new { foo }));
74        }
75
76        [PlaywrightTest("page-evaluate-handle.spec.ts", "should accept multiple nested handles")]
77        public async Task ShouldAcceptMultipleNestedHandles()
78        {
79            var foo = await Page.EvaluateHandleAsync("() => ({ x: 1, y: 'foo' })");
80            var bar = await Page.EvaluateHandleAsync("() => 5");
81            var baz = await Page.EvaluateHandleAsync("() => ['baz']");
82
83            string result = await Page.EvaluateAsync<string>(
84                "x => JSON.stringify(x)",
85                new
86                {
87                    a1 = new { foo },
88                    a2 = new
89                    {
90                        bar,
91                        arr = new[] { new { baz } },
92                    },
93                });
94
95            var json = JsonDocument.Parse(result).RootElement;
96
97            Assert.AreEqual(1, json.GetProperty("a1").GetProperty("foo").GetProperty("x").GetInt32());
98            Assert.AreEqual("foo", json.GetProperty("a1").GetProperty("foo").GetProperty("y").ToString());
99            Assert.AreEqual(5, json.GetProperty("a2").GetProperty("bar").GetInt32());
100            Assert.AreEqual("baz", json.GetProperty("a2").GetProperty("arr").EnumerateArray().First().GetProperty("baz").EnumerateArray().First().ToString());
101        }
102
103        [PlaywrightTest("page-evaluate-handle.spec.ts", "should throw for circular objects")]
104        public async Task ShouldThrowForCircularObjects()
105        {
106            dynamic a = new ExpandoObject();
107            a.a = 1;
108            a.y = a;
109
110            var exception = await PlaywrightAssert.ThrowsAsync<JsonException>(() => Page.EvaluateAsync("x => x", a));
111            Assert.AreEqual("Argument is a circular structure", exception.Message);
112        }
113
114        [PlaywrightTest("page-evaluate-handle.spec.ts", "should accept same nested object multiple times")]
115        public async Task ShouldAcceptSameNestedObjectMultipleTimes()
116        {
117            dynamic foo = new { x = 1 };
118            string result = await Page.EvaluateAsync<string>(
119                "x => JSON.stringify(x)",
120                new
121                {
122                    foo,
123                    bar = new[] { foo },
124                    baz = new { foo },
125                });
126
127            var json = JsonDocument.Parse(result).RootElement;
128
129            Assert.AreEqual(1, json.GetProperty("foo").GetProperty("x").GetInt32());
130            Assert.AreEqual(1, json.GetProperty("bar").EnumerateArray().First().GetProperty("x").GetInt32());
131            Assert.AreEqual(1, json.GetProperty("baz").GetProperty("foo").GetProperty("x").GetInt32());
132        }
133
134        [PlaywrightTest("page-evaluate-handle.spec.ts", "should accept object handle to unserializable value")]
135        public async Task ShouldAcceptObjectHandleToUnserializableValue()
136        {
137            var aHandle = await Page.EvaluateHandleAsync("() => Infinity");
138            Assert.True(await Page.EvaluateAsync<bool>("e => Object.is(e, Infinity)", aHandle));
139        }
140
141        [PlaywrightTest("page-evaluate-handle.spec.ts", "should pass configurable args")]
142        public async Task ShouldPassConfigurableArgs()
143        {
144            JsonElement result = await Page.EvaluateAsync<JsonElement>(
145               @"arg =>{
146                  if (arg.foo !== 42)
147                    throw new Error('Not a 42');
148                  arg.foo = 17;
149                  if (arg.foo !== 17)
150                    throw new Error('Not 17');
151                  delete arg.foo;
152                  if (arg.foo === 17)
153                    throw new Error('Still 17');
154                  return arg;
155                }",
156                new { foo = 42 });
157
158            Assert.AreEqual("{}", result.ToString());
159        }
160
161        [PlaywrightTest("page-evaluate-handle.spec.ts", "should work with primitives")]
162        public async Task ShouldWorkWithPrimitives()
163        {
164            var aHandle = await Page.EvaluateHandleAsync(@"() => {
165                window.FOO = 123;
166                return window;
167            }");
168            Assert.AreEqual(123, await Page.EvaluateAsync<int>("e => e.FOO", aHandle));
169        }
170    }
171}
172
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 ShouldAcceptMultipleNestedHandles code on LambdaTest Cloud Grid

Execute automation tests with ShouldAcceptMultipleNestedHandles 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)