Skip to content
Snippets Groups Projects
Program.cs 4.19 KiB
Newer Older
  • Learn to ignore specific revisions
  • using MasterDevs.ChromeDevTools.Protocol.Chrome.Page;
    
    using System.Threading;
    using MasterDevs.ChromeDevTools.Protocol.Chrome.DOM;
    using MasterDevs.ChromeDevTools.Protocol.Chrome.Emulation;
    
    namespace MasterDevs.ChromeDevTools.Sample
    
    brewdente's avatar
    brewdente committed
        internal class Program
    
            const int ViewPortWidth = 1440;
            const int ViewPortHeight = 900;
    
    brewdente's avatar
    brewdente committed
            private static void Main(string[] args)
    
                    // synchronization
                    var screenshotDone = new ManualResetEventSlim();
    
    
                    var chromeProcessFactory = new ChromeProcessFactory(new StubbornDirectoryCleaner());
    
                    using (var chromeProcess = chromeProcessFactory.Create(9222))
                    {
                        // STEP 2 - Create a debugging session
    
    Georgios Diamantopoulos's avatar
    Georgios Diamantopoulos committed
                        var sessionInfo = (await chromeProcess.GetSessionInfo()).LastOrDefault();
    
    Georgios Diamantopoulos's avatar
    Georgios Diamantopoulos committed
                        var chromeSession = chromeSessionFactory.Create(sessionInfo.WebSocketDebuggerUrl);
    
                        // Here we are sending a commands to tell chrome to set the viewport size 
                        // and navigate to the specified URL
                        await chromeSession.SendAsync(new SetVisibleSizeCommand
                        {
                            Width = ViewPortWidth,
                            Height = ViewPortHeight
                        });
    
                        var navigateResponse = await chromeSession.SendAsync(new NavigateCommand
    
                        {
                            Url = "http://www.google.com"
    
                        Console.WriteLine("NavigateResponse: " + navigateResponse.Id);
    
                        // STEP 4 - Register for events (in this case, "Page" domain events)
    
                        // send an command to tell chrome to send us all Page events
    
                        // but we only subscribe to certain events in this session
    
                        var pageEnableResult = await chromeSession.SendAsync<Protocol.Chrome.Page.EnableCommand>();
    
    
                        chromeSession.Subscribe<LoadEventFiredEvent>(loadEventFired =>
    
                            // we cannot block in event handler, hence the task
                            Task.Run(async () =>
                            {
                                Console.WriteLine("LoadEventFiredEvent: " + loadEventFired.Timestamp);
    
    
                                var documentNodeId = (await chromeSession.SendAsync(new GetDocumentCommand(), CancellationToken.None)).Result.Root.NodeId;
    
                                    (await chromeSession.SendAsync(new QuerySelectorCommand
                                    {
                                        NodeId = documentNodeId,
                                        Selector = "body"
                                    })).Result.NodeId;
                                var height = (await chromeSession.SendAsync(new GetBoxModelCommand {NodeId = bodyNodeId})).Result.Model.Height;
    
    
                                await chromeSession.SendAsync(new SetVisibleSizeCommand {Width = ViewPortWidth, Height = height});
    
                                Console.WriteLine("Taking screenshot");
    
                                var screenshot = await chromeSession.SendAsync(new CaptureScreenshotCommand {Format = "png"});
    
    
                                var data = Convert.FromBase64String(screenshot.Result.Data);
                                File.WriteAllBytes("output.png", data);
                                Console.WriteLine("Screenshot stored");
    
                                // tell the main thread we are done
                                screenshotDone.Set();
                            });
    
                        // wait for screenshoting thread to (start and) finish
                        screenshotDone.Wait();
    
                        Console.WriteLine("Exiting ..");
    
    brewdente's avatar
    brewdente committed
    }