How to use ShouldWorkWithDoctype method of Microsoft.Playwright.Tests.PageSetContentTests class

Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.PageSetContentTests.ShouldWorkWithDoctype

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

PageSetContentTests.cs

Source: PageSetContentTests.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;
26using System.Threading;
27using System.Threading.Tasks;
28using Microsoft.Playwright.NUnit;
29using NUnit.Framework;
30
31namespace Microsoft.Playwright.Tests
32{
33    public class PageSetContentTests : PageTestEx
34    {
35        const string _expectedOutput = "<html><head></head><body><div>hello</div></body></html>";
36
37        /// <inheritdoc />
38        [PlaywrightTest("page-set-content.spec.ts", "should work")]
39        public async Task ShouldWork()
40        {
41            await Page.SetContentAsync("<div>hello</div>");
42            string result = await Page.ContentAsync();
43            Assert.AreEqual(_expectedOutput, result);
44        }
45
46        [PlaywrightTest("page-set-content.spec.ts", "should work with domcontentloaded")]
47        public async Task ShouldWorkWithDomcontentloaded()
48        {
49            await Page.SetContentAsync("<div>hello</div>", new() { WaitUntil = WaitUntilState.DOMContentLoaded });
50            string result = await Page.ContentAsync();
51            Assert.AreEqual(_expectedOutput, result);
52        }
53
54        [PlaywrightTest("page-set-content.spec.ts", "should work with doctype")]
55        public async Task ShouldWorkWithDoctype()
56        {
57            string doctype = "<!DOCTYPE html>";
58            await Page.SetContentAsync($"{doctype}<div>hello</div>");
59            string result = await Page.ContentAsync();
60            Assert.AreEqual($"{doctype}{_expectedOutput}", result);
61        }
62
63        [PlaywrightTest("page-set-content.spec.ts", "should work with HTML 4 doctype")]
64        public async Task ShouldWorkWithHTML4Doctype()
65        {
66            string doctype = "<!DOCTYPE html PUBLIC \" -//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">";
67            await Page.SetContentAsync($"{doctype}<div>hello</div>");
68            string result = await Page.ContentAsync();
69            Assert.AreEqual($"{doctype}{_expectedOutput}", result);
70        }
71
72        [PlaywrightTest("page-set-content.spec.ts", "should respect timeout")]
73        public Task ShouldRespectTimeout()
74        {
75            string imgPath = "/img.png";
76            // stall for image
77            Server.SetRoute(imgPath, _ => Task.Delay(Timeout.Infinite));
78            return PlaywrightAssert.ThrowsAsync<TimeoutException>(() =>
79                 Page.SetContentAsync($"<img src=\"{Server.Prefix + imgPath}\"></img>", new() { Timeout = 1 })
80            );
81        }
82
83        [PlaywrightTest("page-set-content.spec.ts", "should respect default navigation timeout")]
84        public async Task ShouldRespectDefaultNavigationTimeout()
85        {
86            Page.SetDefaultNavigationTimeout(1);
87            string imgPath = "/img.png";
88            // stall for image
89            Server.SetRoute(imgPath, _ => Task.Delay(Timeout.Infinite));
90            var exception = await PlaywrightAssert.ThrowsAsync<TimeoutException>(() =>
91                Page.SetContentAsync($"<img src=\"{Server.Prefix + imgPath}\"></img>", new() { Timeout = 1 })
92            );
93
94            StringAssert.Contains("Timeout 1ms exceeded", exception.Message);
95        }
96
97        [PlaywrightTest("page-set-content.spec.ts", "should await resources to load")]
98        public async Task ShouldAwaitResourcesToLoad()
99        {
100            string imgPath = "/img.png";
101            var imgResponse = new TaskCompletionSource<bool>();
102            Server.SetRoute(imgPath, _ => imgResponse.Task);
103            bool loaded = false;
104            var contentTask = Page.SetContentAsync($"<img src=\"{Server.Prefix + imgPath}\"></img>").ContinueWith(_ => loaded = true);
105            await Server.WaitForRequest(imgPath);
106            Assert.False(loaded);
107            imgResponse.SetResult(true);
108            await contentTask;
109        }
110
111        [PlaywrightTest("page-set-content.spec.ts", "should work fast enough")]
112        public async Task ShouldWorkFastEnough()
113        {
114            for (int i = 0; i < 20; ++i)
115            {
116                await Page.SetContentAsync("<div>yo</div>");
117            }
118        }
119
120        [PlaywrightTest("page-set-content.spec.ts", "should work with tricky content")]
121        public async Task ShouldWorkWithTrickyContent()
122        {
123            await Page.SetContentAsync("<div>hello world</div>" + "\x7F");
124            Assert.AreEqual("hello world", await Page.EvalOnSelectorAsync<string>("div", "div => div.textContent"));
125        }
126
127        [PlaywrightTest("page-set-content.spec.ts", "should work with accents")]
128        public async Task ShouldWorkWithAccents()
129        {
130            await Page.SetContentAsync("<div>aberración</div>");
131            Assert.AreEqual("aberración", await Page.EvalOnSelectorAsync<string>("div", "div => div.textContent"));
132        }
133
134        [PlaywrightTest("page-set-content.spec.ts", "should work with emojis")]
135        public async Task ShouldWorkWithEmojis()
136        {
137            await Page.SetContentAsync("<div>🐥</div>");
138            Assert.AreEqual("🐥", await Page.EvalOnSelectorAsync<string>("div", "div => div.textContent"));
139        }
140
141        [PlaywrightTest("page-set-content.spec.ts", "should work with newline")]
142        public async Task ShouldWorkWithNewline()
143        {
144            await Page.SetContentAsync("<div>\n</div>");
145            Assert.AreEqual("\n", await Page.EvalOnSelectorAsync<string>("div", "div => div.textContent"));
146        }
147    }
148}
149
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 ShouldWorkWithDoctype code on LambdaTest Cloud Grid

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