How to use PageDialogTests class of Microsoft.Playwright.Tests package

Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.PageDialogTests

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

PageDialogTests.cs

Source: PageDialogTests.cs Github

copy
1/*
2 * MIT License
3 *
4 * Copyright (c) 2020 Darío Kondratiuk
5 * Modifications copyright (c) Microsoft Corporation.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26using System.Threading.Tasks;
27using Microsoft.Playwright.NUnit;
28using NUnit.Framework;
29
30namespace Microsoft.Playwright.Tests
31{
32    public class PageDialogTests : PageTestEx
33    {
34        [PlaywrightTest("page-dialog.spec.ts", "should fire")]
35        public Task ShouldFire()
36        {
37            Page.Dialog += async (_, e) =>
38            {
39                Assert.AreEqual(DialogType.Alert, e.Type);
40                Assert.AreEqual(string.Empty, e.DefaultValue);
41                Assert.AreEqual("yo", e.Message);
42
43                await e.AcceptAsync();
44            };
45
46            return Page.EvaluateAsync("alert('yo');");
47        }
48
49        [PlaywrightTest("page-dialog.spec.ts", "should allow accepting prompts")]
50        public async Task ShouldAllowAcceptingPrompts()
51        {
52            Page.Dialog += async (_, e) =>
53            {
54                Assert.AreEqual(DialogType.Prompt, e.Type);
55                Assert.AreEqual("yes.", e.DefaultValue);
56                Assert.AreEqual("question?", e.Message);
57
58                await e.AcceptAsync("answer!");
59            };
60
61            string result = await Page.EvaluateAsync<string>("prompt('question?', 'yes.')");
62            Assert.AreEqual("answer!", result);
63        }
64
65        [PlaywrightTest("page-dialog.spec.ts", "should dismiss the prompt")]
66        public async Task ShouldDismissThePrompt()
67        {
68            Page.Dialog += async (_, e) =>
69            {
70                await e.DismissAsync();
71            };
72
73            string result = await Page.EvaluateAsync<string>("prompt('question?')");
74            Assert.Null(result);
75        }
76
77        [PlaywrightTest("page-dialog.spec.ts", "should accept the confirm prompt")]
78        public async Task ShouldAcceptTheConfirmPrompts()
79        {
80            Page.Dialog += async (_, e) =>
81            {
82                await e.AcceptAsync();
83            };
84
85            bool result = await Page.EvaluateAsync<bool>("confirm('boolean?')");
86            Assert.True(result);
87        }
88
89        [PlaywrightTest("page-dialog.spec.ts", "should dismiss the confirm prompt")]
90        public async Task ShouldDismissTheConfirmPrompt()
91        {
92            Page.Dialog += async (_, e) =>
93            {
94                await e.DismissAsync();
95            };
96
97            bool result = await Page.EvaluateAsync<bool>("prompt('boolean?')");
98            Assert.False(result);
99        }
100
101        [PlaywrightTest("page-dialog.spec.ts", "should log prompt actions")]
102        public async Task ShouldLogPromptActions()
103        {
104            Page.Dialog += async (_, e) =>
105            {
106                await e.DismissAsync();
107            };
108
109            bool result = await Page.EvaluateAsync<bool>("prompt('boolean?')");
110            Assert.False(result);
111        }
112
113        [PlaywrightTest("page-dialog.spec.ts", "should be able to close context with open alert")]
114        [Skip(SkipAttribute.Targets.Webkit)]
115        public async Task ShouldBeAbleToCloseContextWithOpenAlert()
116        {
117            var context = await Browser.NewContextAsync();
118            var page = await context.NewPageAsync();
119
120            var alertEvent = new TaskCompletionSource<IDialog>();
121            page.Dialog += (_, dialog) => alertEvent.TrySetResult(dialog);
122
123            await page.EvaluateAsync("() => setTimeout(() => alert('hello'), 0)");
124            await alertEvent.Task;
125            await context.CloseAsync();
126        }
127
128        [PlaywrightTest("page-dialog.spec.ts", "should auto-dismiss the prompt without listeners")]
129        public async Task ShouldAutoDismissThePrompt()
130        {
131            string result = await Page.EvaluateAsync<string>("prompt('question?')");
132            Assert.Null(result);
133        }
134    }
135}
136
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

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

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)