How to use Get method of PuppeteerSharp.Mobile.Program class

Best Puppeteer-sharp code snippet using PuppeteerSharp.Mobile.Program.Get

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

Scraping.cs

Source: Scraping.cs Github

copy
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using PuppeteerSharp;
6using System.Threading.Tasks;
7using System.Runtime.InteropServices;
8using PuppeteerSharp.Input;
9using System.Runtime.CompilerServices;
10using System.Reflection.Metadata;
11using System.IO;
12using System.Text.Json;
13
14using System.Text.RegularExpressions;
15
16namespace CodeSignalScraper
17{
18    public static class Scraping
19    {
20        public static async Task<List<(string url, string title)>> ScanAreas(Page page)
21        {
22            var result = new List<(string url, string title)>();
23            Console.WriteLine("Scanning areas:");
24            var response = await page.GoToAsync(Program.BaseUrl + "/arcade");
25            var dummy = await page.WaitForSelectorAsync("div.card a");
26            var linkList = await page.QuerySelectorAllAsync("div.card a");
27            foreach(var link in linkList)
28            {
29                var url = await (await link.GetPropertyAsync("href")).JsonValueAsync<string>();
30                var title = await(await(await link.QuerySelectorAsync("h4")).GetPropertyAsync("innerText")).JsonValueAsync<string>();
31                Console.WriteLine($"Found area: {title}");
32                result.Add((url: url, title: title));
33            }
34            return result;
35        }
36        public static async Task<List<TaskInfo>> ScanTasks(Page page, IEnumerable<(string url, string title)> areas)
37        {
38            var result = new List<TaskInfo>();
39            foreach (var area in areas)
40            {
41                if (area.title == "Databases") continue;  // Does not support SQL queries.
42                if (area.title == "Python") continue;  // Does not support multiple choice.
43                Console.WriteLine($"Opening area {area.title}");
44                var response = await page.GoToAsync(area.url);
45                var dummy = await page.WaitForSelectorAsync("div.arcade-map--topic");
46                Console.WriteLine($"Scanning area {area.title}");
47                var topicList = await page.QuerySelectorAllAsync("div.arcade-map--topic");
48                foreach (var topic in topicList)
49                {
50                    var span = await topic.QuerySelectorAsync("span");
51                    var topicTitle = await (await span.GetPropertyAsync("innerText")).JsonValueAsync<string>();
52
53                    var taskList = await topic.QuerySelectorAllAsync("div.arcade-map--topic-details a.arcade-map--task");
54                    foreach (var task in taskList)
55                    {
56                        var taskUrl = await (await task.GetPropertyAsync("href")).JsonValueAsync<string>();
57                        var solvedText = await (await task.GetPropertyAsync("className")).JsonValueAsync<string>();
58                        var solved = !solvedText.Contains("-current") && !solvedText.Contains("-not-solved");
59                        var taskTitle = await (await (await task.QuerySelectorAsync("h3")).GetPropertyAsync("innerText")).JsonValueAsync<string>();
60
61                        result.Add(new TaskInfo(area.url, area.title, topicTitle, taskUrl, taskTitle, solved));
62                    }
63                }
64            }
65            return result;
66        }
67
68        public static async Task RetrieveTask(Page page, TaskInfo task)
69        {
70            Console.Write($"Reading {task.Task} of {task.Topic}.");
71
72            await page.GoToAsync(task.TaskUrl);
73            Console.Write(".");
74            await page.SetViewportAsync(new ViewPortOptions() { Height = 100000, Width = 8000, HasTouch = false, IsMobile = false });
75            Console.Write(".");
76            var source = await page.WaitForSelectorAsync("div.view-lines");
77            Console.Write(".");
78            task.Source = await (await source.GetPropertyAsync("innerText")).JsonValueAsync<string>();
79            Console.Write(".");
80            await page.SetViewportAsync(new ViewPortOptions() { Height = 800, Width = 1600, HasTouch = false, IsMobile = false });
81            Console.Write(".");
82
83            var desc = await page.QuerySelectorAsync("div.markdown");
84            Console.Write(".");
85            task.Description = await (await desc.GetPropertyAsync("innerText")).JsonValueAsync<string>();
86            Console.Write(".");
87            var index = task.Description.IndexOf("[C#] Syntax Tips");
88            if (index > 0) task.Description = task.Description.Substring(0, index);
89
90            if (Source.TestExists(task, out var _)) return;  // No need to read tests.
91
92
93            task.Tests = new List<string>();
94            var list = await page.QuerySelectorAllAsync("div.accordion");
95            Console.Write(".");
96            foreach (var item in list)
97            {
98                var btn = await item.QuerySelectorAsync("div.accordion--head");
99                Console.Write(".");
100                PuppeteerSharp.ElementHandle body;
101                do
102                {
103                    await btn.ClickAsync();
104                    Console.Write(".");
105                    body = await item.QuerySelectorAsync("div.accordion--body");
106                    Console.Write(".");
107                    if (body == null) await Task.Delay(100);
108                } while (body == null);
109                string test;
110                var warning = await body.QuerySelectorAsync("div.task-tests--warning");
111                Console.Write(".");
112                if (warning != null)
113                {
114                    // The test example is too big, download it.
115                    var name = await (await btn.GetPropertyAsync("innerText")).JsonValueAsync<string>();
116                    Console.Write(".");
117                    name = name.Replace(' ', '-') + ".json";
118                    name = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads", name);
119                    if (File.Exists(name)) File.Delete(name);
120                    var download = await warning.QuerySelectorAsync("div.button");
121                    Console.Write(".");
122                    await download.ClickAsync();
123                    Console.Write(".");
124                    int limit = 80;
125                    while (limit-- > 0 && !File.Exists(name))
126                    {
127                        await Task.Delay(250);
128                    }
129                    if (File.Exists(name))
130                    {
131                        test = ReadJsonTest(name);
132                    }
133                    else
134                    {
135                        test = "Failed to download test " + name;
136                        task.Failed = true;
137                    }
138                }
139                else
140                {
141                    var input = await body.QuerySelectorAsync("pre.task-tests--value");
142                    if (input == null) break;
143                    test = await (await input.GetPropertyAsync("innerText")).JsonValueAsync<string>();
144                    Console.Write(".");
145
146                    input = await body.QuerySelectorAsync("pre.-answer");
147                    Console.Write(".");
148                    test += "\nExpected Output: " + await (await input.GetPropertyAsync("innerText")).JsonValueAsync<string>();
149                    Console.Write(".");
150                }
151                task.Tests.Add(test);
152            }
153            Console.WriteLine("");
154        }
155        private static string ReadJsonTest(string filename)
156        {
157            var jsonText = File.ReadAllText(filename);
158            var json = JsonSerializer.Deserialize(jsonText, typeof(object)) as JsonElement?;
159            StringBuilder result = new StringBuilder();
160            json.Value.TryGetProperty("input", out var input);
161            foreach (var value in input.EnumerateObject())
162            {
163                Write(result, value.Name, value.Value);
164            }
165            json.Value.TryGetProperty("output", out var output);
166            Write(result, "Expected Output", output);
167
168            return result.ToString();
169        }
170        static void Write(StringBuilder result, string name, JsonElement value)
171        {
172            result.Append(name);
173            result.Append(": ");
174            var json = value.ToString();
175            if (value.ValueKind == JsonValueKind.Array)
176            {
177                json = Regex.Replace(json, @"(?<=""[^""]*""|-?\w+)\s*,\s*(?=""[^""]*""|-?\w+)", ",");
178                json = Regex.Replace(json, @"(?<=\[|-?\w+)\s*(?=""[^""]*""|-?\w+)", "");
179                json = Regex.Replace(json, @"(?<=""[^""]*""|-?\w+)\s*(?=\])", "");
180                json = Regex.Replace(json, @"(?<=\])\s*,\s*(?=\[)", "," + Environment.NewLine);
181                json = Regex.Replace(json, @"(?<=\[)\s+(?=\[)", "");
182                json = Regex.Replace(json, @"(?<=\])\s+(?=\])", "");
183                json = Regex.Replace(json, @"(?<=\[[^\[\]]{1,16}\])\s*,\s*(?=\[[^\[\]]{1,16}\])", ",");
184            }
185            else if (value.ValueKind == JsonValueKind.String)
186            {
187                json = "\"" + json.Replace("\"", "\"\"") + "\"";
188            }
189            json = json.Replace("True", "true").Replace("False", "false");
190            result.AppendLine(json);
191        }
192    }
193}
194
Full Screen

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;
9using Newtonsoft.Json.Linq;
10using Newtonsoft.Json.Serialization;
11
12namespace PuppeteerSharp.DevicesFetcher
13{
14    class Program
15    {
16        const string DEVICES_URL = "https://raw.githubusercontent.com/ChromeDevTools/devtools-frontend/master/front_end/emulated_devices/module.json";
17
18        static string deviceDescriptorsOutput = "../../../../PuppeteerSharp/Mobile/DeviceDescriptors.cs";
19        static string deviceDescriptorNameOutput = "../../../../PuppeteerSharp/Mobile/DeviceDescriptorName.cs";
20
21        static async Task Main(string[] args)
22        {
23            var url = DEVICES_URL;
24            if (args.Length > 0)
25            {
26                url = args[0];
27            }
28
29            string chromeVersion = null;
30            var fetcher = new BrowserFetcher();
31            await fetcher.DownloadAsync(BrowserFetcher.DefaultRevision);
32            using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions()))
33            {
34                chromeVersion = (await browser.GetVersionAsync()).Split('/').Last();
35            }
36
37            Console.WriteLine($"GET {url}");
38            var text = await HttpGET(url);
39            RootObject json = null;
40            try
41            {
42                json = JsonConvert.DeserializeObject<RootObject>(text);
43            }
44            catch (Exception ex)
45            {
46                Console.WriteLine($"FAILED: error parsing response - {ex.Message}");
47            }
48            var devicePayloads = json.Extensions
49                .Where(extension => extension.Type == "emulated-device")
50                .Select(extension => extension.Device)
51                .ToArray();
52            var devices = new List<OutputDevice>();
53            foreach (var payload in devicePayloads)
54            {
55                string[] names;
56                if (payload.Title == "iPhone 6/7/8")
57                {
58                    names = new[] { "iPhone 6", "iPhone 7", "iPhone 8" };
59                }
60                else if (payload.Title == "iPhone 6/7/8 Plus")
61                {
62                    names = new[] { "iPhone 6 Plus", "iPhone 7 Plus", "iPhone 8 Plus" };
63                }
64                else if (payload.Title == "iPhone 5/SE")
65                {
66                    names = new[] { "iPhone 5", "iPhone SE" };
67                }
68                else
69                {
70                    names = new[] { payload.Title };
71                }
72                foreach (var name in names)
73                {
74                    var device = CreateDevice(chromeVersion, name, payload, false);
75                    var landscape = CreateDevice(chromeVersion, name, payload, true);
76                    devices.Add(device);
77                    if (landscape.Viewport.Width != device.Viewport.Width || landscape.Viewport.Height != device.Viewport.Height)
78                    {
79                        devices.Add(landscape);
80                    }
81                }
82            }
83            devices.RemoveAll(device => !device.Viewport.IsMobile);
84            devices.Sort((a, b) => a.Name.CompareTo(b.Name));
85
86            WriteDeviceDescriptors(devices);
87            WriteDeviceDescriptorName(devices);
88        }
89
90        static void WriteDeviceDescriptors(IEnumerable<OutputDevice> devices)
91        {
92            var builder = new StringBuilder();
93            var begin = @"using System.Collections.Generic;
94
95namespace PuppeteerSharp.Mobile
96{
97    /// <summary>
98    /// Device descriptors.
99    /// </summary>
100    public class DeviceDescriptors
101    {
102        private static readonly Dictionary<DeviceDescriptorName, DeviceDescriptor> Devices = new Dictionary<DeviceDescriptorName, DeviceDescriptor>
103        {
104";
105            var end = @"
106        };
107            
108        /// <summary>
109        /// Get the specified device description.
110        /// </summary>
111        /// <returns>The device descriptor.</returns>
112        /// <param name=""name"">Device Name.</param>
113        public static DeviceDescriptor Get(DeviceDescriptorName name) => Devices[name];
114    }
115}";
116
117            builder.Append(begin);
118            builder.AppendJoin(",\n", devices.Select(GenerateCsharpFromDevice));
119            builder.Append(end);
120
121            File.WriteAllText(deviceDescriptorsOutput, builder.ToString());
122        }
123
124        static void WriteDeviceDescriptorName(IEnumerable<OutputDevice> devices)
125        {
126            var builder = new StringBuilder();
127            var begin = @"namespace PuppeteerSharp.Mobile
128{
129    /// <summary>
130    /// Device descriptor name.
131    /// </summary>
132    public enum DeviceDescriptorName
133    {";
134            var end = @"
135    }
136}";
137
138            builder.Append(begin);
139            builder.AppendJoin(",", devices.Select(device =>
140            {
141                return [email protected]"
142        /// <summary>
143        /// {device.Name}
144        /// </summary>
145        {DeviceNameToEnumValue(device)}";
146            }));
147            builder.Append(end);
148
149            File.WriteAllText(deviceDescriptorNameOutput, builder.ToString());
150        }
151
152        static string GenerateCsharpFromDevice(OutputDevice device)
153        {
154            var w = string.Empty;
155            return [email protected]"            [DeviceDescriptorName.{DeviceNameToEnumValue(device)}] = new DeviceDescriptor
156            {{
157                Name = ""{device.Name}"",
158                UserAgent = ""{device.UserAgent}"",
159                ViewPort = new ViewPortOptions
160                {{
161                    Width = {device.Viewport.Width},
162                    Height = {device.Viewport.Height},
163                    DeviceScaleFactor = {device.Viewport.DeviceScaleFactor},
164                    IsMobile = {device.Viewport.IsMobile.ToString().ToLower()},
165                    HasTouch = {device.Viewport.HasTouch.ToString().ToLower()},
166                    IsLandscape = {device.Viewport.IsLandscape.ToString().ToLower()}
167                }}
168            }}";
169        }
170        static string DeviceNameToEnumValue(OutputDevice device)
171        {
172            var output = new StringBuilder();
173            output.Append(char.ToUpper(device.Name[0]));
174            for (var i = 1; i < device.Name.Length; i++)
175            {
176                if (char.IsWhiteSpace(device.Name[i]))
177                {
178                    output.Append(char.ToUpper(device.Name[i + 1]));
179                    i++;
180                }
181                else
182                {
183                    output.Append(device.Name[i]);
184                }
185            }
186
187            return output.ToString();
188        }
189
190        static OutputDevice CreateDevice(string chromeVersion, string deviceName, RootObject.Device descriptor, bool landscape)
191        {
192            var devicePayload = LoadFromJSONV1(descriptor);
193            var viewportPayload = landscape ? devicePayload.Horizontal : devicePayload.Vertical;
194            return new OutputDevice
195            {
196                Name = deviceName + (landscape ? " landscape" : string.Empty),
197                UserAgent = devicePayload.UserAgent.Replace("%s", chromeVersion),
198                Viewport = new OutputDevice.OutputDeviceViewport
199                {
200                    Width = viewportPayload.Width,
201                    Height = viewportPayload.Height,
202                    DeviceScaleFactor = devicePayload.DeviceScaleFactor,
203                    IsMobile = devicePayload.Capabilities.Contains("mobile"),
204                    HasTouch = devicePayload.Capabilities.Contains("touch"),
205                    IsLandscape = landscape
206                }
207            };
208        }
209
210        static DevicePayload LoadFromJSONV1(RootObject.Device json) => new DevicePayload
211        {
212            Type = json.Type,
213            UserAgent = json.UserAgent,
214            Capabilities = json.Capabilities.ToHashSet(),
215            DeviceScaleFactor = json.Screen.DevicePixelRatio,
216            Horizontal = new ViewportPayload
217            {
218                Height = json.Screen.Horizontal.Height,
219                Width = json.Screen.Horizontal.Width
220            },
221            Vertical = new ViewportPayload
222            {
223                Height = json.Screen.Vertical.Height,
224                Width = json.Screen.Vertical.Width
225            }
226        };
227
228        static Task<string> HttpGET(string url) => new HttpClient().GetStringAsync(url);
229    }
230}
231
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 Get code on LambdaTest Cloud Grid

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