How to use StartTracingAsync method of Microsoft.Playwright.Transport.Channels.BrowserChannel class

Best Playwright-dotnet code snippet using Microsoft.Playwright.Transport.Channels.BrowserChannel.StartTracingAsync

Run Playwright-dotnet automation tests on LambdaTest cloud grid

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

BrowserChannel.cs

Source: BrowserChannel.cs Github

copy
1/*
2 * MIT License
3 *
4 * Copyright (c) Microsoft Corporation.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25using System;
26using System.Collections.Generic;
27using System.IO;
28using System.Linq;
29using System.Text.Json;
30using System.Threading.Tasks;
31using Microsoft.Playwright.Core;
32using Microsoft.Playwright.Transport.Protocol;
33
34namespace Microsoft.Playwright.Transport.Channels
35{
36    internal class BrowserChannel : Channel<Browser>
37    {
38        public BrowserChannel(string guid, Connection connection, Browser owner) : base(guid, connection, owner)
39        {
40        }
41
42        internal event EventHandler Closed;
43
44        internal override void OnMessage(string method, JsonElement? serverParams)
45        {
46            switch (method)
47            {
48                case "close":
49                    Closed?.Invoke(this, EventArgs.Empty);
50                    break;
51            }
52        }
53
54        internal Task<BrowserContextChannel> NewContextAsync(
55            bool? acceptDownloads = null,
56            bool? bypassCSP = null,
57            ColorScheme? colorScheme = null,
58            ReducedMotion? reducedMotion = null,
59            ForcedColors? forcedColors = null,
60            float? deviceScaleFactor = null,
61            IEnumerable<KeyValuePair<string, string>> extraHTTPHeaders = null,
62            Geolocation geolocation = null,
63            bool? hasTouch = null,
64            HttpCredentials httpCredentials = null,
65            bool? ignoreHTTPSErrors = null,
66            bool? isMobile = null,
67            bool? javaScriptEnabled = null,
68            string locale = null,
69            bool? offline = null,
70            IEnumerable<string> permissions = null,
71            Proxy proxy = null,
72            bool? recordHarOmitContent = null,
73            string recordHarPath = null,
74            Dictionary<string, object> recordVideo = null,
75            string storageState = null,
76            string storageStatePath = null,
77            string timezoneId = null,
78            string userAgent = null,
79            ViewportSize viewportSize = default,
80            ScreenSize screenSize = default,
81            string baseUrl = default,
82            bool? strictSelectors = default)
83        {
84            var args = new Dictionary<string, object>
85            {
86                { "acceptDownloads", acceptDownloads },
87                { "bypassCSP", bypassCSP },
88                { "colorScheme", colorScheme },
89                { "reducedMotion", reducedMotion },
90                { "deviceScaleFactor", deviceScaleFactor },
91            };
92
93            if (extraHTTPHeaders != null)
94            {
95                args["extraHTTPHeaders"] = extraHTTPHeaders.Select(kv => new HeaderEntry { Name = kv.Key, Value = kv.Value }).ToArray();
96            }
97
98            args.Add("geolocation", geolocation);
99            args.Add("hasTouch", hasTouch);
100            args.Add("httpCredentials", httpCredentials);
101            args.Add("ignoreHTTPSErrors", ignoreHTTPSErrors);
102            args.Add("isMobile", isMobile);
103            args.Add("javaScriptEnabled", javaScriptEnabled);
104            args.Add("locale", locale);
105            args.Add("offline", offline);
106            args.Add("permissions", permissions);
107            args.Add("proxy", proxy);
108            args.Add("strictSelectors", strictSelectors);
109            args.Add("forcedColors", forcedColors);
110
111            if (!string.IsNullOrEmpty(recordHarPath))
112            {
113                args.Add("recordHar", new
114                {
115                    Path = recordHarPath,
116                    OmitContent = recordHarOmitContent,
117                });
118            }
119
120            if (recordVideo != null)
121            {
122                args.Add("recordVideo", recordVideo);
123            }
124
125            if (!string.IsNullOrEmpty(storageStatePath))
126            {
127                if (!File.Exists(storageStatePath))
128                {
129                    throw new PlaywrightException($"The specified storage state file does not exist: {storageStatePath}");
130                }
131
132                storageState = File.ReadAllText(storageStatePath);
133            }
134
135            if (!string.IsNullOrEmpty(storageState))
136            {
137                args.Add("storageState", JsonSerializer.Deserialize<StorageState>(storageState, Helpers.JsonExtensions.DefaultJsonSerializerOptions));
138            }
139
140            args.Add("timezoneId", timezoneId);
141            args.Add("userAgent", userAgent);
142
143            if (viewportSize?.Width == -1)
144            {
145                args.Add("noDefaultViewport", true);
146            }
147            else
148            {
149                args.Add("viewport", viewportSize);
150                args.Add("screen", screenSize);
151            }
152
153            args.Add("baseURL", baseUrl);
154
155            return Connection.SendMessageToServerAsync<BrowserContextChannel>(
156                Guid,
157                "newContext",
158                args);
159        }
160
161        internal Task CloseAsync() => Connection.SendMessageToServerAsync<BrowserContextChannel>(Guid, "close", null);
162
163        internal Task StartTracingAsync(IPage page, bool screenshots, string path, IEnumerable<string> categories)
164        {
165            var args = new Dictionary<string, object>
166            {
167                ["screenshots"] = screenshots,
168                ["path"] = path,
169                ["page"] = page,
170                ["categories"] = categories,
171            };
172
173            return Connection.SendMessageToServerAsync(Guid, "crStartTracing", args);
174        }
175
176        internal async Task<string> StopTracingAsync()
177            => (await Connection.SendMessageToServerAsync(Guid, "crStopTracing", null).ConfigureAwait(false))?.GetProperty("binary").ToString();
178    }
179}
180
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 method in BrowserChannel

Trigger StartTracingAsync code on LambdaTest Cloud Grid

Execute automation tests with StartTracingAsync 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)