How to use ShouldHighlightLocator method of Microsoft.Playwright.Tests.Locator.LocatorMiscTests class

Best Playwright-dotnet code snippet using Microsoft.Playwright.Tests.Locator.LocatorMiscTests.ShouldHighlightLocator

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

LocatorMiscTests.cs

Source: LocatorMiscTests.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 */
24using System.Threading.Tasks;
25using Microsoft.Playwright.NUnit;
26using NUnit.Framework;
27
28namespace Microsoft.Playwright.Tests.Locator
29{
30    public class LocatorMiscTests : PageTestEx
31    {
32        [PlaywrightTest("locator-misc-1.spec.ts", "should hover")]
33        public async Task ShouldHover()
34        {
35            await Page.GotoAsync(Server.Prefix + "/input/scrollable.html");
36            var button = Page.Locator("#button-6");
37            await button.HoverAsync();
38            Assert.AreEqual("button-6", await Page.EvaluateAsync<string>("() => document.querySelector('button:hover').id"));
39        }
40
41        [PlaywrightTest("locator-misc-1.spec.ts", "should hover when Node is removed")]
42        public async Task ShouldHoverWhenNodeIsRemoved()
43        {
44            await Page.GotoAsync(Server.Prefix + "/input/scrollable.html");
45            await Page.EvaluateAsync("() => delete window['Node']");
46            var button = Page.Locator("#button-6");
47            await button.HoverAsync();
48            Assert.AreEqual("button-6", await Page.EvaluateAsync<string>("() => document.querySelector('button:hover').id"));
49        }
50
51        [PlaywrightTest("locator-misc-1.spec.ts", "should fill input")]
52        public async Task ShouldFillInput()
53        {
54            await Page.GotoAsync(Server.Prefix + "/input/textarea.html");
55            var handle = Page.Locator("input");
56            await Page.FillAsync("input", "some value");
57            //await handle.FillAsync("some value");
58            Assert.AreEqual("some value", await Page.EvaluateAsync<string>("() => window['result']"));
59        }
60
61        [PlaywrightTest("locator-misc-1.spec.ts", "should fill input when Node is removed")]
62        public async Task ShouldFillInputWhenNodeIsRemoved()
63        {
64            await Page.GotoAsync(Server.Prefix + "/input/textarea.html");
65            await Page.EvaluateAsync("() => delete window['Node']");
66            var handle = Page.Locator("input");
67            await handle.FillAsync("some value");
68            Assert.AreEqual("some value", await Page.EvaluateAsync<string>("() => window['result']"));
69        }
70
71        [PlaywrightTest("locator-misc-1.spec.ts", "should check the box")]
72        public async Task ShouldCheckTheBox()
73        {
74            await Page.SetContentAsync("<input id='checkbox' type='checkbox'></input>");
75            var input = Page.Locator("input");
76            await input.CheckAsync();
77            Assert.IsTrue(await Page.EvaluateAsync<bool>("checkbox.checked"));
78        }
79
80        [PlaywrightTest("locator-misc-1.spec.ts", "should uncheck the box")]
81        public async Task ShouldUncheckTheBox()
82        {
83            await Page.SetContentAsync("<input id='checkbox' type='checkbox' checked></input>");
84            var input = Page.Locator("input");
85            await input.UncheckAsync();
86            Assert.IsFalse(await Page.EvaluateAsync<bool>("checkbox.checked"));
87        }
88
89        [PlaywrightTest("locator-misc-1.spec.ts", "should check the box using setChecked")]
90        public async Task ShouldCheckTheBoxUsingSetChecked()
91        {
92            await Page.SetContentAsync("<input id='checkbox' type='checkbox'></input>");
93            var input = Page.Locator("input");
94            await input.SetCheckedAsync(true);
95            Assert.IsTrue(await Page.EvaluateAsync<bool>("checkbox.checked"));
96            await input.SetCheckedAsync(false);
97            Assert.IsFalse(await Page.EvaluateAsync<bool>("checkbox.checked"));
98        }
99
100        [PlaywrightTest("locator-misc-1.spec.ts", "should select single option")]
101        public async Task ShouldSelectSingleOption()
102        {
103            await Page.GotoAsync(Server.Prefix + "/input/select.html");
104            var select = Page.Locator("select");
105            await select.SelectOptionAsync("blue");
106            CollectionAssert.AreEqual(new string[] { "blue" }, await Page.EvaluateAsync<string[]>("() => window['result'].onInput"));
107            CollectionAssert.AreEqual(new string[] { "blue" }, await Page.EvaluateAsync<string[]>("() => window['result'].onChange"));
108        }
109
110        [PlaywrightTest("locator-misc-1.spec.ts", "should focus a button")]
111        public async Task ShouldFocusAButton()
112        {
113            await Page.GotoAsync(Server.Prefix + "/input/button.html");
114            var button = Page.Locator("button");
115            Assert.IsFalse(await button.EvaluateAsync<bool>("button => document.activeElement === button"));
116            await button.FocusAsync();
117            Assert.IsTrue(await button.EvaluateAsync<bool>("button => document.activeElement === button"));
118        }
119
120        [PlaywrightTest("locator-misc-1.spec.ts", "should dispatch click event via ElementHandles")]
121        public async Task ShouldDispatchClickEventViaElementhandles()
122        {
123            await Page.GotoAsync(Server.Prefix + "/input/button.html");
124            var button = Page.Locator("button");
125            await button.DispatchEventAsync("click");
126            Assert.AreEqual("Clicked", await Page.EvaluateAsync<string>("() => window['result']"));
127        }
128
129        [PlaywrightTest("locator-misc-1.spec.ts", "should upload the file")]
130        public async Task ShouldUploadTheFile()
131        {
132            await Page.GotoAsync(Server.Prefix + "/input/fileupload.html");
133            var input = Page.Locator("input[type=file]");
134            await input.SetInputFilesAsync(TestUtils.GetAsset("file-to-upload.txt"));
135            Assert.AreEqual("file-to-upload.txt", await Page.EvaluateAsync<string>("e => e.files[0].name", await input.ElementHandleAsync()));
136        }
137
138        [PlaywrightTest("locator-misc-2.spec.ts", "should press")]
139        public async Task ShouldPress()
140        {
141            await Page.SetContentAsync("<input type='text' />");
142            await Page.Locator("input").PressAsync("h");
143            Assert.AreEqual("h", await Page.EvaluateAsync<string>("input => input.value", await Page.QuerySelectorAsync("input")));
144        }
145
146        [PlaywrightTest("locator-misc-2.spec.ts", "should scroll into view")]
147        public async Task ShouldScrollIntoView()
148        {
149            await Page.GotoAsync(Server.Prefix + "/offscreenbuttons.html");
150
151            for (int i = 0; i < 11; ++i)
152            {
153                var button = Page.Locator($"#btn{i}");
154                var before = await button.EvaluateAsync<int>("button => { return button.getBoundingClientRect().right - window.innerWidth; }");
155                Assert.AreEqual(10 * i, before);
156
157                await button.ScrollIntoViewIfNeededAsync();
158
159                var after = await button.EvaluateAsync<int>("button => { return button.getBoundingClientRect().right - window.innerWidth; }");
160                Assert.IsTrue(after <= 0);
161                await Page.EvaluateAsync("() => window.scrollTo(0, 0)");
162            }
163        }
164
165        [PlaywrightTest("locator-misc-2.spec.ts", "should select textarea")]
166        public async Task ShouldSelectTextarea()
167        {
168            await Page.GotoAsync(Server.Prefix + "/input/textarea.html");
169
170            var textarea = Page.Locator("textarea");
171            await textarea.EvaluateAsync<string>("textarea => textarea.value = 'some value'");
172
173            await textarea.SelectTextAsync();
174            if (TestConstants.IsFirefox)
175            {
176                Assert.AreEqual(0, await textarea.EvaluateAsync<int>("el => el.selectionStart"));
177                Assert.AreEqual(10, await textarea.EvaluateAsync<int>("el => el.selectionEnd"));
178            }
179            else
180            {
181                Assert.AreEqual("some value", await textarea.EvaluateAsync<string>("() => window.getSelection().toString()"));
182            }
183        }
184
185        [PlaywrightTest("locator-misc-2.spec.ts", "should type")]
186        public async Task ShouldType()
187        {
188            await Page.SetContentAsync("<input type='text' />");
189            await Page.Locator("input").TypeAsync("hello");
190            Assert.AreEqual("hello", await Page.EvaluateAsync<string>("input => input.value", await Page.QuerySelectorAsync("input")));
191        }
192
193        [PlaywrightTest("locator-misc-2.spec.ts", "should take screenshot")]
194        public async Task ShouldTakeScreenshot()
195        {
196            await Page.SetViewportSizeAsync(500, 500);
197            await Page.GotoAsync(Server.Prefix + "/grid.html");
198
199            await Page.EvaluateAsync("() => window.scrollBy(50, 100)");
200            var element = Page.Locator(".box:nth-of-type(3)");
201            await element.ScreenshotAsync();
202        }
203
204        [PlaywrightTest("locator-misc-2.spec.ts", "should take screenshot with mask")]
205        public async Task ShouldTakeScreenshotWithMaskOption()
206        {
207            await Page.SetViewportSizeAsync(500, 500);
208            await Page.GotoAsync(Server.Prefix + "/grid.html");
209
210            await Page.EvaluateAsync("() => window.scrollBy(50, 100)");
211            var element = Page.Locator("body");
212            await element.ScreenshotAsync(new()
213            {
214                Mask = new ILocator[] { Page.Locator(".box").Nth(3) },
215            });
216        }
217
218        [PlaywrightTest("locator-misc-2.spec.ts", "should return bounding box")]
219        [Skip(SkipAttribute.Targets.Firefox)]
220        public async Task ShouldReturnBoundingBox()
221        {
222            await Page.SetViewportSizeAsync(500, 500);
223            await Page.GotoAsync(Server.Prefix + "/grid.html");
224
225            var element = Page.Locator(".box:nth-of-type(13)");
226            var box = await element.BoundingBoxAsync();
227
228            Assert.AreEqual(100, box.X);
229            Assert.AreEqual(50, box.Y);
230            Assert.AreEqual(50, box.Width);
231            Assert.AreEqual(50, box.Height);
232        }
233
234        [PlaywrightTest("locator-misc-2.spec.ts", "should waitFor")]
235        public async Task ShouldWaitFor()
236        {
237            await Page.SetContentAsync("<div></div>");
238            var locator = Page.Locator("span");
239            var task = locator.WaitForAsync();
240            await Page.EvalOnSelectorAsync("div", "div => div.innerHTML = '<span>target</span>'");
241            await task;
242            Assert.AreEqual("target", await locator.TextContentAsync());
243        }
244
245        [PlaywrightTest("locator-misc-2.spec.ts", "should waitFor hidden")]
246        public async Task ShouldWaitForHidden()
247        {
248            await Page.SetContentAsync("<div><span></span></div>");
249            var locator = Page.Locator("span");
250            var task = locator.WaitForAsync(new() { State = WaitForSelectorState.Hidden });
251            await Page.EvalOnSelectorAsync("div", "div => div.innerHTML = ''");
252            await task;
253        }
254
255        [PlaywrightTest("locator-highlight.spec.ts", "should highlight locator")]
256        public async Task ShouldHighlightLocator()
257        {
258            await Page.GotoAsync(Server.Prefix + "/grid.html");
259            await Page.Locator(".box").Nth(3).HighlightAsync();
260            Assert.AreEqual(await Page.Locator("x-pw-glass").IsVisibleAsync(), true);
261        }
262    }
263}
264
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)