How to use Credentials class of PuppeteerSharp package

Best Puppeteer-sharp code snippet using PuppeteerSharp.Credentials

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

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

GistGithubService.cs

Source: GistGithubService.cs Github

copy
1using Octokit;
2using Patrick.Helpers;
3using Patrick.Models;
4using System;
5using System.Linq;
6using System.Threading.Tasks;
7
8namespace Patrick.Services.Implementation
9{
10    class GistGithubService : IGistGithubService
11    {
12        private readonly GitHubClient client =
13            new GitHubClient(new ProductHeaderValue("Patrick-Star-Helper"));
14        private readonly PuppeteerSharp.BrowserFetcher browserFetcher =
15            new PuppeteerSharp.BrowserFetcher();
16
17        private readonly GitHubModel gitHubModel;
18
19        public bool IsAuthenticated { get; private set; }
20
21        private readonly ICredentialStore credentialStore;
22
23        public GistGithubService(
24            IAppConfigProvider configProvider,
25            ICredentialStore credentialStore)
26        {
27            this.credentialStore = credentialStore;
28
29            gitHubModel = configProvider.Configuration.GitHub!;
30            browserFetcher.DownloadProgressChanged += Fetcher_DownloadProgressChanged;
31        }
32
33        private void Fetcher_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)
34        {
35            Console.WriteLine("Downloading Chromium: {0}%", e.ProgressPercentage);
36        }
37
38        public async Task<bool> Authenticate()
39        {
40            var accessToken = await credentialStore.LoadAccessToken();
41
42            //if (!string.IsNullOrEmpty(accessToken))
43            //{
44            //    IsAuthenticated = true;
45            //    client.Credentials = new Credentials(accessToken);
46            //    return true;
47            //}
48
49            var loginRequest = new OauthLoginRequest(gitHubModel.ClientId);
50            foreach (var scope in gitHubModel.Scopes!)
51                loginRequest.Scopes.Add(scope);
52
53            var redirectPage = client.Oauth.GetGitHubLoginUrl(loginRequest);
54            client.Credentials = await FetchGitCredential(redirectPage);
55
56            await credentialStore.StoreAccessToken(client.Credentials.GetToken());
57
58            IsAuthenticated = true;
59
60            return true;
61        }
62
63        public async Task<string?> Create(GistModel gist)
64        {
65            try
66            {
67                var result = await client.Gist.Create(new NewGist
68                {
69                    Files =
70                    {
71                        [gist.Name] = gist.Content
72                    },
73                    Description = gist.Description
74                });
75
76                return result?.Id;
77            }
78            catch (Exception ex)
79            {
80                var msg = ex.Message;
81                throw;
82            }
83        }
84
85        public async Task<string?> Update(string id, GistModel gist)
86        {
87            var result = await client.Gist.Edit(id, new GistUpdate
88            {
89                Files =
90                {
91                    [gist.Name] = new GistFileUpdate
92                    {
93                        Content = gist.Content
94                    }
95                },
96                Description = gist.Description
97            });
98
99            return result?.Id;
100        }
101
102        public async Task<GistModel?> Find(string id)
103        {
104            var result = await client.Gist.Get(id);
105            if (result != null)
106            {
107                var firstEntry = result.Files.FirstOrDefault();
108                return new GistModel(firstEntry.Value.Filename, firstEntry.Value.Content)
109                {
110
111                };
112            }
113
114            return null;
115        }
116
117        private async Task<Credentials> FetchGitCredential(Uri redirectPage)
118        {
119            var targetUrl = await GetRedirectCallbackResult(redirectPage);
120            var queryString = QueryStringHelper.ToDictionary(targetUrl);
121            var code = queryString[gitHubModel.TargetRedirectKey!];
122            var result = await client.Oauth.CreateAccessToken(
123                new OauthTokenRequest(gitHubModel.ClientId, gitHubModel.ClientSecret, code));
124
125            return new Credentials(result.AccessToken);
126        }
127
128        private async Task<Uri> GetRedirectCallbackResult(Uri redirectPage)
129        {
130            var revisionInfo = await browserFetcher
131                .DownloadAsync(PuppeteerSharp.BrowserFetcher.DefaultRevision);
132            using var browser = await PuppeteerSharp.Puppeteer.LaunchAsync(new PuppeteerSharp.LaunchOptions
133            {
134                Headless = false,
135                LogProcess = true,
136                DumpIO = true
137            });
138
139            var page = await browser.NewPageAsync();
140            var redirectResult = await page.GoToAsync(redirectPage.AbsoluteUri);
141            var target = await browser.WaitForTargetAsync(e => e.Url.Contains(gitHubModel.RedirectUrl!));
142
143            return new Uri(target.Url);
144        }
145    }
146}
147
Full Screen

