How to use ShouldWorkWithUndefinedNullAndEmpty method of Microsoft.Playwright.Tests.JSHandlePropertiesTests class

Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.JSHandlePropertiesTests.ShouldWorkWithUndefinedNullAndEmpty

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

JSHandlePropertiesTests.cs

Source: JSHandlePropertiesTests.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.Threading.Tasks;
26using Microsoft.Playwright.Helpers;
27using Microsoft.Playwright.NUnit;
28using NUnit.Framework;
29
30namespace Microsoft.Playwright.Tests
31{
32    public class JSHandlePropertiesTests : PageTestEx
33    {
34        [PlaywrightTest("jshandle-properties.spec.ts", "getProperties should work")]
35        public async Task GetPropertiesShouldWork()
36        {
37            var aHandle = await Page.EvaluateHandleAsync(@"() => ({
38                foo: 'bar'
39            })");
40            var properties = await aHandle.GetPropertiesAsync();
41            Assert.True(properties.TryGetValue("foo", out var foo));
42            Assert.AreEqual("bar", await foo.JsonValueAsync<string>());
43        }
44
45        [PlaywrightTest("jshandle-properties.spec.ts", "should return empty map for non-objects")]
46        public async Task ShouldReturnEmptyMapForNonObjects()
47        {
48            var aHandle = await Page.EvaluateHandleAsync("() => 123");
49            var properties = await aHandle.GetPropertiesAsync();
50            Assert.IsEmpty(properties);
51        }
52
53        [PlaywrightTest("jshandle-properties.spec.ts", "should return even non-own properties")]
54        public async Task ShouldReturnEvenNonOwnProperties()
55        {
56            var aHandle = await Page.EvaluateHandleAsync(@"() => {
57                class A
58                {
59                    constructor()
60                    {
61                        this.a = '1';
62                    }
63                }
64                class B extends A
65                {
66                    constructor() {
67                        super();
68                        this.b = '2';
69                    }
70                }
71                return new B();
72            }");
73            var properties = await aHandle.GetPropertiesAsync();
74            Assert.AreEqual("1", await properties["a"].JsonValueAsync<string>());
75            Assert.AreEqual("2", await properties["b"].JsonValueAsync<string>());
76        }
77
78        [PlaywrightTest("jshandle-properties.spec.ts", "should work")]
79        public async Task ShouldWork()
80        {
81            var aHandle = await Page.EvaluateHandleAsync(@"() => ({
82                one: 1,
83                two: 2,
84                three: 3
85            })");
86            var twoHandle = await aHandle.GetPropertyAsync("two");
87            Assert.AreEqual(2, await twoHandle.JsonValueAsync<int>());
88        }
89
90        [PlaywrightTest("jshandle-properties.spec.ts", "should work with undefined, null, and empty")]
91        public async Task ShouldWorkWithUndefinedNullAndEmpty()
92        {
93            var aHandle = await Page.EvaluateHandleAsync(@"() => ({
94                undefined: undefined,
95                null: null,
96            })");
97            var undefinedHandle = await aHandle.GetPropertyAsync("undefined");
98            Assert.Null(await undefinedHandle.JsonValueAsync<string>());
99            var nullHandle = await aHandle.GetPropertyAsync("null");
100            Assert.Null(await nullHandle.JsonValueAsync<string>());
101            var emptyHandle = await aHandle.GetPropertyAsync("emptyHandle");
102            Assert.Null(await emptyHandle.JsonValueAsync<string>());
103        }
104
105        [PlaywrightTest("jshandle-properties.spec.ts", "should work with unserializable values")]
106        public async Task ShouldWorkWithUnserializableValues()
107        {
108            var aHandle = await Page.EvaluateHandleAsync(@"() => ({
109                infinity: Infinity,
110                nInfinity: -Infinity,
111                nan: NaN,
112                nzero: -0
113            })");
114
115            var infinityHandle = await aHandle.GetPropertyAsync("infinity");
116            Assert.AreEqual(double.PositiveInfinity, await infinityHandle.JsonValueAsync<double>());
117            var ninfinityHandle = await aHandle.GetPropertyAsync("nInfinity");
118            Assert.AreEqual(double.NegativeInfinity, await ninfinityHandle.JsonValueAsync<double>());
119            var nanHandle = await aHandle.GetPropertyAsync("nan");
120            Assert.AreEqual(double.NaN, await nanHandle.JsonValueAsync<double>());
121            var nzeroHandle = await aHandle.GetPropertyAsync("nzero");
122            Assert.True((await nzeroHandle.JsonValueAsync<double>()).IsNegativeZero());
123        }
124    }
125}
126
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 ShouldWorkWithUndefinedNullAndEmpty code on LambdaTest Cloud Grid

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