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

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

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.Threading.Tasks;
3using Xunit;
4using Xunit.Abstractions;
5
6namespace PuppeteerSharp.Tests.ElementHandleTests
7{
8    [Collection("PuppeteerLoaderFixture collection")]
9    public class ClickTests : PuppeteerPageBaseTest
10    {
11        public ClickTests(ITestOutputHelper output) : base(output)
12        {
13        }
14
15        [Fact]
16        public async Task ShouldWork()
17        {
18            await Page.GoToAsync(TestConstants.ServerUrl + "/input/button.html");
19            var button = await Page.QuerySelectorAsync("button");
20            await button.ClickAsync();
21            Assert.Equal("Clicked", await Page.EvaluateExpressionAsync<string>("result"));
22        }
23
24        [Fact]
25        public async Task ShouldWorkForShadowDomV1()
26        {
27            await Page.GoToAsync(TestConstants.ServerUrl + "/shadow.html");
28            var buttonHandle = (await Page.EvaluateExpressionHandleAsync("button")) as ElementHandle;
29            await buttonHandle.ClickAsync();
30            Assert.True(await Page.EvaluateExpressionAsync<bool>("clicked"));
31        }
32
33        [Fact]
34        public async Task ShouldWorkForTextNodes()
35        {
36            await Page.GoToAsync(TestConstants.ServerUrl + "/input/button.html");
37            var buttonTextNode = (await Page.EvaluateExpressionHandleAsync(
38                "document.querySelector('button').firstChild")) as ElementHandle;
39            var exception = await Assert.ThrowsAsync<PuppeteerException>(async () => await buttonTextNode.ClickAsync());
40            Assert.Equal("Node is not of type HTMLElement", exception.Message);
41        }
42
43        [Fact]
44        public async Task ShouldThrowForDetachedNodes()
45        {
46            await Page.GoToAsync(TestConstants.ServerUrl + "/input/button.html");
47            var button = await Page.QuerySelectorAsync("button");
48            await Page.EvaluateFunctionAsync("button => button.remove()", button);
49            var exception = await Assert.ThrowsAsync<PuppeteerException>(async () => await button.ClickAsync());
50            Assert.Equal("Node is detached from document", exception.Message);
51        }
52
53        [Fact]
54        public async Task ShouldThrowForHiddenNodes()
55        {
56            await Page.GoToAsync(TestConstants.ServerUrl + "/input/button.html");
57            var button = await Page.QuerySelectorAsync("button");
58            await Page.EvaluateFunctionAsync("button => button.style.display = 'none'", button);
59            var exception = await Assert.ThrowsAsync<PuppeteerException>(async () => await button.ClickAsync());
60            Assert.Equal("Node is not visible", exception.Message);
61        }
62
63        [Fact]
64        public async Task ShouldThrowForRecursivelyHiddenNodes()
65        {
66            await Page.GoToAsync(TestConstants.ServerUrl + "/input/button.html");
67            var button = await Page.QuerySelectorAsync("button");
68            await Page.EvaluateFunctionAsync("button => button.parentElement.style.display = 'none'", button);
69            var exception = await Assert.ThrowsAsync<PuppeteerException>(async () => await button.ClickAsync());
70            Assert.Equal("Node is not visible", exception.Message);
71        }
72
73        [Fact]
74        public async Task ShouldThrowForBrElements()
75        {
76            await Page.SetContentAsync("hello<br>goodbye");
77            var br = await Page.QuerySelectorAsync("br");
78            var exception = await Assert.ThrowsAsync<PuppeteerException>(async () => await br.ClickAsync());
79            Assert.Equal("Node is not visible", exception.Message);
80        }
81    }
82}
83
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)