How to use BrowserClient class of PupppeterSharpAspNetFrameworkSample.Services package

Best Puppeteer-sharp code snippet using PupppeterSharpAspNetFrameworkSample.Services.BrowserClient

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

TestController.cs

Source: TestController.cs Github

copy
1using System.Threading.Tasks;
2using System.Web.Http;
3using PupppeterSharpAspNetFrameworkSample.Services;
4
5namespace PupppeterSharpAspNetFrameworkSample.Controllers
6{
7    public class TestController : ApiController
8    {
9        public Task<string> Get() => BrowserClient.GetTextAsync("http://localhost/PupppeterSharpAspNetFrameworkSample/api/foo");
10    }
11}
12
Full Screen

BrowserClient.cs

Source: BrowserClient.cs Github

copy
1using System.Threading.Tasks;
2using System.Web.Hosting;
3using PuppeteerSharp;
4using PuppeteerSharp.AspNetFramework;
5
6namespace PupppeterSharpAspNetFrameworkSample.Services
7{
8    public class BrowserClient
9    {
10        private static readonly string HostPath = HostingEnvironment.MapPath("~/App_Data/");
11
12        public static async Task<string> GetTextAsync(string url)
13        {
14            using var browserFetcher = new BrowserFetcher(new BrowserFetcherOptions()
15            {
16                Path = HostPath
17            });
18            await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision).ConfigureAwait(false);
19
20            using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions()
21            {
22                Headless = true,
23                TransportFactory = AspNetWebSocketTransport.AspNetTransportFactory,
24                ExecutablePath = browserFetcher.GetExecutablePath(BrowserFetcher.DefaultRevision)
25            }).ConfigureAwait(false))
26            using (var page = await browser.NewPageAsync().ConfigureAwait(false))
27            {
28                var response = await page.GoToAsync(url).ConfigureAwait(false);
29                return await response.TextAsync().ConfigureAwait(false);
30            }
31        }
32    }
33}
34
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 methods in BrowserClient

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

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)