How to use Main method of Example.ReuseDownloadedChrome.Program class

Best Puppeteer-sharp code snippet using Example.ReuseDownloadedChrome.Program.Main

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.IO;
3using System.Threading.Tasks;
4using System.Linq;
5using PuppeteerSharp;
6
7namespace Example.ReuseDownloadedChrome
8{
9    class Program
10    {
11        public static async Task Main(string[] args)
12        {
13            Console.WriteLine("This example downloads the default version of Chromium to a custom location");
14
15            var currentDirectory = Directory.GetCurrentDirectory();
16            var downloadPath = Path.Combine(currentDirectory, "..", "..", "CustomChromium");
17            Console.WriteLine($"Attemping to set up puppeteer to use Chromium found under directory {downloadPath} ");
18
19            if (!Directory.Exists(downloadPath))
20            {
21                Console.WriteLine("Custom directory not found. Creating directory");
22                Directory.CreateDirectory(downloadPath);
23            }
24
25            Console.WriteLine("Downloading Chromium");
26
27            var browserFetcherOptions = new BrowserFetcherOptions { Path = downloadPath };
28            var browserFetcher = new BrowserFetcher(browserFetcherOptions);
29            await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);
30
31            var executablePath = browserFetcher.GetExecutablePath(BrowserFetcher.DefaultRevision);
32
33            if (string.IsNullOrEmpty(executablePath))
34            {
35                Console.WriteLine("Custom Chromium location is empty. Unable to start Chromium. Exiting.\n Press any key to continue");
36                Console.ReadLine();
37                return;
38            }
39
40            Console.WriteLine($"Attemping to start Chromium using executable path: {executablePath}");
41
42            var options = new LaunchOptions { Headless = true, ExecutablePath = executablePath };
43
44            using (var browser = await Puppeteer.LaunchAsync(options))
45            using (var page = await browser.NewPageAsync())
46            {
47                await page.GoToAsync("http://www.google.com");
48                var jsSelectAllAnchors = @"Array.from(document.querySelectorAll('a')).map(a => a.href);";
49                var urls = await page.EvaluateExpressionAsync<string[]>(jsSelectAllAnchors);
50                foreach (string url in urls)
51                {
52                    Console.WriteLine($"Url: {url}");
53                }
54                Console.WriteLine("Press any key to continue...");
55                Console.ReadLine();
56            }
57            return;
58        }
59    }
60}
61
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 Program

Trigger Main code on LambdaTest Cloud Grid

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