How to use ViewPortOptions class of PuppeteerSharp package

Best Puppeteer-sharp code snippet using PuppeteerSharp.ViewPortOptions

BaseScraper.cs

Source:BaseScraper.cs Github

copy

Full Screen

...14 where P: ScraperParameters15 where S: CoreScrapperSettings16 {17 protected P Parameters;18 protected ViewPortOptions ViewPortOptions;19 protected LaunchOptions LaunchOptions;20 protected TypeOptions TypeOptions;21 protected Browser Browser;22 protected Page CurrentPage;23 public ILogger Logger { get; set; }24 public IBasicSettingsManager<S> SettingsManager { get; set; }25 public IDataSerializer DataSerializer { get; set; }26 public BaseScraper() { }27 public BaseScraper(ILogger logger,28 IBasicSettingsManager<S> settingsManager,29 IDataSerializer dataSerializer)30 {31 Logger = logger;32 SettingsManager = settingsManager;33 DataSerializer = dataSerializer;34 }35 /// <summary>36 /// Initializes: Settings Manager with settings loaded, saves the parameters and initializes the browser instance37 /// </summary>38 /// <param name="parameters"></param>39 public virtual async Task Initialize(P parameters)40 {41 // initialize settings42 await SettingsManager.LoadSettings();43 var appSettings = SettingsManager.Settings;44 if (appSettings == null)45 throw new ArgumentException("Cannot instantiate scraper without settings loaded.");46 // initialize parameters47 Parameters = parameters;48 // initialize browser49 // prepare launch options50 LaunchOptions = new LaunchOptions() { Headless = appSettings.Headless };51 ViewPortOptions = new ViewPortOptions() { Width = appSettings.WindowWidth, Height = appSettings.WindowHeight };52 TypeOptions = new TypeOptions() { Delay = appSettings.TYPE_DELAY };53 // get chromium reference (not async, downloads it, blocks everything while downloading)54 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultChromiumRevision);55 Browser = await Puppeteer.LaunchAsync(LaunchOptions);56 CurrentPage = await Browser.NewPageAsync();57 await CurrentPage.SetViewportAsync(ViewPortOptions);58 }59 public virtual async Task CloseBrowser()60 {61 await Browser.CloseAsync();62 Browser = null;63 }64 public virtual async Task NavigateToPage(string url, bool force = false)65 {66 var appSettings = SettingsManager.Settings;67 if (force || CurrentPage.Url != appSettings.AppUrl)68 await CurrentPage.GoToAsync(appSettings.AppUrl);69 }70 public virtual async Task ExecuteClick(string selector)71 {...

Full Screen

Full Screen

Program.cs

Source:Program.cs Github

copy

Full Screen

...26 var browser = await Puppeteer.LaunchAsync(launchOptions);27 var page = await browser.NewPageAsync();28 //await page.SetUserAgentAsync("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36");29 //Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3617.0 Safari/537.3630 ViewPortOptions viewPortOptions = new ViewPortOptions();31 viewPortOptions.Width = 800;32 viewPortOptions.Height = 1200;33 await page.SetViewportAsync(viewPortOptions);34 var navigationOption = new NavigationOptions35 {36 WaitUntil = new[]37 {38 WaitUntilNavigation.Load, WaitUntilNavigation.DOMContentLoaded, WaitUntilNavigation.Networkidle239 }40 };41 //await page.GoToAsync("http://192.168.0.107:98/static/index.html#/preview?recordid=059d67fea03a4c3687bc49ac04669968&t=6deb2042ec114ac291fadb2514182352&generate=true", navigationOption);42 await page.GoToAsync("https://zhuangxiu.xxxxxx.com/#/preview?recordid=1095a3231f704b419b8b0676fa8d147e&t=e1859920de894de394c577f802f67167&generate=true", navigationOption);43 44 var o = new ScreenshotOptions { Type = ScreenshotType.Png, FullPage = true };...

Full Screen

Full Screen

AutomationEngineFactory.cs

Source:AutomationEngineFactory.cs Github

copy

Full Screen

...18 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);19 return await PuppeteerSharp.Puppeteer.LaunchAsync(new LaunchOptions20 {21 Headless = false,22 DefaultViewport = new ViewPortOptions()23 {24 Width = 1100,25 Height = 100026 }27 });28 }29 public static Task<IWebDriver> GetEdgeDriverAsync()30 {31 var options = new EdgeOptions() {32 PageLoadStrategy = PageLoadStrategy.Eager,33 UnhandledPromptBehavior = UnhandledPromptBehavior.Accept,34 UseInPrivateBrowsing = true35 };36 var pathsToCheck = new[]...

Full Screen

Full Screen

BrowserProvider.cs

Source:BrowserProvider.cs Github

copy

Full Screen

...26 new ConnectOptions27 {28 BrowserWSEndpoint = Options.BrowserWsEndpoint,29 IgnoreHTTPSErrors = Options.IgnoreHTTPSErrors,30 DefaultViewport = Options.ViewPortOptions31 },32 loggerFactory);33 }34 if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("PUPPETEER_EXECUTABLE_PATH")))35 {36 logger.LogDebug("Download browser");37 using var browserFetcher = new BrowserFetcher(Options.Product);38 if (!string.IsNullOrEmpty(Options.Revision))39 {40 await browserFetcher.DownloadAsync(Options.Revision);41 }42 else43 {44 await browserFetcher.DownloadAsync();45 }46 logger.LogDebug("Browser downloaded");47 }48 logger.LogInformation("Start new Browser");49 return await PuppeteerSharp.Puppeteer.LaunchAsync(50 new LaunchOptions51 {52 Headless = Options.Headless,53 Args = Options.BrowserArgs,54 IgnoreHTTPSErrors = Options.IgnoreHTTPSErrors,55 DefaultViewport = Options.ViewPortOptions56 }, loggerFactory);57 }58}...

