Best Puppeteer-sharp code snippet using PuppeteerSharp.Page.EmulateMediaAsync
Page.cs
Source:Page.cs
...554 /// <seealso cref="GoToAsync(string, NavigationOptions)"/>555 public Task<Response> GoToAsync(string url, WaitUntilNavigation waitUntil)556 => GoToAsync(url, new NavigationOptions { WaitUntil = new[] { waitUntil } });557 /// <summary>558 /// generates a pdf of the page with <see cref="MediaType.Print"/> css media. To generate a pdf with <see cref="MediaType.Screen"/> media call <see cref="EmulateMediaAsync(MediaType)"/> with <see cref="MediaType.Screen"/>559 /// </summary>560 /// <param name="file">The file path to save the PDF to. paths are resolved using <see cref="Path.GetFullPath(string)"/></param>561 /// <returns></returns>562 /// <remarks>563 /// Generating a pdf is currently only supported in Chrome headless564 /// </remarks>565 public Task PdfAsync(string file) => PdfAsync(file, new PdfOptions());566 /// <summary>567 /// generates a pdf of the page with <see cref="MediaType.Print"/> css media. To generate a pdf with <see cref="MediaType.Screen"/> media call <see cref="EmulateMediaAsync(MediaType)"/> with <see cref="MediaType.Screen"/>568 /// </summary>569 /// <param name="file">The file path to save the PDF to. paths are resolved using <see cref="Path.GetFullPath(string)"/></param>570 /// <param name="options">pdf options</param>571 /// <returns></returns>572 /// <remarks>573 /// Generating a pdf is currently only supported in Chrome headless574 /// </remarks>575 public async Task PdfAsync(string file, PdfOptions options)576 => await PdfInternalAsync(file, options).ConfigureAwait(false);577 /// <summary>578 /// generates a pdf of the page with <see cref="MediaType.Print"/> css media. To generate a pdf with <see cref="MediaType.Screen"/> media call <see cref="EmulateMediaAsync(MediaType)"/> with <see cref="MediaType.Screen"/>579 /// </summary>580 /// <returns>Task which resolves to a <see cref="Stream"/> containing the PDF data.</returns>581 /// <remarks>582 /// Generating a pdf is currently only supported in Chrome headless583 /// </remarks>584 public Task<Stream> PdfStreamAsync() => PdfStreamAsync(new PdfOptions());585 /// <summary>586 /// Generates a pdf of the page with <see cref="MediaType.Print"/> css media. To generate a pdf with <see cref="MediaType.Screen"/> media call <see cref="EmulateMediaAsync(MediaType)"/> with <see cref="MediaType.Screen"/>587 /// </summary>588 /// <param name="options">pdf options</param>589 /// <returns>Task which resolves to a <see cref="Stream"/> containing the PDF data.</returns>590 /// <remarks>591 /// Generating a pdf is currently only supported in Chrome headless592 /// </remarks>593 public async Task<Stream> PdfStreamAsync(PdfOptions options)594 => new MemoryStream(await PdfDataAsync(options).ConfigureAwait(false));595 /// <summary>596 /// Generates a pdf of the page with <see cref="MediaType.Print"/> css media. To generate a pdf with <see cref="MediaType.Screen"/> media call <see cref="EmulateMediaAsync(MediaType)"/> with <see cref="MediaType.Screen"/>597 /// </summary>598 /// <returns>Task which resolves to a <see cref="byte"/>[] containing the PDF data.</returns>599 /// <remarks>600 /// Generating a pdf is currently only supported in Chrome headless601 /// </remarks>602 public Task<byte[]> PdfDataAsync() => PdfDataAsync(new PdfOptions());603 /// <summary>604 /// Generates a pdf of the page with <see cref="MediaType.Print"/> css media. To generate a pdf with <see cref="MediaType.Screen"/> media call <see cref="EmulateMediaAsync(MediaType)"/> with <see cref="MediaType.Screen"/>605 /// </summary>606 /// <param name="options">pdf options</param>607 /// <returns>Task which resolves to a <see cref="byte"/>[] containing the PDF data.</returns>608 /// <remarks>609 /// Generating a pdf is currently only supported in Chrome headless610 /// </remarks>611 public Task<byte[]> PdfDataAsync(PdfOptions options) => PdfInternalAsync(null, options);612 internal async Task<byte[]> PdfInternalAsync(string file, PdfOptions options)613 {614 var paperWidth = PaperFormat.Letter.Width;615 var paperHeight = PaperFormat.Letter.Height;616 if (options.Format != null)617 {618 paperWidth = options.Format.Width;619 paperHeight = options.Format.Height;620 }621 else622 {623 if (options.Width != null)624 {625 paperWidth = ConvertPrintParameterToInches(options.Width);626 }627 if (options.Height != null)628 {629 paperHeight = ConvertPrintParameterToInches(options.Height);630 }631 }632 var marginTop = ConvertPrintParameterToInches(options.MarginOptions.Top);633 var marginLeft = ConvertPrintParameterToInches(options.MarginOptions.Left);634 var marginBottom = ConvertPrintParameterToInches(options.MarginOptions.Bottom);635 var marginRight = ConvertPrintParameterToInches(options.MarginOptions.Right);636 var result = await Client.SendAsync<PagePrintToPDFResponse>("Page.printToPDF", new PagePrintToPDFRequest637 {638 TransferMode = "ReturnAsStream",639 Landscape = options.Landscape,640 DisplayHeaderFooter = options.DisplayHeaderFooter,641 HeaderTemplate = options.HeaderTemplate,642 FooterTemplate = options.FooterTemplate,643 PrintBackground = options.PrintBackground,644 Scale = options.Scale,645 PaperWidth = paperWidth,646 PaperHeight = paperHeight,647 MarginTop = marginTop,648 MarginBottom = marginBottom,649 MarginLeft = marginLeft,650 MarginRight = marginRight,651 PageRanges = options.PageRanges,652 PreferCSSPageSize = options.PreferCSSPageSize653 }).ConfigureAwait(false);654 return await ProtocolStreamReader.ReadProtocolStreamByteAsync(Client, result.Stream, file).ConfigureAwait(false);655 }656 /// <summary>657 /// Enables/Disables Javascript on the page658 /// </summary>659 /// <returns>Task.</returns>660 /// <param name="enabled">Whether or not to enable JavaScript on the page.</param>661 public Task SetJavaScriptEnabledAsync(bool enabled)662 {663 if (enabled == JavascriptEnabled)664 {665 return Task.CompletedTask;666 }667 JavascriptEnabled = enabled;668 return Client.SendAsync("Emulation.setScriptExecutionDisabled", new EmulationSetScriptExecutionDisabledRequest669 {670 Value = !enabled671 });672 }673 /// <summary>674 /// Toggles bypassing page's Content-Security-Policy.675 /// </summary>676 /// <param name="enabled">sets bypassing of page's Content-Security-Policy.</param>677 /// <returns></returns>678 /// <remarks>679 /// CSP bypassing happens at the moment of CSP initialization rather then evaluation.680 /// Usually this means that <see cref="SetBypassCSPAsync(bool)"/> should be called before navigating to the domain.681 /// </remarks>682 public Task SetBypassCSPAsync(bool enabled) => Client.SendAsync("Page.setBypassCSP", new PageSetBypassCSPRequest683 {684 Enabled = enabled685 });686 /// <summary>687 /// Emulates a media such as screen or print.688 /// </summary>689 /// <returns>Task.</returns>690 /// <param name="media">Media to set.</param>691 [Obsolete("User EmulateMediaTypeAsync instead")]692 public Task EmulateMediaAsync(MediaType media) => EmulateMediaTypeAsync(media);693 /// <summary>694 /// Emulates a media such as screen or print.695 /// </summary>696 /// <param name="type">Media to set.</param>697 /// <example>698 /// <code>699 /// <![CDATA[700 /// await page.EvaluateFunctionAsync<bool>("() => matchMedia('screen').matches)");701 /// // â true702 /// await page.EvaluateFunctionAsync<bool>("() => matchMedia('print').matches)");703 /// // â true704 /// await page.EmulateMediaTypeAsync(MediaType.Print);705 /// await page.EvaluateFunctionAsync<bool>("() => matchMedia('screen').matches)");706 /// // â false...
Program.cs
Source:Program.cs
...45 if (!string.IsNullOrEmpty(options.WaitForSelector))46 {47 await page.WaitForSelectorAsync(options.WaitForSelector);48 }49 await page.EmulateMediaAsync(MediaType.Print);50 var pdfOptions = new PdfOptions51 {52 PrintBackground = options.PrintBackground,53 Landscape = options.Landscape54 };55 await page.PdfAsync(options.SavePath, pdfOptions);56 }57 }58}...
EmulateMediaTests.cs
Source:EmulateMediaTests.cs
...14 public async Task ShouldWork()15 {16 Assert.True(await Page.EvaluateExpressionAsync<bool>("window.matchMedia('screen').matches"));17 Assert.False(await Page.EvaluateExpressionAsync<bool>("window.matchMedia('print').matches"));18 await Page.EmulateMediaAsync(MediaType.Print);19 Assert.False(await Page.EvaluateExpressionAsync<bool>("window.matchMedia('screen').matches"));20 Assert.True(await Page.EvaluateExpressionAsync<bool>("window.matchMedia('print').matches"));21 await Page.EmulateMediaAsync(MediaType.None);22 Assert.True(await Page.EvaluateExpressionAsync<bool>("window.matchMedia('screen').matches"));23 Assert.False(await Page.EvaluateExpressionAsync<bool>("window.matchMedia('print').matches"));24 }25 }26}...
EmulateMediaAsync
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 var browser = await Puppeteer.LaunchAsync(new LaunchOptions9 {10 Args = new string[] { "--start-maximized" }11 });12 var page = await browser.NewPageAsync();13 await page.EmulateMediaAsync(MediaType.Screen);14 await page.ScreenshotAsync("google.png");15 await browser.CloseAsync();16 }17 }18}
EmulateMediaAsync
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);9 var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false });10 var page = await browser.NewPageAsync();11 await page.EmulateMediaAsync(MediaType.Screen);12 Console.ReadLine();13 }14 }15}
EmulateMediaAsync
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 {9 };10 using (var browser = await Puppeteer.LaunchAsync(options))11 {12 var page = await browser.NewPageAsync();13 await page.EmulateMediaAsync(new MediaFeature14 {15 });16 }17 }18 }19}
EmulateMediaAsync
Using AI Code Generation
1using System;2using System.IO;3using System.Threading.Tasks;4using PuppeteerSharp;5{6 {7 static async Task Main(string[] args)8 {9 var browser = await Puppeteer.LaunchAsync(new LaunchOptions10 {11 });12 var page = await browser.NewPageAsync();13 await page.EmulateMediaAsync(MediaType.Screen);14 await page.ScreenshotAsync("screen.png");15 await page.EmulateMediaAsync(MediaType.Print);16 await page.ScreenshotAsync("print.png");17 await browser.CloseAsync();18 }19 }20}
EmulateMediaAsync
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static async Task Main(string[] args)7 {8 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);9 var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = false });10 var page = await browser.NewPageAsync();11 await page.EmulateMediaAsync(MediaType.Screen);12 await page.ScreenshotAsync("screenshot.png");13 await browser.CloseAsync();14 }15 }16}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!