Program.cs

Source: Program.cs Github

copy
1using PuppeteerSharp;
2using System;
3using System.Collections.Generic;
4using System.Text;
5using System.Threading.Tasks;
6
7
8namespace TestPuppeteerSharp
9{
10    class Program
11    {
12        //cd "C:\Users\UserName\Documents\GitHub\TestPuppeteerSharp\TestPuppeteerSharp\bin\Debug\netcoreapp2.1\.local-chromium\Win64-641577\chrome-win"
13        //start chrome.exe --remote-debugging-port=9222 --user-data-dir=remote-profile
14        //curl localhost:9222/json/version
15        //public static string wsChromeEndpointurl = "ws://localhost:9222/devtools/page/C68E3CBEA9CAA22965B3C1BAD26814F2";
16        //public static string wsChromeEndpointurl = "ws://localhost:9222/devtools/page/4F737F7723F448296F0D7399013056EE";
17        public static string wsChromeEndpointurl = "ws://localhost:9222/devtools/browser/f1a2cc63-57d1-42d2-9a5f-78886eec9da1";
18
19        static async Task Main(string[] args)
20        {
21            Console.WriteLine("Program Main");
22            await TestUber();
23            Console.WriteLine("Press any key to continue...");
24            Console.ReadLine();
25            Console.WriteLine("Program Main End");
26        }
27
28        public static async Task TestUber()
29        {
30
31
32            Console.WriteLine("Downloading chromium");
33
34            Console.WriteLine("Downloading browser");
35            await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
36            Console.WriteLine("Downloading browser end");
37            //Console.WriteLine("Navigating to developers.google.com");
38
39            var LaunchOptions = new LaunchOptions
40            {
41                Headless = false
42
43            };
44
45            var ConnectOptions = new ConnectOptions()
46            {
47                BrowserWSEndpoint = wsChromeEndpointurl
48            };
49
50            //var url = "https://www.google.com/";
51
52            //await Puppeteer.LaunchAsync(LaunchOptions);
53
54            //using (var browser = await Puppeteer.LaunchAsync(LaunchOptions))
55            using (var browser = await PuppeteerSharp.Puppeteer.ConnectAsync(ConnectOptions))
56            using (var page = await browser.NewPageAsync())
57            {
58                Uber.page = page;
59                var user = Credentials.Uber.user;
60                var password = Credentials.Uber.password;
61                await Uber.Login(user, password);
62            }
63        }
64
65        public static async Task TestBanorte ()
66        {
67            var token = Console.ReadLine();
68            var user = Credentials.Banorte.user;
69            var password = Credentials.Banorte.password;
70            await Banorte.Login(user, password, token);
71
72        }
73    }
74    //Util
75    //https://github.com/checkly/puppeteer-recorder
76    //Puppeteer recorder is a Chrome extension that records your browser interactions and generates a Puppeteer script.
77    //https://medium.com/@jaredpotter1/connecting-puppeteer-to-existing-chrome-window-8a10828149e0
78    //Connecting Puppeteer to Existing Chrome Window w/ reCAPTCHA
79    //curl localhost:9222/json
80    //https://www.igvita.com/2012/04/09/driving-google-chrome-via-websocket-api/
81    //http://localhost:9222/
82    //https://blog.chromium.org/2011/05/remote-debugging-with-chrome-developer.html
83}
84
Full Screen

Methods.cs

Source: Methods.cs Github

