How to use ShouldClickTheButton method of PuppeteerSharp.Tests.ClickTests.ClickTests class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Tests.ClickTests.ClickTests.ShouldClickTheButton

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

ClickTests.cs

Source: ClickTests.cs Github

copy
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using PuppeteerSharp.Input;
5using Xunit;
6using Xunit.Abstractions;
7
8namespace PuppeteerSharp.Tests.InputTests
9{
10    [Collection(TestConstants.TestFixtureCollectionName)]
11    public class ClickTests : PuppeteerPageBaseTest
12    {
13        public ClickTests(ITestOutputHelper output) : base(output)
14        {
15        }
16
17        [Fact]
18        public async Task ShouldClickTheButton()
19        {
20            await Page.GoToAsync(TestConstants.ServerUrl + "/input/button.html");
21            await Page.ClickAsync("button");
22            Assert.Equal("Clicked", await Page.EvaluateExpressionAsync<string>("result"));
23        }
24
25        [Fact]
26        public async Task ShouldClickSvg()
27        {
28            await Page.SetContentAsync([email protected]"
29                <svg height=""100"" width=""100"">
30                  <circle onclick=""javascript:window.__CLICKED=42"" cx=""50"" cy=""50"" r=""40"" stroke=""black"" stroke-width=""3"" fill=""red""/>
31                </svg>
32            ");
33            await Page.ClickAsync("circle");
34            Assert.Equal(42, await Page.EvaluateFunctionAsync<int>("() => window.__CLICKED"));
35        }
36
37        [Fact]
38        public async Task ShouldClickTheButtonIfWindowNodeIsRemoved()
39        {
40            await Page.GoToAsync(TestConstants.ServerUrl + "/input/button.html");
41            await Page.EvaluateExpressionAsync("delete window.Node");
42            await Page.ClickAsync("button");
43            Assert.Equal("Clicked", await Page.EvaluateExpressionAsync("result"));
44        }
45
46        [Fact(Skip = "See https://github.com/GoogleChrome/puppeteer/issues/4281")]
47        public async Task ShouldClickOnASpanWithAnInlineElementInside()
48        {
49            await Page.SetContentAsync([email protected]"
50                <style>
51                span::before {{
52                    content: 'q';
53                }}
54                </style>
55                <span onclick='javascript:window.CLICKED=42'></span>
56            ");
57            await Page.ClickAsync("span");
58            Assert.Equal(42, await Page.EvaluateFunctionAsync<int>("() => window.CLICKED"));
59        }
60
61        /// <summary>
62        /// This test is called ShouldNotThrowUnhandledPromiseRejectionWhenPageCloses in puppeteer.
63        /// </summary>
64        [Fact]
65        public async Task ShouldGracefullyFailWhenPageCloses()
66        {
67            var newPage = await Browser.NewPageAsync();
68            await Assert.ThrowsAsync<TargetClosedException>(() => Task.WhenAll(
69                newPage.CloseAsync(),
70                newPage.Mouse.ClickAsync(1, 2)
71             ));
72        }
73
74        [Fact]
75        public async Task ShouldClickTheButtonAfterNavigation()
76        {
77            await Page.GoToAsync(TestConstants.ServerUrl + "/input/button.html");
78            await Page.ClickAsync("button");
79            await Page.GoToAsync(TestConstants.ServerUrl + "/input/button.html");
80            await Page.ClickAsync("button");
81            Assert.Equal("Clicked", await Page.EvaluateExpressionAsync<string>("result"));
82        }
83
84        [Fact]
85        public async Task ShouldClickWithDisabledJavascript()
86        {
87            await Page.SetJavaScriptEnabledAsync(false);
88            await Page.GoToAsync(TestConstants.ServerUrl + "/wrappedlink.html");
89            await Task.WhenAll(
90                Page.ClickAsync("a"),
91                Page.WaitForNavigationAsync()
92            );
93            Assert.Equal(TestConstants.ServerUrl + "/wrappedlink.html#clicked", Page.Url);
94        }
95
96        [Fact]
97        public async Task ShouldClickWhenOneOfInlineBoxChildrenIsOutsideOfViewport()
98        {
99            await Page.SetContentAsync([email protected]"
100            <style>
101            i {{
102                position: absolute;
103                top: -1000px;
104            }}
105            </style>
106            <span onclick='javascript:window.CLICKED = 42;'><i>woof</i><b>doggo</b></span>
107            ");
108
109            await Page.ClickAsync("span");
110            Assert.Equal(42, await Page.EvaluateFunctionAsync<int>("() => window.CLICKED"));
111        }
112
113        [Fact]
114        public async Task ShouldSelectTheTextByTripleClicking()
115        {
116            await Page.GoToAsync(TestConstants.ServerUrl + "/input/textarea.html");
117            await Page.FocusAsync("textarea");
118            const string text = "This is the text that we are going to try to select. Let's see how it goes.";
119            await Page.Keyboard.TypeAsync(text);
120            await Page.ClickAsync("textarea");
121            await Page.ClickAsync("textarea", new ClickOptions { ClickCount = 2 });
122            await Page.ClickAsync("textarea", new ClickOptions { ClickCount = 3 });
123            Assert.Equal(text, await Page.EvaluateExpressionAsync<string>("window.getSelection().toString()"));
124        }
125
126        [Fact]
127        public async Task ShouldClickOffscreenButtons()
128        {
129            await Page.GoToAsync(TestConstants.ServerUrl + "/offscreenbuttons.html");
130            var messages = new List<string>();
131            Page.Console += (sender, e) => messages.Add(e.Message.Text);
132
133            for (var i = 0; i < 11; ++i)
134            {
135                // We might've scrolled to click a button - reset to (0, 0).
136                await Page.EvaluateFunctionAsync("() => window.scrollTo(0, 0)");
137                await Page.ClickAsync($"#btn{i}");
138            }
139            Assert.Equal(new List<string>
140            {
141                "button #0 clicked",
142                "button #1 clicked",
143                "button #2 clicked",
144                "button #3 clicked",
145                "button #4 clicked",
146                "button #5 clicked",
147                "button #6 clicked",
148                "button #7 clicked",
149                "button #8 clicked",
150                "button #9 clicked",
151                "button #10 clicked"
152            }, messages);
153        }
154
155        [Fact]
156        public async Task ShouldClickWrappedLinks()
157        {
158            await Page.GoToAsync(TestConstants.ServerUrl + "/wrappedlink.html");
159            await Page.ClickAsync("a");
160            Assert.True(await Page.EvaluateExpressionAsync<bool>("window.__clicked"));
161        }
162
163        [Fact]
164        public async Task ShouldClickOnCheckboxInputAndToggle()
165        {
166            await Page.GoToAsync(TestConstants.ServerUrl + "/input/checkbox.html");
167            Assert.Null(await Page.EvaluateExpressionAsync("result.check"));
168            await Page.ClickAsync("input#agree");
169            Assert.True(await Page.EvaluateExpressionAsync<bool>("result.check"));
170            Assert.Equal(new[] {
171                "mouseover",
172                "mouseenter",
173                "mousemove",
174                "mousedown",
175                "mouseup",
176                "click",
177                "input",
178                "change"
179            }, await Page.EvaluateExpressionAsync<string[]>("result.events"));
180            await Page.ClickAsync("input#agree");
181            Assert.False(await Page.EvaluateExpressionAsync<bool>("result.check"));
182        }
183
184        [Fact]
185        public async Task ShouldClickOnCheckboxLabelAndToggle()
186        {
187            await Page.GoToAsync(TestConstants.ServerUrl + "/input/checkbox.html");
188            Assert.Null(await Page.EvaluateExpressionAsync("result.check"));
189            await Page.ClickAsync("label[for=\"agree\"]");
190            Assert.True(await Page.EvaluateExpressionAsync<bool>("result.check"));
191            Assert.Equal(new[] {
192                "click",
193                "input",
194                "change"
195            }, await Page.EvaluateExpressionAsync<string[]>("result.events"));
196            await Page.ClickAsync("label[for=\"agree\"]");
197            Assert.False(await Page.EvaluateExpressionAsync<bool>("result.check"));
198        }
199
200        [Fact]
201        public async Task ShouldFailToClickAMissingButton()
202        {
203            await Page.GoToAsync(TestConstants.ServerUrl + "/input/button.html");
204            var exception = await Assert.ThrowsAsync<SelectorException>(()
205                => Page.ClickAsync("button.does-not-exist"));
206            Assert.Equal("No node found for selector: button.does-not-exist", exception.Message);
207            Assert.Equal("button.does-not-exist", exception.Selector);
208        }
209
210        [Fact]
211        public async Task ShouldScrollAndClickTheButton()
212        {
213            await Page.GoToAsync(TestConstants.ServerUrl + "/input/scrollable.html");
214            await Page.ClickAsync("#button-5");
215            Assert.Equal("clicked", await Page.EvaluateExpressionAsync<string>("document.querySelector(\"#button-5\").textContent"));
216            await Page.ClickAsync("#button-80");
217            Assert.Equal("clicked", await Page.EvaluateExpressionAsync<string>("document.querySelector(\"#button-80\").textContent"));
218        }
219
220        [Fact]
221        public async Task ShouldDoubleClickTheButton()
222        {
223            await Page.GoToAsync(TestConstants.ServerUrl + "/input/button.html");
224            await Page.EvaluateExpressionAsync(@"{
225               window.double = false;
226               const button = document.querySelector('button');
227               button.addEventListener('dblclick', event => {
228                 window.double = true;
229               });
230            }");
231            var button = await Page.QuerySelectorAsync("button");
232            await button.ClickAsync(new ClickOptions { ClickCount = 2 });
233            Assert.True(await Page.EvaluateExpressionAsync<bool>("double"));
234            Assert.Equal("Clicked", await Page.EvaluateExpressionAsync<string>("result"));
235        }
236
237        [Fact]
238        public async Task ShouldClickAPartiallyObscuredButton()
239        {
240            await Page.GoToAsync(TestConstants.ServerUrl + "/input/button.html");
241            await Page.EvaluateExpressionAsync(@"{
242                const button = document.querySelector('button');
243                button.textContent = 'Some really long text that will go offscreen';
244                button.style.position = 'absolute';
245                button.style.left = '368px';
246            }");
247            await Page.ClickAsync("button");
248            Assert.Equal("Clicked", await Page.EvaluateExpressionAsync<string>("result"));
249        }
250
251        [Fact]
252        public async Task ShouldClickARotatedButton()
253        {
254            await Page.GoToAsync(TestConstants.ServerUrl + "/input/rotatedButton.html");
255            await Page.ClickAsync("button");
256            Assert.Equal("Clicked", await Page.EvaluateExpressionAsync<string>("result"));
257        }
258
259        [Fact]
260        public async Task ShouldFireContextmenuEventOnRightClick()
261        {
262            await Page.GoToAsync(TestConstants.ServerUrl + "/input/scrollable.html");
263            await Page.ClickAsync("#button-8", new ClickOptions { Button = MouseButton.Right });
264            Assert.Equal("context menu", await Page.EvaluateExpressionAsync<string>("document.querySelector('#button-8').textContent"));
265        }
266
267        // @see https://github.com/GoogleChrome/puppeteer/issues/206
268        [Fact]
269        public async Task ShouldClickLinksWhichCauseNavigation()
270        {
271            await Page.SetContentAsync($"<a href=\"{TestConstants.EmptyPage}\">empty.html</a>");
272            // This await should not hang.
273            await Page.ClickAsync("a");
274        }
275
276        [Fact]
277        public async Task ShouldClickTheButtonInsideAnIframe()
278        {
279            await Page.GoToAsync(TestConstants.EmptyPage);
280            await Page.SetContentAsync("<div style=\"width:100px;height:100px\">spacer</div>");
281            await FrameUtils.AttachFrameAsync(Page, "button-test", TestConstants.ServerUrl + "/input/button.html");
282            var frame = Page.FirstChildFrame();
283            var button = await frame.QuerySelectorAsync("button");
284            await button.ClickAsync();
285            Assert.Equal("Clicked", await frame.EvaluateExpressionAsync<string>("window.result"));
286        }
287
288        [Fact(Skip = "see https://github.com/GoogleChrome/puppeteer/issues/4110")]
289        public async Task ShouldClickTheButtonWithFixedPositionInsideAnIframe()
290        {
291            await Page.GoToAsync(TestConstants.EmptyPage);
292            await Page.SetViewportAsync(new ViewPortOptions
293            {
294                Width = 500,
295                Height = 500
296            });
297            await Page.SetContentAsync("<div style=\"width:100px;height:2000px\">spacer</div>");
298            await FrameUtils.AttachFrameAsync(Page, "button-test", TestConstants.ServerUrl + "/input/button.html");
299            var frame = Page.FirstChildFrame();
300            await frame.QuerySelectorAsync("button").EvaluateFunctionAsync("button => button.style.setProperty('position', 'fixed')");
301            await frame.ClickAsync("button");
302            Assert.Equal("Clicked", await frame.EvaluateExpressionAsync<string>("window.result"));
303        }
304
305        [Fact]
306        public async Task ShouldClickTheButtonWithDeviceScaleFactorSet()
307        {
308            await Page.SetViewportAsync(new ViewPortOptions { Width = 400, Height = 400, DeviceScaleFactor = 5 });
309            Assert.Equal(5, await Page.EvaluateExpressionAsync<int>("window.devicePixelRatio"));
310            await Page.SetContentAsync("<div style=\"width:100px;height:100px\">spacer</div>");
311            await FrameUtils.AttachFrameAsync(Page, "button-test", TestConstants.ServerUrl + "/input/button.html");
312            var frame = Page.FirstChildFrame();
313            var button = await frame.QuerySelectorAsync("button");
314            await button.ClickAsync();
315            Assert.Equal("Clicked", await frame.EvaluateExpressionAsync<string>("window.result"));
316        }
317    }
318}
319
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)