How to use ConsoleEventArgs class of PuppeteerSharp package

Best Puppeteer-sharp code snippet using PuppeteerSharp.ConsoleEventArgs

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

BrowserTests.cs

Source: BrowserTests.cs Github

copy
1using System;
2using System.IO;
3using System.Linq;
4using System.Threading.Tasks;
5using PuppeteerSharp;
6using PuppeteerSharp.Contrib.Extensions;
7using Shouldly;
8using Xunit;
9
10namespace BlazorTable.Tests
11{
12    public class BrowserTests : IAsyncLifetime
13    {
14        private string BaseAddress;
15
16        private Browser Browser { get; set; }
17
18        public async Task InitializeAsync()
19        {
20            string filename = "BrowserTestsAddress.config";
21
22            if (File.Exists(filename))
23                BaseAddress = File.ReadAllText(filename);
24            else
25                throw new Exception($"Missing {filename}");
26
27            await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
28
29            Browser = await Puppeteer.LaunchAsync(new LaunchOptions
30            {
31                Headless = true
32            });
33        }
34
35        public async Task DisposeAsync()
36        {
37            await Browser?.CloseAsync();
38        }
39
40        private async Task PrintPerf(Page page)
41        {
42            var perf = await page.EvaluateExpressionAsync<long>("window.performance.timing.domContentLoadedEventEnd - window.performance.timing.navigationStart");
43            Console.WriteLine($"Load Time: {perf}ms");
44        }
45
46        [Fact]
47        public async Task CheckRoot()
48        {
49            bool hasError = false;
50
51            var page = await Browser.NewPageAsync();
52
53            page.Console += Page_Console;
54
55            void Page_Console(object sender, ConsoleEventArgs e)
56            {
57                if (e.Message.Type == ConsoleType.Error)
58                    hasError = true;
59            }
60
61            await page.GoToAsync(BaseAddress);
62
63            var selector = await page.WaitForSelectorAsync("div.table-responsive > table > tbody > tr:nth-child(1) > td:nth-child(3)");
64
65            (await selector.InnerTextAsync()).ShouldBe("Astrix Mariette");
66
67            hasError.ShouldBeFalse();
68
69            await PrintPerf(page);
70        }
71    }
72}
Full Screen

ConsoleEventArgs.cs

Source: ConsoleEventArgs.cs Github

copy
1using System;
2
3namespace PuppeteerSharp
4{
5    /// <summary>
6    /// <see cref="Page.Console"/> data.
7    /// </summary>
8    public class ConsoleEventArgs : EventArgs
9    {
10        /// <summary>
11        /// Gets the message.
12        /// </summary>
13        /// <value>The message.</value>
14        public ConsoleMessage Message { get; }
15
16        /// <summary>
17        /// Initializes a new instance of the <see cref="ConsoleEventArgs"/> class.
18        /// </summary>
19        /// <param name="message">Message.</param>
20        public ConsoleEventArgs(ConsoleMessage message) => Message = message;
21    }
22}
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 ConsoleEventArgs

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)