copy
1using Yove.Proxy;
2using PuppeteerExtraSharp;
3using PuppeteerExtraSharp.Plugins.ExtraStealth;
4using PuppeteerSharp;
5using RuriLib.Attributes;
6using RuriLib.Logging;
7using RuriLib.Models.Bots;
8using System;
9using System.Collections.Generic;
10using System.Linq;
11using System.Threading.Tasks;
12
13using ProxyType = RuriLib.Models.Proxies.ProxyType;
14using RuriLib.Helpers;
15
16namespace RuriLib.Blocks.Puppeteer.Browser
17{
18    [BlockCategory("Browser", "Blocks for interacting with a puppeteer browser", "#e9967a")]
19    public static class Methods
20    {
21        [Block("Opens a new puppeteer browser", name = "Open Browser")]
22        public static async Task PuppeteerOpenBrowser(BotData data, string extraCmdLineArgs = "")
23        {
24            data.Logger.LogHeader();
25
26            // Check if there is already an open browser
27            var oldBrowser = data.TryGetObject<PuppeteerSharp.Browser>("puppeteer");
28            if (oldBrowser is not null && !oldBrowser.IsClosed)
29            {
30                data.Logger.Log("The browser is already open, close it if you want to open a new browser", LogColors.DarkSalmon);
31                return;
32            }
33
34            var args = data.ConfigSettings.BrowserSettings.CommandLineArgs;
35
36            // Extra command line args (to have dynamic args via variables)
37            if (!string.IsNullOrWhiteSpace(extraCmdLineArgs))
38            {
39                args += ' ' + extraCmdLineArgs;
40            }
41
42            // If it's running in docker, currently it runs under root, so add the --no-sandbox otherwise chrome won't work
43            if (Utils.IsDocker())
44            {
45                args += " --no-sandbox";
46            }
47
48            if (data.Proxy != null && data.UseProxy)
49            {
50                if (data.Proxy.Type == ProxyType.Http || !data.Proxy.NeedsAuthentication)
51                {
52                    args += $" --proxy-server={data.Proxy.Type.ToString().ToLower()}://{data.Proxy.Host}:{data.Proxy.Port}";
53                }
54                else
55                {
56                    var proxyType = data.Proxy.Type == ProxyType.Socks5 ? Yove.Proxy.ProxyType.Socks5 : Yove.Proxy.ProxyType.Socks4;
57                    var proxyClient = new ProxyClient(
58                        data.Proxy.Host, data.Proxy.Port,
59                        data.Proxy.Username, data.Proxy.Password, 
60                        proxyType);
61                    data.SetObject("puppeteer.yoveproxy", proxyClient);
62                    args += $" --proxy-server={proxyClient.GetProxy(null).Authority}";
63                }
64            }
65
66            // Configure the options
67            var launchOptions = new LaunchOptions
68            {
69                Args = new string[] { args },
70                ExecutablePath = data.Providers.PuppeteerBrowser.ChromeBinaryLocation,
71                IgnoredDefaultArgs = new string[] { "--disable-extensions", "--enable-automation" },
72                Headless = data.ConfigSettings.BrowserSettings.Headless,
73                DefaultViewport = null // This is important
74            };
75
76            // Add the plugins
77            var extra = new PuppeteerExtra();
78            extra.Use(new StealthPlugin());
79
80            // Launch the browser
81            var browser = await extra.LaunchAsync(launchOptions);
82            browser.IgnoreHTTPSErrors = data.ConfigSettings.BrowserSettings.IgnoreHttpsErrors;
83
84            // Save the browser for further use
85            data.SetObject("puppeteer", browser);
86            var page = (await browser.PagesAsync()).First();
87            SetPageAndFrame(data, page);
88            await SetPageLoadingOptions(data, page);
89
90            // Authenticate if the proxy requires auth
91            if (data.UseProxy && data.Proxy is { NeedsAuthentication: true, Type: ProxyType.Http } proxy)
92                await page.AuthenticateAsync(new Credentials { Username = proxy.Username, Password = proxy.Password });
93
94            data.Logger.Log($"{(launchOptions.Headless ? "Headless " : "")}Browser opened successfully!", LogColors.DarkSalmon);
95        }
96
97        [Block("Closes an open puppeteer browser", name = "Close Browser")]
98        public static async Task PuppeteerCloseBrowser(BotData data)
99        {
100            data.Logger.LogHeader();
101
102            var browser = GetBrowser(data);
103            await browser.CloseAsync();
104            StopYoveProxyInternalServer(data);
105            data.Logger.Log("Browser closed successfully!", LogColors.DarkSalmon);
106        }
107
108        [Block("Opens a new page in a new browser tab", name = "New Tab")]
109        public static async Task PuppeteerNewTab(BotData data)
110        {
111            data.Logger.LogHeader();
112
113            var browser = GetBrowser(data);
114            var page = await browser.NewPageAsync();
115            await SetPageLoadingOptions(data, page);
116
117            SetPageAndFrame(data, page); // Set the new page as active
118            data.Logger.Log($"Opened a new page", LogColors.DarkSalmon);
119        }
120
121        [Block("Closes the currently active browser tab", name = "Close Tab")]
122        public static async Task PuppeteerCloseTab(BotData data)
123        {
124            data.Logger.LogHeader();
125
126            var browser = GetBrowser(data);
127            var page = GetPage(data);
128            
129            // Close the page
130            await page.CloseAsync();
131            
132            // Set the first page as active
133            page = (await browser.PagesAsync()).FirstOrDefault();
134            SetPageAndFrame(data, page);
135
136            if (page != null)
137                await page.BringToFrontAsync();
138
139            data.Logger.Log($"Closed the active page", LogColors.DarkSalmon);
140        }
141
142        [Block("Switches to the browser tab with a specified index", name = "Switch to Tab")]
143        public static async Task PuppeteerSwitchToTab(BotData data, int index)
144        {
145            data.Logger.LogHeader();
146
147            var browser = GetBrowser(data);
148            var page = (await browser.PagesAsync())[index];
149            await page.BringToFrontAsync();
150            SetPageAndFrame(data, page);
151
152            data.Logger.Log($"Switched to tab with index {index}", LogColors.DarkSalmon);
153        }
154
155        [Block("Reloads the current page", name = "Reload")]
156        public static async Task PuppeteerReload(BotData data)
157        {
158            data.Logger.LogHeader();
159
160            var page = GetPage(data);
161            await page.ReloadAsync();
162            SwitchToMainFramePrivate(data);
163
164            data.Logger.Log($"Reloaded the page", LogColors.DarkSalmon);
165        }
166
167        [Block("Goes back to the previously visited page", name = "Go Back")]
168        public static async Task PuppeteerGoBack(BotData data)
169        {
170            data.Logger.LogHeader();
171
172            var page = GetPage(data);
173            await page.GoBackAsync();
174            SwitchToMainFramePrivate(data);
175
176            data.Logger.Log($"Went back to the previously visited page", LogColors.DarkSalmon);
177        }
178
179        [Block("Goes forward to the next visited page", name = "Go Forward")]
180        public static async Task PuppeteerGoForward(BotData data)
181        {
182            data.Logger.LogHeader();
183
184            var page = GetPage(data);
185            await page.GoForwardAsync();
186            SwitchToMainFramePrivate(data);
187
188            data.Logger.Log($"Went forward to the next visited page", LogColors.DarkSalmon);
189        }
190
191        private static PuppeteerSharp.Browser GetBrowser(BotData data)
192            => data.TryGetObject<PuppeteerSharp.Browser>("puppeteer") ?? throw new Exception("The browser is not open!");
193
194        private static PuppeteerSharp.Page GetPage(BotData data)
195            => data.TryGetObject<PuppeteerSharp.Page>("puppeteerPage") ?? throw new Exception("No pages open!");
196
197        private static void SwitchToMainFramePrivate(BotData data)
198            => data.SetObject("puppeteerFrame", GetPage(data).MainFrame);
199
200        private static void SetPageAndFrame(BotData data, PuppeteerSharp.Page page)
201        {
202            data.SetObject("puppeteerPage", page, false);
203            SwitchToMainFramePrivate(data);
204        }
205
206        private static void StopYoveProxyInternalServer(BotData data)
207            => data.TryGetObject<ProxyClient>("puppeteer.yoveproxy")?.Dispose();
208
209        private static async Task SetPageLoadingOptions(BotData data, PuppeteerSharp.Page page)
210        {
211            await page.SetRequestInterceptionAsync(true);
212            page.Request += (sender, e) =>
213            {
214                // If we only want documents and scripts but the resource is not one of those, block
215                if (data.ConfigSettings.BrowserSettings.LoadOnlyDocumentAndScript && 
216                    e.Request.ResourceType != ResourceType.Document && e.Request.ResourceType != ResourceType.Script)
217                {
218                    e.Request.AbortAsync();
219                }
220
221                // If the url contains one of the blocked urls
222                else if (data.ConfigSettings.BrowserSettings.BlockedUrls
223                    .Where(u => !string.IsNullOrWhiteSpace(u))
224                    .Any(u => e.Request.Url.Contains(u, StringComparison.OrdinalIgnoreCase)))
225                {
226                    e.Request.AbortAsync();
227                }
228
229                // Otherwise all good, continue
230                else
231                {
232                    e.Request.ContinueAsync();
233                }
234            };
235
236            if (data.ConfigSettings.BrowserSettings.DismissDialogs)
237            {
238                page.Dialog += (sender, e) =>
239                {
240                    data.Logger.Log($"Dialog automatically dismissed: {e.Dialog.Message}", LogColors.DarkSalmon);
241                    e.Dialog.Dismiss();
242                };
243            }
244        }
245    }
246}
247
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 Credentials

    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)