How to use WriteDeviceDescriptorName method of PuppeteerSharp.Mobile.DeviceDescriptors class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Mobile.DeviceDescriptors.WriteDeviceDescriptorName

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

Program.cs

Source: Program.cs Github

copy
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using System.Net.Http;
6using System.Text;
7using System.Threading.Tasks;
8using Newtonsoft.Json;
9namespace PuppeteerSharp.DevicesFetcher
10{
11    class Program
12    {
13        const string DEVICES_URL = "https://raw.githubusercontent.com/puppeteer/puppeteer/master/src/common/DeviceDescriptors.ts";
14
15        static readonly string deviceDescriptorsOutput = "../../../../PuppeteerSharp/Mobile/DeviceDescriptors.cs";
16        static readonly string deviceDescriptorNameOutput = "../../../../PuppeteerSharp/Mobile/DeviceDescriptorName.cs";
17
18        static async Task Main(string[] args)
19        {
20            var url = DEVICES_URL;
21            if (args.Length > 0)
22            {
23                url = args[0];
24            }
25
26            Console.WriteLine($"GET {url}");
27            var text = await HttpGET(url);
28
29            const string DeviceArray = "Device[] = [";
30            var startIndex = text.IndexOf(DeviceArray) + DeviceArray.Length;
31            var endIndex = text.IndexOf("];", startIndex);
32            var length = endIndex - startIndex;
33            text = "[" + text.Substring(startIndex, length) + "]";
34
35            Device[] devices;
36            try
37            {
38                devices = JsonConvert.DeserializeObject<Device[]>(text);
39            }
40            catch (Exception ex)
41            {
42                Console.WriteLine($"FAILED: error parsing response - {ex.Message}");
43                return;
44            }
45
46            WriteDeviceDescriptors(devices);
47            WriteDeviceDescriptorName(devices);
48        }
49
50        static void WriteDeviceDescriptors(IEnumerable<Device> devices)
51        {
52            var builder = new StringBuilder();
53            var begin = @"using System;
54using System.Collections.Generic;
55using System.Collections.ObjectModel;
56
57namespace PuppeteerSharp.Mobile
58{
59    /// <summary>
60    /// Device descriptors.
61    /// </summary>
62    public static class DeviceDescriptors
63    {
64        private static readonly Dictionary<DeviceDescriptorName, DeviceDescriptor> Devices = new Dictionary<DeviceDescriptorName, DeviceDescriptor>
65        {
66";
67            var end = @"
68        };
69
70        private static Lazy<IReadOnlyDictionary<DeviceDescriptorName, DeviceDescriptor>> _readOnlyDevices =
71            new Lazy<IReadOnlyDictionary<DeviceDescriptorName, DeviceDescriptor>>(() => new ReadOnlyDictionary<DeviceDescriptorName, DeviceDescriptor>(Devices));
72
73        internal static IReadOnlyDictionary<DeviceDescriptorName, DeviceDescriptor> ToReadOnly() => _readOnlyDevices.Value;
74
75        /// <summary>
76        /// Get the specified device description.
77        /// </summary>
78        /// <returns>The device descriptor.</returns>
79        /// <param name=""name"">Device Name.</param>
80        [Obsolete(""Use Puppeteer.Devices instead"")]
81        public static DeviceDescriptor Get(DeviceDescriptorName name) => Devices[name];
82    }
83}";
84
85            builder.Append(begin);
86            builder.AppendJoin(",\n", devices.Select(GenerateCsharpFromDevice));
87            builder.Append(end);
88
89            File.WriteAllText(deviceDescriptorsOutput, builder.ToString());
90        }
91
92        static void WriteDeviceDescriptorName(IEnumerable<Device> devices)
93        {
94            var builder = new StringBuilder();
95            var begin = @"namespace PuppeteerSharp.Mobile
96{
97    /// <summary>
98    /// Device descriptor name.
99    /// </summary>
100    public enum DeviceDescriptorName
101    {";
102            var end = @"
103    }
104}";
105
106            builder.Append(begin);
107            builder.AppendJoin(",", devices.Select(device =>
108            {
109                return [email protected]"
110        /// <summary>
111        /// {device.Name}
112        /// </summary>
113        {DeviceNameToEnumValue(device)}";
114            }));
115            builder.Append(end);
116
117            File.WriteAllText(deviceDescriptorNameOutput, builder.ToString());
118        }
119
120        static string GenerateCsharpFromDevice(Device device)
121        {
122            return [email protected]"            [DeviceDescriptorName.{DeviceNameToEnumValue(device)}] = new DeviceDescriptor
123            {{
124                Name = ""{device.Name}"",
125                UserAgent = ""{device.UserAgent}"",
126                ViewPort = new ViewPortOptions
127                {{
128                    Width = {device.Viewport.Width},
129                    Height = {device.Viewport.Height},
130                    DeviceScaleFactor = {device.Viewport.DeviceScaleFactor},
131                    IsMobile = {device.Viewport.IsMobile.ToString().ToLower()},
132                    HasTouch = {device.Viewport.HasTouch.ToString().ToLower()},
133                    IsLandscape = {device.Viewport.IsLandscape.ToString().ToLower()}
134                }}
135            }}";
136        }
137
138        static string DeviceNameToEnumValue(Device device)
139        {
140            var dotNetName = device.Name.Replace("+", "Plus");
141            var output = new StringBuilder();
142            output.Append(char.ToUpper(dotNetName[0]));
143            for (var i = 1; i < dotNetName.Length; i++)
144            {
145                if (char.IsWhiteSpace(dotNetName[i]))
146                {
147                    output.Append(char.ToUpper(dotNetName[i + 1]));
148                    i++;
149                }
150                else
151                {
152                    output.Append(dotNetName[i]);
153                }
154            }
155
156            return output.ToString();
157        }
158
159        static Task<string> HttpGET(string url) => new HttpClient().GetStringAsync(url);
160    }
161}
162
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 DeviceDescriptors

Trigger WriteDeviceDescriptorName code on LambdaTest Cloud Grid

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