How to use PageQuerySelectorOptions class of Microsoft.Playwright package

Best Playwright-dotnet code snippet using Microsoft.Playwright.PageQuerySelectorOptions

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

PageAssertions.cs

Source: PageAssertions.cs Github

copy
1/*
2 * MIT License
3 *
4 * Copyright (c) Evgeny Nazarchuk.
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 Microsoft.Playwright;
26using Playwright.Synchronous;
27
28namespace Playwright.FluentAssertions;
29
30public static class PageAssertions
31{
32    public static ReferenceTypeAssertion<IPage> Should(this IPage page)
33    {
34        return new ReferenceTypeAssertion<IPage>(page);
35    }
36
37    private static IElementHandle GetElement(IPage page,
38        string selector,
39        PageWaitForSelectorOptions? waitOptions = null,
40        PageQuerySelectorOptions? queryOptions = null)
41    {
42        page.WaitForSelector(selector, waitOptions);
43        var element = page.QuerySelector(selector, queryOptions)!;
44        return element!;
45    }
46
47    public static IPage HaveTitle(
48        this ReferenceTypeAssertion<IPage> page,
49        string expected,
50        string because = "no reason given")
51    {
52        var actual = page.Value.Title();
53
54        if (string.Compare(actual, expected) != 0)
55        {
56            throw new AssertException(@$"
57HaveTitle Assert Exception
58Expected:
59{expected}
60Actual:
61{actual}
62Because: {because}
63");
64        }
65
66        return page.Value;
67    }
68
69    public static IPage HaveNotTitle(
70        this ReferenceTypeAssertion<IPage> page,
71        string notExpected,
72        string because = "no reason given")
73    {
74        var actual = page.Value.Title();
75
76        if (string.Compare(actual, notExpected) == 0)
77        {
78            throw new AssertException(@$"
79HaveNotTitle Assert Exception
80Not expected:
81{notExpected}
82Actual:
83{actual}
84Because:
85{because}
86");
87        }
88
89        return page.Value;
90    }
91
92    public static IPage HaveContent(
93        this ReferenceTypeAssertion<IPage> page,
94        string expected,
95        string because = "no reason given")
96    {
97        var actual = page.Value.Content();
98
99        if (string.Compare(actual, expected) != 0)
100        {
101            throw new AssertException(@$"
102HaveContent Assert Exception
103Not expected:
104{expected}
105Actual:
106{actual}
107Because:
108{because}
109");
110        }
111
112        return page.Value;
113    }
114
115    public static IPage HaveNotContent(
116        this ReferenceTypeAssertion<IPage> page,
117        string notExpected,
118        string because = "no reason given")
119    {
120        var actual = page.Value.Content();
121
122        if (string.Compare(actual, notExpected) == 0)
123        {
124            throw new AssertException(@$"
125HaveNotContent Assert Exception
126Not expected:
127{notExpected}
128Actual:
129{actual}
130Because:
131{because}
132");
133        }
134
135        return page.Value;
136    }
137
138    public static IPage HaveCheckedElement(
139        this ReferenceTypeAssertion<IPage> page,
140        string selector,
141        string because = "no reason given",
142        PageWaitForSelectorOptions? waitOptions = null,
143        PageQuerySelectorOptions? queryOptions = null)
144    {
145        var element = GetElement(page.Value, selector, waitOptions, queryOptions);
146        var isChecked = element.IsChecked();
147
148        if (isChecked is false)
149        {
150            throw new AssertException(@$"
151HaveElementChecked Assert Exception
152Selector: {selector}
153Expected: checked
154Actual: not checked
155Because: {because}
156");
157        }
158
159        return page.Value;
160    }
161
162    public static IPage HaveNotCheckedElement(
163        this ReferenceTypeAssertion<IPage> page,
164        string selector,
165        string because = "no reason given",
166        PageWaitForSelectorOptions? waitOptions = null,
167        PageQuerySelectorOptions? queryOptions = null)
168    {
169        var element = GetElement(page.Value, selector, waitOptions, queryOptions);
170        var isChecked = element.IsChecked();
171
172        if (isChecked is true)
173        {
174            throw new AssertException(@$"
175HaveNotElementChecked Assert Exception
176Selector: {selector}
177Expected: not checked
178Actual: checked
179Because: {because}
180");
181        }
182
183        return page.Value;
184    }
185
186    public static IPage HaveDisabledElement(
187        this ReferenceTypeAssertion<IPage> page,
188        string selector,
189        string because = "no reason given",
190        PageWaitForSelectorOptions? waitOptions = null,
191        PageQuerySelectorOptions? queryOptions = null)
192    {
193        var element = GetElement(page.Value, selector, waitOptions, queryOptions);
194        var isDisabled = element.IsDisabled();
195
196        if (isDisabled is false)
197        {
198            throw new AssertException(@$"
199HaveNotElementChecked Assert Exception
200Selector: {selector}
201Expected: disable
202Actual: not disable
203Because: {because}
204");
205        }
206
207        return page.Value;
208    }
209
210    public static IPage HaveNotDisabledElement(
211        this ReferenceTypeAssertion<IPage> page,
212        string selector,
213        string because = "no reason given",
214        PageWaitForSelectorOptions? waitOptions = null,
215        PageQuerySelectorOptions? queryOptions = null)
216    {
217        var element = GetElement(page.Value, selector, waitOptions, queryOptions);
218        var isDisabled = element.IsDisabled();
219
220        if (isDisabled is true)
221        {
222            throw new AssertException(@$"
223HaveNotElementDisabled Assert Exception
224Selector: {selector}
225Expected: not disable
226Actual: disable
227Because: {because}
228");
229        }
230
231        return page.Value;
232    }
233
234    public static IPage HaveEditableElement(
235        this ReferenceTypeAssertion<IPage> page,
236        string selector,
237        string because = "no reason given",
238        PageWaitForSelectorOptions? waitOptions = null,
239        PageQuerySelectorOptions? queryOptions = null)
240    {
241        var element = GetElement(page.Value, selector, waitOptions, queryOptions);
242        var isEditable = element.IsEditable();
243
244        if (isEditable is false)
245        {
246            throw new AssertException(@$"
247HaveElementEditable Assert Exception
248Selector: {selector}
249Expected: editable
250Actual: not editable
251Because: {because}
252");
253        }
254
255        return page.Value;
256    }
257
258    public static IPage HaveNotEditableElement(
259        this ReferenceTypeAssertion<IPage> page,
260        string selector,
261        string because = "no reason given",
262        PageWaitForSelectorOptions? waitOptions = null,
263        PageQuerySelectorOptions? queryOptions = null)
264    {
265        var element = GetElement(page.Value, selector, waitOptions, queryOptions);
266        var isEditable = element.IsEditable();
267
268        if (isEditable is true)
269        {
270            throw new AssertException(@$"
271HaveElementEditable Assert Exception
272Selector: {selector}
273Expected: not editable
274Actual: editable
275Because: {because}
276");
277        }
278
279        return page.Value;
280    }
281
282    public static IPage HaveEnabledElement(
283        this ReferenceTypeAssertion<IPage> page,
284        string selector,
285        string because = "no reason given",
286        PageWaitForSelectorOptions? waitOptions = null,
287        PageQuerySelectorOptions? queryOptions = null)
288    {
289        var element = GetElement(page.Value, selector, waitOptions, queryOptions);
290        var isEnabled = element.IsEnabled();
291
292        if (isEnabled is false)
293        {
294            throw new AssertException(@$"
295HaveElementEnabled Assert Exception
296Selector: {selector}
297Expected: enable
298Actual: not enable
299Because: {because}
300");
301        }
302
303        return page.Value;
304    }
305
306    public static IPage HaveNotEnabledElement(
307        this ReferenceTypeAssertion<IPage> page,
308        string selector,
309        string because = "no reason given",
310        PageWaitForSelectorOptions? waitOptions = null,
311        PageQuerySelectorOptions? queryOptions = null)
312    {
313        var element = GetElement(page.Value, selector, waitOptions, queryOptions);
314        var isEnabled = element.IsEnabled();
315
316        if (isEnabled is true)
317        {
318            throw new AssertException(@$"
319HaveNotElementEnabled Assert Exception
320Selector: {selector}
321Expected: not enable
322Actual: enable
323Because: {because}
324");
325        }
326
327        return page.Value;
328    }
329
330    public static IPage HaveHiddenElement(
331        this ReferenceTypeAssertion<IPage> page,
332        string selector,
333        string because = "no reason given",
334        PageWaitForSelectorOptions? waitOptions = null,
335        PageQuerySelectorOptions? queryOptions = null)
336    {
337        var element = GetElement(page.Value, selector, waitOptions, queryOptions);
338        var isHidden = element.IsHidden();
339
340        if (isHidden is false)
341        {
342            throw new AssertException(@$"
343HaveElementHidden Assert Exception
344Selector: {selector}
345Expected: hidden
346Actual: not hidden
347Because: {because}
348");
349        }
350
351        return page.Value;
352    }
353
354    public static IPage HaveNotHiddenElement(
355        this ReferenceTypeAssertion<IPage> page,
356        string selector,
357        string because = "no reason given",
358        PageWaitForSelectorOptions? waitOptions = null,
359        PageQuerySelectorOptions? queryOptions = null)
360    {
361        var element = GetElement(page.Value, selector, waitOptions, queryOptions);
362        var isHidden = element.IsHidden();
363
364        if (isHidden is true)
365        {
366            throw new AssertException(@$"
367HaveNotElementHidden Assert Exception
368Selector: {selector}
369Expected: not hidden
370Actual: hidden
371Because: {because}
372");
373        }
374
375        return page.Value;
376    }
377
378    public static IPage HaveVisibleElement(
379        this ReferenceTypeAssertion<IPage> page,
380        string selector,
381        string because = "no reason given",
382        PageWaitForSelectorOptions? waitOptions = null,
383        PageQuerySelectorOptions? queryOptions = null)
384    {
385        var element = GetElement(page.Value, selector, waitOptions, queryOptions);
386        var isVisible = element.IsVisible();
387
388        if (isVisible is false)
389        {
390            throw new AssertException(@$"
391HaveElementVisible Assert Exception
392Selector: {selector}
393Expected: visible
394Actual: not visible
395Because: {because}
396");
397        }
398
399        return page.Value;
400    }
401
402    public static IPage HaveNotVisibleElement(
403        this ReferenceTypeAssertion<IPage> page,
404        string selector,
405        string because = "no reason given",
406        PageWaitForSelectorOptions? waitOptions = null,
407        PageQuerySelectorOptions? queryOptions = null)
408    {
409        var element = GetElement(page.Value, selector, waitOptions, queryOptions);
410        var isVisible = element.IsVisible();
411
412        if (isVisible is true)
413        {
414            throw new AssertException(@$"
415HaveElementVisible Assert Exception
416Selector: {selector}
417Expected: not visible
418Actual: visible
419Because: {because}
420");
421        }
422
423        return page.Value;
424    }
425
426    public static IPage HaveElementTextContent(
427        this ReferenceTypeAssertion<IPage> page,
428        string selector,
429        string expected,
430        string because = "no reason given",
431        PageWaitForSelectorOptions? waitOptions = null,
432        PageQuerySelectorOptions? queryOptions = null)
433    {
434        var element = GetElement(page.Value, selector, waitOptions, queryOptions);
435        var actual = element.TextContent() ?? "";
436
437        if (string.Compare(actual, expected) != 0)
438        {
439            throw new AssertException(@$"
440HaveElementTextContent Assert Exception
441Selector: {selector}
442Expected:
443{expected}
444Actual:
445{actual}
446Because:
447{because}
448");
449        }
450
451        return page.Value;
452    }
453
454    public static IPage HaveNotElementTextContent(
455        this ReferenceTypeAssertion<IPage> page,
456        string selector,
457        string notExpected,
458        string because = "no reason given",
459        PageWaitForSelectorOptions? waitOptions = null,
460        PageQuerySelectorOptions? queryOptions = null)
461    {
462        var element = GetElement(page.Value, selector, waitOptions, queryOptions);
463        var actual = element.TextContent() ?? "";
464
465        if (string.Compare(actual, notExpected) == 0)
466        {
467            throw new AssertException(@$"
468HaveNotElementTextContent Assert Exception
469Selector: {selector}
470Not expected:
471{notExpected}
472Actual:
473{actual}
474Because:
475{because}
476");
477        }
478
479        return page.Value;
480    }
481
482    public static IPage HaveElementInnerHTML(
483        this ReferenceTypeAssertion<IPage> page,
484        string selector,
485        string expected,
486        string because = "no reason given",
487        PageWaitForSelectorOptions? waitOptions = null,
488        PageQuerySelectorOptions? queryOptions = null)
489    {
490        var element = GetElement(page.Value, selector, waitOptions, queryOptions);
491        var actual = element.InnerHTML();
492
493        if (string.Compare(actual, expected) != 0)
494        {
495            throw new AssertException(@$"
496HaveElementInnerHTML Assert Exception
497Selector: {selector}
498Expected:
499{expected}
500Actual:
501{actual}
502Because:
503{because}
504");
505        }
506
507        return page.Value;
508    }
509
510    public static IPage HaveNotElementInnerHTML(
511        this ReferenceTypeAssertion<IPage> page,
512        string selector,
513        string notExpected,
514        string because = "no reason given",
515        PageWaitForSelectorOptions? waitOptions = null,
516        PageQuerySelectorOptions? queryOptions = null)
517    {
518        var element = GetElement(page.Value, selector, waitOptions, queryOptions);
519        var actual = element.InnerHTML();
520
521        if (string.Compare(actual, notExpected) == 0)
522        {
523            throw new AssertException(@$"
524HaveNotElementInnerHTML Assert Exception
525Selector: {selector}
526Not expected:
527{notExpected}
528Actual:
529{actual}
530Because:
531{because}
532");
533        }
534
535        return page.Value;
536    }
537
538    public static IPage HaveElementInnerText(
539        this ReferenceTypeAssertion<IPage> page,
540        string selector,
541        string expected,
542        string because = "no reason given",
543        PageWaitForSelectorOptions? waitOptions = null,
544        PageQuerySelectorOptions? queryOptions = null)
545    {
546        var element = GetElement(page.Value, selector, waitOptions, queryOptions);
547        var actual = element.InnerText();
548
549        if (string.Compare(actual, expected) != 0)
550        {
551            throw new AssertException(@$"
552HaveElementInnerText Assert Exception
553Selector: {selector}
554Expected:
555{expected}
556Actual:
557{actual}
558Because:
559{because}
560");
561        }
562
563        return page.Value;
564    }
565
566    public static IPage HaveNotElementInnerText(
567        this ReferenceTypeAssertion<IPage> page,
568        string selector,
569        string notExpected,
570        string because = "no reason given",
571        PageWaitForSelectorOptions? waitOptions = null,
572        PageQuerySelectorOptions? queryOptions = null)
573    {
574        var element = GetElement(page.Value, selector, waitOptions, queryOptions);
575        var actual = element.InnerText();
576
577        if (string.Compare(actual, notExpected) == 0)
578        {
579            throw new AssertException(@$"
580HaveNotElementInnerText Assert Exception
581Selector: {selector}
582Not expected:
583{notExpected}
584Actual:
585{actual}
586Because:
587{because}
588");
589        }
590
591        return page.Value;
592    }
593
594    public static IPage HaveElementInputValue(
595        this ReferenceTypeAssertion<IPage> page,
596        string selector,
597        string expected,
598        string because = "no reason given",
599        PageWaitForSelectorOptions? waitOptions = null,
600        PageQuerySelectorOptions? queryOptions = null,
601        ElementHandleInputValueOptions? inputOptions = null)
602    {
603        var element = GetElement(page.Value, selector, waitOptions, queryOptions);
604        var actual = element.InputValue(inputOptions);
605
606        if (string.Compare(actual, expected) != 0)
607        {
608            throw new AssertException(@$"
609HaveElementInputValue Assert Exception
610Selector: {selector}
611Expected:
612{expected}
613Actual:
614{actual}
615Because:
616{because}
617");
618        }
619
620        return page.Value;
621    }
622
623    public static IPage HaveNotElementInputValue(
624        this ReferenceTypeAssertion<IPage> page,
625        string selector,
626        string notExpected,
627        string because = "no reason given",
628        PageWaitForSelectorOptions? waitOptions = null,
629        PageQuerySelectorOptions? queryOptions = null,
630        ElementHandleInputValueOptions? inputOptions = null)
631    {
632        var element = GetElement(page.Value, selector, waitOptions, queryOptions);
633        var actual = element.InputValue(inputOptions);
634
635        if (string.Compare(actual, notExpected) == 0)
636        {
637            throw new AssertException(@$"
638HaveNotElementInputValue Assert Exception
639Selector: {selector}
640Not expected:
641{notExpected}
642Actual:
643{actual}
644Because:
645{because}
646");
647        }
648
649        return page.Value;
650    }
651
652    public static IPage HaveElementAttribute(
653        this ReferenceTypeAssertion<IPage> page,
654        string selector,
655        string attributeName,
656        string because = "no reason given",
657        PageWaitForSelectorOptions? waitOptions = null,
658        PageQuerySelectorOptions? queryOptions = null)
659    {
660        var element = GetElement(page.Value, selector, waitOptions, queryOptions);
661        var value = element.GetAttribute(attributeName);
662
663        if (value is null)
664        {
665            throw new AssertException(@$"
666HaveElementAttribute Assert Exception
667Selector: {selector}
668Expected attribute: {attributeName}
669Because: {because}
670");
671        }
672
673        return page.Value;
674    }
675
676    public static IPage HaveNotElementAttribute(
677        this ReferenceTypeAssertion<IPage> page,
678        string selector,
679        string attributeName,
680        string because = "no reason given",
681        PageWaitForSelectorOptions? waitOptions = null,
682        PageQuerySelectorOptions? queryOptions = null)
683    {
684        var element = GetElement(page.Value, selector, waitOptions, queryOptions);
685        var value = element.GetAttribute(attributeName);
686
687        if (value is not null)
688        {
689            throw new AssertException(@$"
690HaveNotElementAttribute Assert Exception
691Selector: {selector}
692Not expected attribute: {attributeName}
693Because: {because}
694");
695        }
696
697        return page.Value;
698    }
699
700    public static IPage HaveElementAttributeValue(
701        this ReferenceTypeAssertion<IPage> page,
702        string selector,
703        string attributeName,
704        string expectedAttributeValue,
705        string because = "no reason given",
706        PageWaitForSelectorOptions? waitOptions = null,
707        PageQuerySelectorOptions? queryOptions = null)
708    {
709        var element = GetElement(page.Value, selector, waitOptions, queryOptions);
710        var value = element.GetAttribute(attributeName);
711
712        if (value is null)
713        {
714            throw new AssertException($"Attribute not found. Attibute name: {attributeName}");
715        }
716
717        if (string.Compare(value, expectedAttributeValue) != 0)
718        {
719            throw new AssertException(@$"
720HaveElementAttributeValue Assert Exception
721Selector: {selector}
722Expected attribute: {attributeName}
723Expected attribute value: {expectedAttributeValue}
724Actual attribute value: {value}
725Because: {because}
726");
727        }
728
729        return page.Value;
730    }
731
732    public static IPage HaveElementNotAttributeValue(
733        this ReferenceTypeAssertion<IPage> page,
734        string selector,
735        string attributeName,
736        string expectedAttributeValue,
737        string because = "no reason given",
738        PageWaitForSelectorOptions? waitOptions = null,
739        PageQuerySelectorOptions? queryOptions = null)
740    {
741        var element = GetElement(page.Value, selector, waitOptions, queryOptions);
742        var value = element.GetAttribute(attributeName);
743
744        if (value is null)
745        {
746            throw new AssertException($"Attribute not found. Attibute name: {attributeName}");
747        }
748
749        if (string.Compare(value, expectedAttributeValue) == 0)
750        {
751            throw new AssertException(@$"
752HaveElementNotAttributeValue Assert Exception
753Selector: {selector}
754Expected attribute: {attributeName}
755Not expected attribute value: {expectedAttributeValue}
756Actual attribute value: {value}
757Because: {because}
758");
759        }
760
761        return page.Value;
762    }
763
764    public static IPage HaveElementComputedStyle(
765        this ReferenceTypeAssertion<IPage> page,
766        string selector,
767        string styleName,
768        string expectedStyleValue,
769        string because = "no reason given",
770        PageWaitForSelectorOptions? waitOptions = null,
771        PageQuerySelectorOptions? queryOptions = null)
772    {
773        var element = GetElement(page.Value, selector, waitOptions, queryOptions);
774        var value = element.Evaluate($"e => getComputedStyle(e).{styleName}", element).ToString();
775
776        if (value == "")
777        {
778            throw new AssertException($"Style not found. Style name: {styleName}");
779        }
780
781        if (string.Compare(value, expectedStyleValue) != 0)
782        {
783            throw new AssertException([email protected]"
784HaveElementComputedStyle Assert Exception
785Selector: {selector}
786Style name: {styleName}
787Expected style value: {expectedStyleValue}
788Actual style value: {value}
789Because: {because}
790");
791        }
792
793        return page.Value;
794    }
795}
796
Full Screen

PlaywrightHook.cs

Source: PlaywrightHook.cs Github

copy
1using BrowserHook;
2using Microsoft.Playwright;
3using System;
4using System.Text.RegularExpressions;
5using System.Threading.Tasks;
6
7namespace PlaywrightHook
8{
9    public class PlaywrightHook : IBrowserHook, IAsyncDisposable
10    {
11        IPlaywright _playwright;
12        IBrowserContext _browser;
13        IPage _page;
14        public async Task Initialize()
15        {
16            _playwright = await Playwright.CreateAsync();
17            _browser = await _playwright.Chromium.LaunchPersistentContextAsync("browserContext", new BrowserTypeLaunchPersistentContextOptions {  Headless = false});
18            _page = await _browser.NewPageAsync();
19        }
20
21        public async Task NavigateTo(string url) => 
22            await _page.GotoAsync(url);
23
24        public async Task WaitForElement(string xpath, bool continueOnTimeout = false, int timeoutMilliseconds = 30000)
25        {
26            try
27            {
28                await _page.WaitForSelectorAsync(xpath, new PageWaitForSelectorOptions { Timeout = timeoutMilliseconds });
29            }
30            catch (System.TimeoutException) {
31                if (!continueOnTimeout) throw;
32            }
33        }
34           
35        public async Task WaitForPage(string url) => 
36            await _page.WaitForURLAsync(url, new PageWaitForURLOptions { Timeout = 0, WaitUntil = WaitUntilState.NetworkIdle  });
37
38        public async Task WaitForPage(Regex regex) =>
39            await _page.WaitForURLAsync(regex, new PageWaitForURLOptions { Timeout = 0, WaitUntil = WaitUntilState.NetworkIdle });
40
41        public async Task<bool> IsElementPresent(string xpath) => 
42            (await _page.QuerySelectorAsync(xpath, new PageQuerySelectorOptions { })) != null;
43
44        public async Task<string> GetInnerText(string xpath) =>
45            await _page.InnerTextAsync(xpath, new PageInnerTextOptions {  });
46
47        public async ValueTask DisposeAsync()
48        {
49            await _page.CloseAsync();
50            await _browser.DisposeAsync();
51            _playwright.Dispose();
52        }
53
54        public async Task Click(string xpath) => 
55            await _page.ClickAsync(xpath, new PageClickOptions {  });
56
57        public async Task SetText(string xpath, string text)
58        {
59            await _page.TypeAsync(xpath, text, new PageTypeOptions { });
60        }
61
62        public async Task SendKey(string key, int count = 1)
63        {
64            for (var x = 0; x < count; x++)
65                await _page.Keyboard.PressAsync(key, new KeyboardPressOptions { });
66        }
67    }
68}
69
Full Screen

Menu.cs

Source: Menu.cs Github

copy
1using Playwright.PageObjectModel.Samples.BaseModels;
2using Playwright.PageObjectModel.Samples.Pages;
3using Playwright.Synchronous;
4using Microsoft.Playwright;
5
6namespace Playwright.PageObjectModel.Samples.Blocks;
7
8public class Menu<TPageModel> : UIBlock<TPageModel>
9    where TPageModel : PageModel
10{
11    public Menu(TPageModel pageModel, IElementHandle element) : base(pageModel, element)
12    {
13    }
14
15    public Menu(ElementModel<TPageModel> parentBlockModel, IElementHandle element) : base(parentBlockModel, element)
16    {
17    }
18
19    public Menu(ElementModel<TPageModel> parentBlockModel, string selector, ElementHandleWaitForSelectorOptions? waitOptions = null) : base(parentBlockModel, selector, waitOptions)
20    {
21    }
22
23    public Menu(TPageModel pageModel, string selector, PageWaitForSelectorOptions? waitOptions = null, PageQuerySelectorOptions? queryOptions = null) : base(pageModel, selector, waitOptions, queryOptions)
24    {
25    }
26
27    public TPageModel ToggleTheme()
28    {
29        Click(".toggleTrackThumb_xI_Z");
30        //WaitForLoadNetworkIdle();
31        return PageModel;
32    }
33
34    public DocsPage Docs()
35    {
36        Click("//a[text()='Docs']");
37        //WaitForLoadNetworkIdle();
38        return new DocsPage(this.Page);
39    }
40
41    public ApiPage API()
42    {
43        Click("//a[text()='API']");
44        //WaitForLoadNetworkIdle();
45        return new ApiPage(this.Page);
46    }
47}
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

Most used methods in PageQuerySelectorOptions

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)