Full Screen

Full Screen

HtmlToImage.cs

Source:HtmlToImage.cs Github

copy

Full Screen

...29 var dto = JsonConvert.DeserializeObject<PostHtmlToImageDTO>(requestBody);30//#if true == DEBUG31// await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);32//#endif33 ViewPortOptions defaultViewport = null;34 if (dto.Width.HasValue && dto.Height.HasValue)35 defaultViewport = new ViewPortOptions36 {37 Width = dto.Width.Value,38 Height = dto.Height.Value39 };40 using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions41 {42 Headless = true,43 Args = new[] { "--no-sandbox" },44 DefaultViewport = defaultViewport45 }))46 using (var page = await browser.NewPageAsync())47 {48 var buff = Convert.FromBase64String(dto.HtmlBase64);49 var text = UTF8Encoding.UTF8.GetString(buff);...

Full Screen

Full Screen

MakeTweet.cs

Source:MakeTweet.cs Github

copy

Full Screen

...25 Timeout = 0,26 WaitUntil = new[] {27 WaitUntilNavigation.Networkidle2 }28 };29 ViewPortOptions v = new ViewPortOptions();30 v.Width = 1920;31 v.Height = 1000;32 await page.SetViewportAsync(v);33 await page.GoToAsync("https://twitter.com/", navigation);34 }35 static async Task Main(string[] args)36 {37 await launchBrowser();38 await makeTweetAsync("Hello, I am currently still working on the automation of this account updates on Putin's jet will start soon!");39 }40 public static async Task makeTweetAsync(String str)41 {42 await page.SetCacheEnabledAsync(true);43 await page.SetJavaScriptEnabledAsync(true);...

Full Screen

Full Screen

PuppeteerBrowser.cs

Source:PuppeteerBrowser.cs Github

copy

Full Screen

...7 public class PuppeteerBrowser : IBrowser8 {9 private Browser _browser;10 private Page _page;11 private ViewPortOptions _vpOpts;12 public PuppeteerBrowser(ViewPortOptions viewPortOptions = null)13 {14 _vpOpts = viewPortOptions ?? new ViewPortOptions15 {16 Width = 1920,17 Height = 108018 };19 }20 public async Task Initialize()21 {22 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);23 _browser = await Puppeteer.LaunchAsync(new LaunchOptions24 {25 Headless = true26 });27 _page = await _browser.NewPageAsync();28 await _page.SetViewportAsync(_vpOpts);...

Full Screen

Full Screen

PuppeteerModuleOptions.cs

Source:PuppeteerModuleOptions.cs Github

copy

Full Screen

...5public class PuppeteerModuleOptions : BaseModuleOptions6{7 public string? BrowserWsEndpoint { get; set; }8 public bool IgnoreHTTPSErrors { get; set; }9 [JsonIgnore] public ViewPortOptions ViewPortOptions { get; set; } = ViewPortOptions.Default;10 public string[] BrowserArgs { get; set; } = { "--no-sandbox" };11 public bool Headless { get; set; } = true;12 public Product Product { get; set; } = Product.Chrome;13 public string? Revision { get; set; }14}...

Full Screen

Full Screen

ViewPortOptions

Using AI Code Generation

copy

Full Screen

1{2};3var browser = await Puppeteer.LaunchAsync(new LaunchOptions4{5 Args = new string[] { "--start-maximized" }6});7var page = await browser.NewPageAsync();8await page.SetViewportAsync(options);9await page.ScreenshotAsync("google.png");10{11};12var browser = await Puppeteer.LaunchAsync(new LaunchOptions13{14 Args = new string[] { "--start-maximized" }15});16var page = await browser.NewPageAsync();17await page.SetViewportAsync(options);18await page.ScreenshotAsync("google.png");19{20};21var browser = await Puppeteer.LaunchAsync(new LaunchOptions22{23 Args = new string[] { "--start-maximized" }24});25var page = await browser.NewPageAsync();26await page.SetViewportAsync(options);27await page.ScreenshotAsync("google.png");28{29};30var browser = await Puppeteer.LaunchAsync(new LaunchOptions31{32 Args = new string[] { "--start-maximized" }33});34var page = await browser.NewPageAsync();35await page.SetViewportAsync(options);36await page.ScreenshotAsync("google.png");37{38};39var browser = await Puppeteer.LaunchAsync(new LaunchOptions40{

Full Screen

Full Screen

ViewPortOptions

Using AI Code Generation

copy

Full Screen

1{2};3var browser = await Puppeteer.LaunchAsync(new LaunchOptions4{5 Args = new string[] { "--no-sandbox" }6});7var page = await browser.NewPageAsync();8await page.SetViewportAsync(options);9await page.ScreenshotAsync("google.png");10await browser.CloseAsync();11{12};13var browser = await Puppeteer.LaunchAsync(new LaunchOptions14{15 Args = new string[] { "--no-sandbox" }16});17var page = await browser.NewPageAsync();18await page.SetViewportAsync(options);19await page.ScreenshotAsync("google.png");20await browser.CloseAsync();21{22};23var browser = await Puppeteer.LaunchAsync(new LaunchOptions24{25 Args = new string[] { "--no-sandbox" }26});27var page = await browser.NewPageAsync();28await page.SetViewportAsync(options);29await page.ScreenshotAsync("google.png");30await browser.CloseAsync();31{32};33var browser = await Puppeteer.LaunchAsync(new LaunchOptions34{35 Args = new string[] { "--no-sandbox" }36});37var page = await browser.NewPageAsync();38await page.SetViewportAsync(options);39await page.ScreenshotAsync("google.png");40await browser.CloseAsync();41{42};

Full Screen

Full Screen

ViewPortOptions

Using AI Code Generation

copy

Full Screen

1var browserFetcher = new BrowserFetcher();2await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);3var browser = await Puppeteer.LaunchAsync(new LaunchOptions4{5});6var page = await browser.NewPageAsync();7await page.ScreenshotAsync("google.png");8var browserFetcher = new BrowserFetcher();9await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);10var browser = await Puppeteer.LaunchAsync(new LaunchOptions11{12});13var page = await browser.NewPageAsync();14await page.ScreenshotAsync("google.png");15var browserFetcher = new BrowserFetcher();16await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);17var browser = await Puppeteer.LaunchAsync(new LaunchOptions18{19});20var page = await browser.NewPageAsync();21await page.ScreenshotAsync("google.png");22var browserFetcher = new BrowserFetcher();23await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);24var browser = await Puppeteer.LaunchAsync(new LaunchOptions25{26});27var page = await browser.NewPageAsync();28await page.ScreenshotAsync("google.png");29var browserFetcher = new BrowserFetcher();30await browserFetcher.DownloadAsync(BrowserFetcher.DefaultRevision);31var browser = await Puppeteer.LaunchAsync(new LaunchOptions32{33});34var page = await browser.NewPageAsync();

Full Screen

Full Screen

ViewPortOptions

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 {9 Args = new string[] { "--start-maximized" }10 };11 {12 };13 options.ViewPort = viewPortOptions;14 var browser = await Puppeteer.LaunchAsync(options);15 var page = await browser.NewPageAsync();16 await page.ScreenshotAsync("google.png");17 await browser.CloseAsync();18 }19 }20}21using System;22using System.Threading.Tasks;23using PuppeteerSharp;24{25 {26 static async Task Main(string[] args)27 {28 {

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

Most used methods in ViewPortOptions

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful