How to use AddTagOptions class of PuppeteerSharp package

Best Puppeteer-sharp code snippet using PuppeteerSharp.AddTagOptions

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

PdfRequest.cs

Source: PdfRequest.cs Github

copy
1using System.Collections.Generic;
2using PuppeteerSharp;
3using PuppeteerSharp.Media;
4
5namespace AbdusCo.Matbaa.Abstractions
6{
7    public class PdfRequest
8    {
9        public static readonly PdfOptions DefaultPdfOptions = new PdfOptions()
10        {
11            Format = PaperFormat.A4,
12            DisplayHeaderFooter = false,
13            PrintBackground = true,
14            PreferCSSPageSize = true,
15        };
16
17        /// <summary>
18        /// HTML to create PDF from
19        /// </summary>
20        public string Html { get; set; }
21
22        public PageOptions? Options { get; set; }
23
24        /// <summary>
25        /// Puppeteer compatible PDF options. See [puppeteer documentation](https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#pagepdfoptions)
26        /// </summary>
27        public PdfOptions? PdfOptions { get; set; }
28
29        public class PageOptions
30        {
31            /// <summary>
32            /// Enable Javascript evaluation on the page
33            /// </summary>
34            public bool EnableJavascript { get; set; } = true;
35
36            /// <summary>
37            /// Add script tags. See [puppeteer docs](https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#frameaddscripttagoptions)
38            /// You can also add script tags in HTML
39            /// </summary>
40            public List<AddTagOptions>? AddScriptTags { get; set; }
41
42            /// <summary>
43            /// Add style tags. See [puppeteer docs](https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#frameaddstyletagoptions)
44            /// You can also add style tags in HTML
45            /// </summary>
46            public List<AddTagOptions>? AddStyleTags { get; set; }
47        }
48    }
49}
Full Screen

ChromeClient.cs

Source: ChromeClient.cs Github

