How to use BrowserEventsDisconnectedTests class of PuppeteerSharp.Tests.LauncherTests package

Best Puppeteer-sharp code snippet using PuppeteerSharp.Tests.LauncherTests.BrowserEventsDisconnectedTests

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

BrowserEventsDisconnectedTests.cs

Source: BrowserEventsDisconnectedTests.cs Github

copy
1using System.Threading.Tasks;
2using PuppeteerSharp.Tests.Attributes;
3using PuppeteerSharp.Xunit;
4using Xunit;
5using Xunit.Abstractions;
6
7namespace PuppeteerSharp.Tests.LauncherTests
8{
9    [Collection(TestConstants.TestFixtureCollectionName)]
10    public class BrowserEventsDisconnectedTests : PuppeteerBrowserBaseTest
11    {
12        public BrowserEventsDisconnectedTests(ITestOutputHelper output) : base(output)
13        {
14        }
15
16        [PuppeteerTest("launcher.spec.ts", "Browser.Events.disconnected", "should be emitted when: browser gets closed, disconnected or underlying websocket gets closed")]
17        [PuppeteerFact]
18        public async Task ShouldEmittedWhenBrowserGetsClosedDisconnectedOrUnderlyingWebsocketGetsClosed()
19        {
20            var originalBrowser = await Puppeteer.LaunchAsync(TestConstants.DefaultBrowserOptions(), TestConstants.LoggerFactory);
21            var connectOptions = new ConnectOptions { BrowserWSEndpoint = originalBrowser.WebSocketEndpoint };
22            var remoteBrowser1 = await Puppeteer.ConnectAsync(connectOptions, TestConstants.LoggerFactory);
23            var remoteBrowser2 = await Puppeteer.ConnectAsync(connectOptions, TestConstants.LoggerFactory);
24
25            var disconnectedOriginal = 0;
26            var disconnectedRemote1 = 0;
27            var disconnectedRemote2 = 0;
28            originalBrowser.Disconnected += (_, _) => ++disconnectedOriginal;
29            remoteBrowser1.Disconnected += (_, _) => ++disconnectedRemote1;
30            remoteBrowser2.Disconnected += (_, _) => ++disconnectedRemote2;
31
32            var remoteBrowser2Disconnected = WaitForBrowserDisconnect(remoteBrowser2);
33            remoteBrowser2.Disconnect();
34            await remoteBrowser2Disconnected;
35
36            Assert.Equal(0, disconnectedOriginal);
37            Assert.Equal(0, disconnectedRemote1);
38            Assert.Equal(1, disconnectedRemote2);
39
40            var remoteBrowser1Disconnected = WaitForBrowserDisconnect(remoteBrowser1);
41            var originalBrowserDisconnected = WaitForBrowserDisconnect(originalBrowser);
42
43            await Task.WhenAll(
44                originalBrowser.CloseAsync(),
45                remoteBrowser1Disconnected,
46                originalBrowserDisconnected
47            );
48
49            Assert.Equal(1, disconnectedOriginal);
50            Assert.Equal(1, disconnectedRemote1);
51            Assert.Equal(1, disconnectedRemote2);
52        }
53    }
54}
55
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

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)