How to use ShouldFillContenteditable method of Microsoft.Playwright.Tests.PageFillTests class

Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.PageFillTests.ShouldFillContenteditable

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

PageFillTests.cs

Source: PageFillTests.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.NUnit;
27using NUnit.Framework;
28
29namespace Microsoft.Playwright.Tests
30{
31    public class PageFillTests : PageTestEx
32    {
33        [PlaywrightTest("page-fill.spec.ts", "should fill textarea")]
34        public async Task ShouldFillTextarea()
35        {
36            await Page.GotoAsync(Server.Prefix + "/input/textarea.html");
37            await Page.FillAsync("textarea", "some value");
38            Assert.AreEqual("some value", await Page.EvaluateAsync<string>("() => result"));
39        }
40
41        [PlaywrightTest("page-fill.spec.ts", "should fill input")]
42        public async Task ShouldFillInput()
43        {
44            await Page.GotoAsync(Server.Prefix + "/input/textarea.html");
45            await Page.FillAsync("input", "some value");
46            Assert.AreEqual("some value", await Page.EvaluateAsync<string>("() => result"));
47        }
48
49        [PlaywrightTest("page-fill.spec.ts", "should throw on unsupported inputs")]
50        public async Task ShouldThrowOnUnsupportedInputs()
51        {
52            await Page.GotoAsync(Server.Prefix + "/input/textarea.html");
53            foreach (string type in new[] { "button", "checkbox", "file", "image", "radio", "reset", "submit" })
54            {
55                await Page.EvalOnSelectorAsync("input", "(input, type) => input.setAttribute('type', type)", type);
56                var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Page.FillAsync("input", string.Empty));
57                StringAssert.Contains($"input of type \"{type}\" cannot be filled", exception.Message);
58            }
59        }
60
61        [PlaywrightTest("page-fill.spec.ts", "should fill different input types")]
62        public async Task ShouldFillDifferentInputTypes()
63        {
64            await Page.GotoAsync(Server.Prefix + "/input/textarea.html");
65            foreach (string type in new[] { "password", "search", "tel", "text", "url" })
66            {
67                await Page.EvalOnSelectorAsync("input", "(input, type) => input.setAttribute('type', type)", type);
68                await Page.FillAsync("input", "text " + type);
69                Assert.AreEqual("text " + type, await Page.EvaluateAsync<string>("() => result"));
70            }
71        }
72
73        [PlaywrightTest("page-fill.spec.ts", "should fill date input after clicking")]
74        public async Task ShouldFillDateInputAfterClicking()
75        {
76            await Page.SetContentAsync("<input type=date>");
77            await Page.ClickAsync("input");
78            await Page.FillAsync("input", "2020-03-02");
79            Assert.AreEqual("2020-03-02", await Page.EvalOnSelectorAsync<string>("input", "input => input.value"));
80        }
81
82        [PlaywrightTest("page-fill.spec.ts", "should throw on incorrect date")]
83        [Skip(SkipAttribute.Targets.Webkit)]
84        public async Task ShouldThrowOnIncorrectDate()
85        {
86            await Page.SetContentAsync("<input type=date>");
87            await Page.ClickAsync("input");
88            var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Page.FillAsync("input", "2020-13-02"));
89            StringAssert.Contains("Malformed value", exception.Message);
90        }
91
92        [PlaywrightTest("page-fill.spec.ts", "should fill time input after clicking")]
93        public async Task ShouldFillTimeInputAfterClicking()
94        {
95            await Page.SetContentAsync("<input type=time>");
96            await Page.ClickAsync("input");
97            await Page.FillAsync("input", "13:15");
98            Assert.AreEqual("13:15", await Page.EvalOnSelectorAsync<string>("input", "input => input.value"));
99        }
100
101        [PlaywrightTest("page-fill.spec.ts", "should throw on incorrect time")]
102        [Skip(SkipAttribute.Targets.Webkit)]
103        public async Task ShouldThrowOnIncorrectTime()
104        {
105            await Page.SetContentAsync("<input type=time>");
106            await Page.ClickAsync("input");
107            var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Page.FillAsync("input", "25:05"));
108            StringAssert.Contains("Malformed value", exception.Message);
109        }
110
111        [PlaywrightTest("page-fill.spec.ts", "should fill datetime-local input")]
112        public async Task ShouldFillDatetimeLocalInput()
113        {
114            await Page.SetContentAsync("<input type=datetime-local>");
115            await Page.ClickAsync("input");
116            await Page.FillAsync("input", "2020-03-02T05:15");
117            Assert.AreEqual("2020-03-02T05:15", await Page.EvalOnSelectorAsync<string>("input", "input => input.value"));
118        }
119
120        [PlaywrightTest("page-fill.spec.ts", "should throw on incorrect datetime-local")]
121        [Skip(SkipAttribute.Targets.Firefox, SkipAttribute.Targets.Webkit)]
122        public async Task ShouldThrowOnIncorrectDateTimeLocal()
123        {
124            await Page.SetContentAsync("<input type=datetime-local>");
125            await Page.ClickAsync("input");
126            var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Page.FillAsync("input", "abc"));
127            StringAssert.Contains("Malformed value", exception.Message);
128        }
129
130        [PlaywrightTest("page-fill.spec.ts", "should fill contenteditable")]
131        public async Task ShouldFillContenteditable()
132        {
133            await Page.GotoAsync(Server.Prefix + "/input/textarea.html");
134            await Page.FillAsync("div[contenteditable]", "some value");
135            Assert.AreEqual("some value", await Page.EvalOnSelectorAsync<string>("div[contenteditable]", "div => div.textContent"));
136        }
137
138        [PlaywrightTest("page-fill.spec.ts", "should fill elements with existing value and selection")]
139        public async Task ShouldFillElementsWithExistingValueAndSelection()
140        {
141            await Page.GotoAsync(Server.Prefix + "/input/textarea.html");
142
143            await Page.EvalOnSelectorAsync("input", "input => input.value = 'value one'");
144            await Page.FillAsync("input", "another value");
145            Assert.AreEqual("another value", await Page.EvaluateAsync<string>("() => result"));
146
147            await Page.EvalOnSelectorAsync("input", @"input => {
148                input.selectionStart = 1;
149                input.selectionEnd = 2;
150            }");
151            await Page.FillAsync("input", "maybe this one");
152            Assert.AreEqual("maybe this one", await Page.EvaluateAsync<string>("() => result"));
153
154            await Page.EvalOnSelectorAsync("div[contenteditable]", @"div => {
155                div.innerHTML = 'some text <span>some more text<span> and even more text';
156                var range = document.createRange();
157                range.selectNodeContents(div.querySelector('span'));
158                var selection = window.getSelection();
159                selection.removeAllRanges();
160                selection.addRange(range);
161            }");
162            await Page.FillAsync("div[contenteditable]", "replace with this");
163            Assert.AreEqual("replace with this", await Page.EvalOnSelectorAsync<string>("div[contenteditable]", "div => div.textContent"));
164        }
165
166        [PlaywrightTest("page-fill.spec.ts", "should throw when element is not an &lt;input&gt;, &lt;textarea&gt; or [contenteditable]")]
167        public async Task ShouldThrowWhenElementIsNotAnInputOrTextareaOrContenteditable()
168        {
169            await Page.GotoAsync(Server.Prefix + "/input/textarea.html");
170            var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Page.FillAsync("body", string.Empty));
171            StringAssert.Contains("Element is not an <input>", exception.Message);
172        }
173
174        [PlaywrightTest("page-fill.spec.ts", "should retry on disabled element")]
175        public async Task ShouldRetryOnDisabledElement()
176        {
177            await Page.GotoAsync(Server.Prefix + "/input/textarea.html");
178            await Page.EvalOnSelectorAsync("input", "i => i.disabled = true");
179
180            var task = Page.FillAsync("input", "some value");
181            await GiveItAChanceToFillAsync(Page);
182            Assert.False(task.IsCompleted);
183            Assert.IsEmpty(await Page.EvaluateAsync<string>("() => result"));
184
185            await Page.EvalOnSelectorAsync("input", "i => i.disabled = false");
186            await task;
187            Assert.AreEqual("some value", await Page.EvaluateAsync<string>("() => result"));
188        }
189
190        [PlaywrightTest("page-fill.spec.ts", "should retry on readonly element")]
191        public async Task ShouldRetryOnReadonlyElement()
192        {
193            await Page.GotoAsync(Server.Prefix + "/input/textarea.html");
194            await Page.EvalOnSelectorAsync("textarea", "i => i.readOnly = true");
195            var task = Page.FillAsync("textarea", "some value");
196            await GiveItAChanceToFillAsync(Page);
197            Assert.False(task.IsCompleted);
198            Assert.IsEmpty(await Page.EvaluateAsync<string>("() => result"));
199
200            await Page.EvalOnSelectorAsync("textarea", "i => i.readOnly = false");
201            await task;
202            Assert.AreEqual("some value", await Page.EvaluateAsync<string>("() => result"));
203        }
204
205        [PlaywrightTest("page-fill.spec.ts", "should retry on invisible element")]
206        public async Task ShouldRetryOnInvisibleElement()
207        {
208            await Page.GotoAsync(Server.Prefix + "/input/textarea.html");
209            await Page.EvalOnSelectorAsync("input", "i => i.style.display = 'none'");
210
211            var task = Page.FillAsync("input", "some value");
212            await GiveItAChanceToFillAsync(Page);
213            Assert.False(task.IsCompleted);
214            Assert.IsEmpty(await Page.EvaluateAsync<string>("() => result"));
215
216            await Page.EvalOnSelectorAsync("input", "i => i.style.display = 'inline'");
217            await task;
218            Assert.AreEqual("some value", await Page.EvaluateAsync<string>("() => result"));
219        }
220
221        [PlaywrightTest("page-fill.spec.ts", "should be able to fill the body")]
222        public async Task ShouldBeAbleToFillTheBody()
223        {
224            await Page.SetContentAsync("<body contentEditable=\"true\"></body>");
225            await Page.FillAsync("body", "some value");
226            Assert.AreEqual("some value", await Page.EvaluateAsync<string>("() => document.body.textContent"));
227        }
228
229        [PlaywrightTest("page-fill.spec.ts", "should fill fixed position input")]
230        public async Task ShouldFillFixedPositionInput()
231        {
232            await Page.SetContentAsync("<input style='position: fixed;' />");
233            await Page.FillAsync("input", "some value");
234            Assert.AreEqual("some value", await Page.EvalOnSelectorAsync<string>("input", "i => i.value"));
235        }
236
237        [PlaywrightTest("page-fill.spec.ts", "should be able to fill when focus is in the wrong frame")]
238        public async Task ShouldBeAbleToFillWhenFocusIsInTheWrongFrame()
239        {
240            await Page.SetContentAsync("<div contentEditable=\"true\"></div><iframe></iframe>");
241            await Page.FocusAsync("iframe");
242            await Page.FillAsync("div", "some value");
243            Assert.AreEqual("some value", await Page.EvalOnSelectorAsync<string>("div", "d => d.textContent"));
244        }
245
246        [PlaywrightTest("page-fill.spec.ts", "should be able to fill the input[type=number]")]
247        public async Task ShouldBeAbleToFillTheInputTypeNumber()
248        {
249            await Page.SetContentAsync("<input id=\"input\" type=\"number\"></input>");
250            await Page.FillAsync("input", "42");
251            Assert.AreEqual("42", await Page.EvaluateAsync<string>("() => input.value"));
252        }
253
254        [PlaywrightTest("page-fill.spec.ts", "should be able to fill exponent into the input[type=number]")]
255        public async Task ShouldBeAbleToFillTheInputExponentIntoTypeNumber()
256        {
257            await Page.SetContentAsync("<input id=\"input\" type=\"number\"></input>");
258            await Page.FillAsync("input", "-10e5");
259            Assert.AreEqual("-10e5", await Page.EvaluateAsync<string>("() => input.value"));
260        }
261
262        [PlaywrightTest("page-fill.spec.ts", "should be able to fill the input[type=number] with empty string")]
263        public async Task ShouldBeAbleToFillTheInputTypeNumberWithEmptyString()
264        {
265            await Page.SetContentAsync("<input id=\"input\" type=\"number\"></input>");
266            await Page.FillAsync("input", "");
267            Assert.IsEmpty(await Page.EvaluateAsync<string>("() => input.value"));
268        }
269
270        [PlaywrightTest("page-fill.spec.ts", "should not be able to fill text into the input[type=number]")]
271        public async Task ShouldNotBeAbleToFillTextIntoTheInputTypeNumber()
272        {
273            await Page.SetContentAsync("<input id=\"input\" type=\"number\"></input>");
274            var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Page.FillAsync("input", "abc"));
275            StringAssert.Contains("Cannot type text into input[type=number]", exception.Message);
276        }
277
278        [PlaywrightTest("page-fill.spec.ts", "should be able to clear")]
279        public async Task ShouldBeAbleToClear()
280        {
281            await Page.GotoAsync(Server.Prefix + "/input/textarea.html");
282            await Page.FillAsync("input", "some value");
283            Assert.AreEqual("some value", await Page.EvaluateAsync<string>("() => result"));
284            await Page.FillAsync("input", "");
285            Assert.IsEmpty(await Page.EvaluateAsync<string>("() => result"));
286        }
287
288        private async Task GiveItAChanceToFillAsync(IPage page)
289        {
290            for (int i = 0; i < 5; i++)
291            {
292                await page.EvaluateAsync("() => new Promise(f => requestAnimationFrame(() => requestAnimationFrame(f)))");
293            }
294        }
295    }
296}
297
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
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)