copy
1using KKBoxCD.Core.Utils;
2using KKBoxCD.Properties;
3using PuppeteerExtraSharp;
4using PuppeteerSharp;
5using System;
6
7namespace KKBoxCD.Core
8{
9    class ChromeClient: IDisposable
10    {
11        #region Dispose
12
13        private bool _Disposed = false;
14
15        protected virtual void Dispose(bool disposing)
16        {
17            if (_Disposed)
18            {
19                return;
20            }
21
22            if (disposing)
23            {
24                if (Page != null)
25                {
26                    try { Page.CloseAsync().Wait(); } catch { }
27                    Page = null;
28                }
29                if (Browser != null)
30                {
31                    try { Browser.CloseAsync().Wait(); } catch { }
32                    Browser = null;
33                }
34            }
35            _Disposed = true;
36        }
37
38        public void Dispose()
39        {
40            Dispose(true);
41            GC.SuppressFinalize(this);
42        }
43
44        ~ChromeClient()
45        {
46            Dispose(false);
47        }
48
49        #endregion
50
51        #region Singleton
52
53        private static readonly Lazy<ChromeClient> Singleton = new Lazy<ChromeClient>(() => new ChromeClient());
54
55        public static ChromeClient Instance => Singleton.Value;
56
57        #endregion
58
59        private Config mConfig;
60
61        public bool IsReady { get; private set; }
62
63        public Browser Browser { get; private set; }
64
65        public Page Page { get; private set; }
66
67        protected ChromeClient()
68        {
69            mConfig = Config.Instance;
70        }
71
72        public ChromeClient Init()
73        {
74            IsReady = false;
75            if (Page != null)
76            {
77                try
78                {
79                    Page.CloseAsync().Wait();
80                    Page.Dispose();
81                }
82                catch { }
83                Page = null;
84            }
85            if (Browser != null)
86            {
87                try
88                {
89                    Browser.CloseAsync().Wait();
90                    Browser.Dispose();
91                }
92                catch { }
93                Browser = null;
94            }
95
96            PuppeteerExtra extra = new PuppeteerExtra();
97            LaunchOptions options = new LaunchOptions()
98            {
99                Headless = true,
100                ExecutablePath = Consts.ChromeFile,
101                Args = new string[]
102                {
103                    "--app=\"data:text/html,<title>Chrome Client</title>\"",
104                    "--window-size=270,150",
105                    "--allow-cross-origin-auth-prompt",
106                    "--disable-web-security",
107                    "--disable-sync",
108                    "--disable-translate",
109                    "--disable-backgrounding-occluded-windows",
110                    "--disable-background-networking",
111                    "--disable-client-side-phishing-detection",
112                    "--disable-dev-shm-usage",
113                    "--disable-breakpad",
114                    "--disable-domain-reliability",
115                    "--disable-features=HardwareMediaKeyHandling,OmniboxUIExperimentHideSteadyStateUrlPathQueryAndRef,OmniboxUIExperimentHideSteadyStateUrlScheme,OmniboxUIExperimentHideSteadyStateUrlTrivialSubdomains,ShowManagedUi",
116                    "--disable-hang-monitor",
117                    "--disable-ipc-flooding-protection",
118                    "--disable-notifications",
119                    "--disable-offer-store-unmasked-wallet-cards",
120                    "--disable-popup-blocking",
121                    "--disable-print-preview",
122                    "--disable-prompt-on-repost",
123                    "--disable-remote-fonts",
124                    "--disable-default-apps",
125                    "--disable-image-loading",
126                    "--disable-speech-api",
127                    "--hide-scrollbars",
128                    "--ignore-certificate-errors",
129                    "--ignore-gpu-blacklist",
130                    "--metrics-recording-only",
131                    "--no-default-browser-check",
132                    "--no-first-run",
133                    "--no-pings",
134                    "--no-sandbox",
135                    "--no-zygote",
136                    "--disable-gpu",
137                    "--password-store=basic",
138                    "--reset-variation-state",
139                    "--use-mock-keychain",
140                },
141                DefaultViewport = null
142            };
143            Browser = extra.LaunchAsync(options).Result;
144            Page = Browser.PagesAsync().Result[0];
145
146            Page.AddScriptTagAsync(new AddTagOptions
147            {
148                Content = Resources.base64
149            }).Wait();
150            Page.AddScriptTagAsync(new AddTagOptions
151            {
152                Content = Resources.hashes_min
153            }).Wait();
154            Page.AddScriptTagAsync(new AddTagOptions
155            {
156                Content = Resources.jsbn
157            }).Wait();
158            Page.AddScriptTagAsync(new AddTagOptions
159            {
160                Content = Resources.jsbn2
161            }).Wait();
162            Page.AddScriptTagAsync(new AddTagOptions
163            {
164                Content = Resources.scrypt
165            }).Wait();
166            Page.AddScriptTagAsync(new AddTagOptions
167            {
168                Content = Resources.srp
169            }).Wait();
170            Page.AddScriptTagAsync(new AddTagOptions
171            {
172                Content = Resources.chrome_client
173            }).Wait();
174
175            IsReady = true;
176            return this;
177        }
178    }
179}
Full Screen

AddTagOptions.cs

Source: AddTagOptions.cs Github

copy
1namespace PuppeteerSharp
2{
3    /// <summary>
4    /// Options used by <see cref="Page.AddScriptTagAsync(AddTagOptions)"/> &amp; <see cref="Page.AddStyleTagAsync(AddTagOptions)"/>
5    /// </summary>
6    public class AddTagOptions
7    {
8        /// <summary>
9        /// Url of a script to be added
10        /// </summary>
11        public string Url { get; set; }
12
13        /// <summary>
14        /// Path to the JavaScript file to be injected into frame. If its a relative path, then it is resolved relative to <see cref="System.IO.Directory.GetCurrentDirectory"/>
15        /// </summary>
16        public string Path { get; set; }
17
18        /// <summary>
19        /// Raw JavaScript content to be injected into frame
20        /// </summary>
21        public string Content { get; set; }
22    }
23}
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 AddTagOptions

    No methods found for this class ­čś×

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)