How to use EnterFromAsync method of PuppeteerSharp.States.ChromiumStartingState class

Best Puppeteer-sharp code snippet using PuppeteerSharp.States.ChromiumStartingState.EnterFromAsync

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

ChromiumStartingState.cs

Source: ChromiumStartingState.cs Github

copy
1using System;
2using System.Diagnostics;
3using System.Text;
4using System.Text.RegularExpressions;
5using System.Threading;
6using System.Threading.Tasks;
7
8namespace PuppeteerSharp.States
9{
10    internal class ChromiumStartingState : State
11    {
12        public ChromiumStartingState(StateManager stateManager) : base(stateManager)
13        {
14        }
15
16        public override Task EnterFromAsync(LauncherBase p, State fromState, TimeSpan timeout)
17        {
18            if (!StateManager.TryEnter(p, fromState, this))
19            {
20                // Delegate StartAsync to current state, because it has already changed since
21                // transition to this state was initiated.
22                return StateManager.CurrentState.StartAsync(p);
23            }
24
25            return StartCoreAsync(p);
26        }
27
28        public override Task StartAsync(LauncherBase p) => p.StartCompletionSource.Task;
29
30        public override Task ExitAsync(LauncherBase p, TimeSpan timeout) => StateManager.Exiting.EnterFromAsync(p, this, timeout);
31
32        public override Task KillAsync(LauncherBase p) => StateManager.Killing.EnterFromAsync(p, this);
33
34        public override void Dispose(LauncherBase p)
35        {
36            p.StartCompletionSource.TrySetException(new ObjectDisposedException(p.ToString()));
37            base.Dispose(p);
38        }
39
40        protected virtual async Task StartCoreAsync(LauncherBase p)
41        {
42            var output = new StringBuilder();
43
44            void OnProcessDataReceivedWhileStarting(object sender, DataReceivedEventArgs e)
45            {
46                if (e.Data != null)
47                {
48                    output.AppendLine(e.Data);
49                    var match = Regex.Match(e.Data, "^DevTools listening on (ws:\\/\\/.*)");
50                    if (match.Success)
51                    {
52                        p.StartCompletionSource.TrySetResult(match.Groups[1].Value);
53                    }
54                }
55            }
56
57            void OnProcessExitedWhileStarting(object sender, EventArgs e)
58                => p.StartCompletionSource.TrySetException(new ProcessException($"Failed to launch browser! {output}"));
59
60            void OnProcessExited(object sender, EventArgs e) => StateManager.Exited.EnterFrom(p, StateManager.CurrentState);
61
62            p.Process.ErrorDataReceived += OnProcessDataReceivedWhileStarting;
63            p.Process.Exited += OnProcessExitedWhileStarting;
64            p.Process.Exited += OnProcessExited;
65            CancellationTokenSource cts = null;
66            try
67            {
68                p.Process.Start();
69                await StateManager.Started.EnterFromAsync(p, this).ConfigureAwait(false);
70
71                p.Process.BeginErrorReadLine();
72
73                var timeout = p.Options.Timeout;
74                if (timeout > 0)
75                {
76                    cts = new CancellationTokenSource(timeout);
77                    cts.Token.Register(() => p.StartCompletionSource.TrySetException(
78                        new ProcessException($"Timed out after {timeout} ms while trying to connect to Base!")));
79                }
80
81                try
82                {
83                    await p.StartCompletionSource.Task.ConfigureAwait(false);
84                    await StateManager.Started.EnterFromAsync(p, this).ConfigureAwait(false);
85                }
86                catch
87                {
88                    await StateManager.Killing.EnterFromAsync(p, this).ConfigureAwait(false);
89                    throw;
90                }
91            }
92            finally
93            {
94                cts?.Dispose();
95                p.Process.Exited -= OnProcessExitedWhileStarting;
96                p.Process.ErrorDataReceived -= OnProcessDataReceivedWhileStarting;
97            }
98        }
99    }
100}
101
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

Trigger EnterFromAsync code on LambdaTest Cloud Grid

Execute automation tests with EnterFromAsync on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

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)