How to use ShouldHandleOddValues method of Microsoft.Playwright.Tests.PageEventPageErrorTests class

Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.PageEventPageErrorTests.ShouldHandleOddValues

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

PageEventPageErrorTests.cs

Source: PageEventPageErrorTests.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.Text.Json;
26using System.Threading.Tasks;
27using Microsoft.Playwright.NUnit;
28using NUnit.Framework;
29
30namespace Microsoft.Playwright.Tests
31{
32    public class PageEventPageErrorTests : PageTestEx
33    {
34        [PlaywrightTest("page-event-pageerror.spec.ts", "should fire")]
35        public async Task ShouldFire()
36        {
37            var errorEvent = new TaskCompletionSource<string>();
38            Page.PageError += (_, error) => errorEvent.TrySetResult(error);
39
40            var (error, _) = await TaskUtils.WhenAll(
41                errorEvent.Task,
42                Page.GotoAsync(Server.Prefix + "/error.html")
43            );
44
45            string expectedError = "";
46            if (TestConstants.IsFirefox)
47            {
48                expectedError = @"Error: Fancy error!
49    at c (myscript.js:14:11)
50    at b (myscript.js:10:5)
51    at a (myscript.js:6:5)
52    at  (myscript.js:3:1)";
53            }
54            else if (TestConstants.IsChromium)
55            {
56                expectedError = @"Error: Fancy error!
57    at c (myscript.js:14:11)
58    at b (myscript.js:10:5)
59    at a (myscript.js:6:5)
60    at myscript.js:3:1";
61            }
62            else if (TestConstants.IsWebKit)
63            {
64                expectedError = @$"Error: Fancy error!
65    at c ({Page.Url}:14:36)
66    at b ({Page.Url}:10:6)
67    at a ({Page.Url}:6:6)
68    at global code ({Page.Url}:3:2)";
69            }
70
71            expectedError = expectedError.Replace("\r", "");
72
73            StringAssert.AreEqualIgnoringCase(expectedError, error);
74        }
75
76        [PlaywrightTest("page-event-pageerror.spec.ts", "should contain sourceURL")]
77        [Skip(SkipAttribute.Targets.Webkit)]
78        public async Task ShouldContainSourceURL()
79        {
80            var pageError = new TaskCompletionSource<string>();
81            Page.PageError += (_, error) => pageError.TrySetResult(error);
82            var (error, _) = await TaskUtils.WhenAll(
83                pageError.Task,
84                Page.GotoAsync(Server.Prefix + "/error.html"));
85
86            StringAssert.Contains("myscript.js", error);
87        }
88
89        [PlaywrightTest("page-event-pageerror.spec.ts", "should handle odd values")]
90        public async Task ShouldHandleOddValues()
91        {
92            object[][] cases = new object[][]
93            {
94                new []{ null, "null"},
95                //[undefined], "undefined" Not undefined here
96                new object[]{ 0, "0"},
97                new []{ "", "" },
98            };
99
100            foreach (object[] kv in cases)
101            {
102                var pageError = new TaskCompletionSource<string>();
103                Page.PageError += (_, error) => pageError.TrySetResult(error);
104                var (error, _) = await TaskUtils.WhenAll(
105                    pageError.Task,
106                    Page.EvaluateAsync<JsonElement>("value => setTimeout(() => { throw value; }, 0)", kv[0]));
107
108                StringAssert.Contains(TestConstants.IsFirefox ? "uncaught exception: " + kv[1].ToString() : kv[1].ToString(), error);
109            }
110        }
111
112        [PlaywrightTest("page-event-pageerror.spec.ts", "should handle object")]
113        [Skip(SkipAttribute.Targets.Firefox)]
114        public async Task ShouldHandleObject()
115        {
116            var pageError = new TaskCompletionSource<string>();
117            Page.PageError += (_, error) => pageError.TrySetResult(error);
118            var (error, _) = await TaskUtils.WhenAll(
119                pageError.Task,
120                Page.EvaluateAsync<JsonElement>("value => setTimeout(() => { throw {}; }, 0)", 0));
121
122            StringAssert.Contains(TestConstants.IsChromium ? "Object" : "[object Object]", error);
123        }
124
125        [PlaywrightTest("page-event-pageerror.spec.ts", "should handle window")]
126        [Skip(SkipAttribute.Targets.Firefox)]
127        public async Task ShouldHandleWindow()
128        {
129            var pageError = new TaskCompletionSource<string>();
130            Page.PageError += (_, error) => pageError.TrySetResult(error);
131            var (error, _) = await TaskUtils.WhenAll(
132                pageError.Task,
133                Page.EvaluateAsync<JsonElement>("value => setTimeout(() => { throw window ; }, 0)", 0));
134
135            StringAssert.Contains(TestConstants.IsChromium ? "Window" : "[object Window]", error);
136        }
137    }
138}
139
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 ShouldHandleOddValues code on LambdaTest Cloud Grid

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