How to use ShouldDeselectAllOptionsWhenPassedNoValuesForAMultipleSelect method of Microsoft.Playwright.Tests.PageSelectOptionTests class

Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.PageSelectOptionTests.ShouldDeselectAllOptionsWhenPassedNoValuesForAMultipleSelect

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

PageSelectOptionTests.cs

Source: PageSelectOptionTests.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.Linq;
27using System.Threading.Tasks;
28using Microsoft.Playwright.NUnit;
29using NUnit.Framework;
30
31namespace Microsoft.Playwright.Tests
32{
33    public class PageSelectOptionTests : PageTestEx
34    {
35        [PlaywrightTest("page-select-option.spec.ts", "should select single option")]
36        public async Task ShouldSelectSingleOption()
37        {
38            await Page.GotoAsync(Server.Prefix + "/input/select.html");
39            await Page.SelectOptionAsync("select", "blue");
40            Assert.AreEqual(new[] { "blue" }, await Page.EvaluateAsync<string[]>("() => result.onInput"));
41            Assert.AreEqual(new[] { "blue" }, await Page.EvaluateAsync<string[]>("() => result.onChange"));
42        }
43
44        [PlaywrightTest("page-select-option.spec.ts", "should select single option by value")]
45        public async Task ShouldSelectSingleOptionByValue()
46        {
47            await Page.GotoAsync(Server.Prefix + "/input/select.html");
48            await Page.SelectOptionAsync("select", new SelectOptionValue { Value = "blue" });
49            Assert.AreEqual(new[] { "blue" }, await Page.EvaluateAsync<string[]>("() => result.onInput"));
50            Assert.AreEqual(new[] { "blue" }, await Page.EvaluateAsync<string[]>("() => result.onChange"));
51        }
52
53        [PlaywrightTest("page-select-option.spec.ts", "should select single option by label")]
54        public async Task ShouldSelectSingleOptionByLabel()
55        {
56            await Page.GotoAsync(Server.Prefix + "/input/select.html");
57            await Page.SelectOptionAsync("select", new SelectOptionValue { Label = "Indigo" });
58            Assert.AreEqual(new[] { "indigo" }, await Page.EvaluateAsync<string[]>("() => result.onInput"));
59            Assert.AreEqual(new[] { "indigo" }, await Page.EvaluateAsync<string[]>("() => result.onChange"));
60        }
61
62        [PlaywrightTest("page-select-option.spec.ts", "should select single option by handle")]
63        public async Task ShouldSelectSingleOptionByHandle()
64        {
65            await Page.GotoAsync(Server.Prefix + "/input/select.html");
66            await Page.SelectOptionAsync("select", await Page.QuerySelectorAsync("[id=whiteOption]"));
67            Assert.AreEqual(new[] { "white" }, await Page.EvaluateAsync<string[]>("() => result.onInput"));
68            Assert.AreEqual(new[] { "white" }, await Page.EvaluateAsync<string[]>("() => result.onChange"));
69        }
70
71        [PlaywrightTest("page-select-option.spec.ts", "should select single option by index")]
72        public async Task ShouldSelectSingleOptionByIndex()
73        {
74            await Page.GotoAsync(Server.Prefix + "/input/select.html");
75            await Page.SelectOptionAsync("select", new SelectOptionValue { Index = 2 });
76            Assert.AreEqual(new[] { "brown" }, await Page.EvaluateAsync<string[]>("() => result.onInput"));
77            Assert.AreEqual(new[] { "brown" }, await Page.EvaluateAsync<string[]>("() => result.onChange"));
78        }
79
80        [PlaywrightTest("page-select-option.spec.ts", "should select single option by multiple attributes")]
81        public async Task ShouldSelectSingleOptionByMultipleAttributes()
82        {
83            await Page.GotoAsync(Server.Prefix + "/input/select.html");
84            await Page.SelectOptionAsync("select", new SelectOptionValue { Value = "green", Label = "Green" });
85            Assert.AreEqual(new[] { "green" }, await Page.EvaluateAsync<string[]>("() => result.onInput"));
86            Assert.AreEqual(new[] { "green" }, await Page.EvaluateAsync<string[]>("() => result.onChange"));
87        }
88
89        [PlaywrightTest("page-select-option.spec.ts", "should not select single option when some attributes do not match")]
90        public async Task ShouldNotSelectSingleOptionWhenSomeAttributesDoNotMatch()
91        {
92            await Page.GotoAsync(Server.Prefix + "/input/select.html");
93            await Page.EvalOnSelectorAsync("select", "s => s.value = undefined");
94            await PlaywrightAssert.ThrowsAsync<TimeoutException>(() => Page.SelectOptionAsync("select", new SelectOptionValue { Value = "green", Label = "Brown" }, new() { Timeout = 300 }));
95            Assert.IsEmpty(await Page.EvaluateAsync<string>("() => document.querySelector('select').value"));
96        }
97
98        [PlaywrightTest("page-select-option.spec.ts", "should select only first option")]
99        public async Task ShouldSelectOnlyFirstOption()
100        {
101            await Page.GotoAsync(Server.Prefix + "/input/select.html");
102            await Page.SelectOptionAsync("select", new[] { "blue", "green", "red" });
103            Assert.AreEqual(new[] { "blue" }, await Page.EvaluateAsync<string[]>("() => result.onInput"));
104            Assert.AreEqual(new[] { "blue" }, await Page.EvaluateAsync<string[]>("() => result.onChange"));
105        }
106
107        [PlaywrightTest("page-select-option.spec.ts", "should not throw when select causes navigation")]
108        public async Task ShouldNotThrowWhenSelectCausesNavigation()
109        {
110            await Page.GotoAsync(Server.Prefix + "/input/select.html");
111            await Page.EvalOnSelectorAsync("select", "select => select.addEventListener('input', () => window.location = '/empty.html')");
112            await TaskUtils.WhenAll(
113                Page.SelectOptionAsync("select", "blue"),
114                Page.WaitForNavigationAsync()
115            );
116            StringAssert.Contains("empty.html", Page.Url);
117        }
118
119        [PlaywrightTest("page-select-option.spec.ts", "should select multiple options")]
120        public async Task ShouldSelectMultipleOptions()
121        {
122            await Page.GotoAsync(Server.Prefix + "/input/select.html");
123            await Page.EvaluateAsync("() => makeMultiple()");
124            await Page.SelectOptionAsync("select", new[] { "blue", "green", "red" });
125            Assert.AreEqual(new[] { "blue", "green", "red" }, await Page.EvaluateAsync<string[]>("() => result.onInput"));
126            Assert.AreEqual(new[] { "blue", "green", "red" }, await Page.EvaluateAsync<string[]>("() => result.onChange"));
127        }
128
129        [PlaywrightTest("page-select-option.spec.ts", "should select multiple options with attributes")]
130        public async Task ShouldSelectMultipleOptionsWithAttributes()
131        {
132            await Page.GotoAsync(Server.Prefix + "/input/select.html");
133            await Page.EvaluateAsync("() => makeMultiple()");
134            await Page.SelectOptionAsync(
135                "select",
136                new[] {
137                    new SelectOptionValue { Value = "blue" },
138                    new SelectOptionValue { Label = "Green" },
139                    new SelectOptionValue { Index = 4 }
140                });
141            Assert.AreEqual(new[] { "blue", "gray", "green" }, await Page.EvaluateAsync<string[]>("() => result.onInput"));
142            Assert.AreEqual(new[] { "blue", "gray", "green" }, await Page.EvaluateAsync<string[]>("() => result.onChange"));
143        }
144
145        [PlaywrightTest("page-select-option.spec.ts", "should respect event bubbling")]
146        public async Task ShouldRespectEventBubbling()
147        {
148            await Page.GotoAsync(Server.Prefix + "/input/select.html");
149            await Page.SelectOptionAsync("select", "blue");
150            Assert.AreEqual(new[] { "blue" }, await Page.EvaluateAsync<string[]>("() => result.onBubblingInput"));
151            Assert.AreEqual(new[] { "blue" }, await Page.EvaluateAsync<string[]>("() => result.onBubblingChange"));
152        }
153
154        [PlaywrightTest("page-select-option.spec.ts", "should throw when element is not a &lt;select&gt;")]
155        public async Task ShouldThrowWhenElementIsNotASelect()
156        {
157            await Page.GotoAsync(Server.Prefix + "/input/select.html");
158            var exception = await PlaywrightAssert.ThrowsAsync<PlaywrightException>(() => Page.SelectOptionAsync("body", string.Empty));
159            StringAssert.Contains("Element is not a <select> element", exception.Message);
160        }
161
162        [PlaywrightTest("page-select-option.spec.ts", "should return [] on no matched values")]
163        public async Task ShouldReturnEmptyArrayOnNoMatchedValues()
164        {
165            await Page.GotoAsync(Server.Prefix + "/input/select.html");
166            var result = await Page.SelectOptionAsync("select", Array.Empty<string>());
167            Assert.IsEmpty(result);
168        }
169
170        [PlaywrightTest("page-select-option.spec.ts", "should return an array of matched values")]
171        public async Task ShouldReturnAnArrayOfMatchedValues()
172        {
173            await Page.GotoAsync(Server.Prefix + "/input/select.html");
174            await Page.EvaluateAsync<string>("() => makeMultiple()");
175            var result = await Page.SelectOptionAsync("select", new[] { "blue", "black", "magenta" });
176            Assert.AreEqual(new[] { "blue", "black", "magenta" }.OrderBy(v => v), result.OrderBy(v => v));
177        }
178
179        [PlaywrightTest("page-select-option.spec.ts", "should return an array of one element when multiple is not set")]
180        public async Task ShouldReturnAnArrayOfOneElementWhenMultipleIsNotSet()
181        {
182            await Page.GotoAsync(Server.Prefix + "/input/select.html");
183            var result = await Page.SelectOptionAsync("select", new[] { "42", "blue", "black", "magenta" });
184            Assert.That(result, Has.Count.EqualTo(1));
185        }
186
187        [PlaywrightTest("page-select-option.spec.ts", "should return [] on no values")]
188        public async Task ShouldReturnEmptyArrayOnNoValues()
189        {
190            await Page.GotoAsync(Server.Prefix + "/input/select.html");
191            var result = await Page.SelectOptionAsync("select", Array.Empty<string>());
192            Assert.IsEmpty(result);
193        }
194
195        [PlaywrightTest("page-select-option.spec.ts", "should unselect with null")]
196        public async Task ShouldUnselectWithNull()
197        {
198            await Page.GotoAsync(Server.Prefix + "/input/select.html");
199            await Page.EvaluateAsync("() => makeMultiple()");
200            var result = await Page.SelectOptionAsync("select", new[] { "blue", "black", "magenta" });
201            Assert.True(result.All(r => new[] { "blue", "black", "magenta" }.Contains(r)));
202            await Page.SelectOptionAsync("select", new string[] { });
203            Assert.True(await Page.EvalOnSelectorAsync<bool?>("select", "select => Array.from(select.options).every(option => !option.selected)"));
204        }
205
206        [PlaywrightTest("page-select-option.spec.ts", "should deselect all options when passed no values for a multiple select")]
207        public async Task ShouldDeselectAllOptionsWhenPassedNoValuesForAMultipleSelect()
208        {
209            await Page.GotoAsync(Server.Prefix + "/input/select.html");
210            await Page.EvaluateAsync("() => makeMultiple()");
211            await Page.SelectOptionAsync("select", new[] { "blue", "black", "magenta" });
212            await Page.SelectOptionAsync("select", new string[] { });
213            Assert.True(await Page.EvalOnSelectorAsync<bool>("select", "select => Array.from(select.options).every(option => !option.selected)"));
214        }
215
216        [PlaywrightTest("page-select-option.spec.ts", "should deselect all options when passed no values for a select without multiple")]
217        public async Task ShouldDeselectAllOptionsWhenPassedNoValuesForASelectWithoutMultiple()
218        {
219            await Page.GotoAsync(Server.Prefix + "/input/select.html");
220            await Page.SelectOptionAsync("select", new[] { "blue", "black", "magenta" });
221            await Page.SelectOptionAsync("select", Array.Empty<string>());
222            Assert.True(await Page.EvalOnSelectorAsync<bool>("select", "select => Array.from(select.options).every(option => !option.selected)"));
223        }
224
225        [PlaywrightTest("page-select-option.spec.ts", "should work when re-defining top-level Event class")]
226        public async Task ShouldWorkWhenReDefiningTopLevelEventClass()
227        {
228            await Page.GotoAsync(Server.Prefix + "/input/select.html");
229            await Page.EvaluateAsync("() => window.Event = null");
230            await Page.SelectOptionAsync("select", "blue");
231            Assert.AreEqual(new[] { "blue" }, await Page.EvaluateAsync<string[]>("() => result.onInput"));
232            Assert.AreEqual(new[] { "blue" }, await Page.EvaluateAsync<string[]>("() => result.onChange"));
233        }
234    }
235}
236
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)