Exemplo n.º 1
0
        // This is executed on Awesomium's thread.
        static void TakeSnapshots(WebView view, int messageLine, bool exit = true)
        {
            if (!view.IsLive)
            {
                // Dispose the view.
                view.Dispose();
                return;
            }

            // Get the hostname. If it's empty, it must be our JS Console that saves.
            string host = String.IsNullOrEmpty(view.Source.Host) ? "JS" : view.Source.Host;

            // A BitmapSurface is assigned by default to all WebViews.
            BitmapSurface surface = (BitmapSurface)view.Surface;
            // Build a name for the saved image.
            string imageFile = String.Format("{0}.{1:yyyyMMddHHmmss}.png", host, DateTime.Now);

            // Save the buffer to a PNG image.
            surface.SaveToPNG(imageFile, true);
            // Print message.
            ReplaceLine(String.Format("Saved: {0}", imageFile), messageLine + 2);

            // Check if we can execute JavaScript
            // against the DOM.
            if (!view.IsDocumentReady)
            {
                // Print message.
                ReplaceLine("DOM not available.", messageLine + 1);

                if (!exit)
                {
                    return;
                }

                // Dispose the view.
                view.Dispose();
                savingSnapshots--;
                return;
            }

            // We demonstrate resizing to full height.
            ReplaceLine("Attempting to resize to full height... ", messageLine + 1);

            // This JS call will normally return the full height
            // of the page loaded.
            int docHeight = (int)view.ExecuteJavascriptWithResult(PAGE_HEIGHT_FUNC);

            // ExecuteJavascriptWithResult is a synchronous call. Synchronous
            // calls may fail. We check for errors that may occur. Note that
            // if you often get a Error.TimedOut, you may need to set the
            // IWebView.SynchronousMessageTimeout property to a higher value
            // (the default is 800ms).
            Error lastError = view.GetLastError();

            // Report errors.
            if (lastError != Error.None)
            {
                ReplaceLine(String.Format("Error: {0} occurred while getting the page's height.", lastError), messageLine + 1);
            }

            // Exit if the operation failed or the height is 0.
            if (docHeight == 0)
            {
                return;
            }

            // No more content to display.
            if (docHeight == view.Height)
            {
                // Print message.
                ReplaceLine("Full height already loaded.", messageLine + 1);

                if (!exit)
                {
                    return;
                }

                // Dispose the view.
                view.Dispose();
                savingSnapshots--;
                return;
            }

            // All predefined surfaces of Awesomium.NET,
            // support resizing. Here is a demonstration.
            surface.Resized += (s, e) =>
            {
                // Print message.
                ReplaceLine("Surface Resized", messageLine + 1);
                // Build a name for the saved image.
                string fullImageFile = String.Format("{0}.{1:yyyyMMddHHmmss}.full.png", host, DateTime.Now);
                // Save the updated buffer to a new PNG image.
                surface.SaveToPNG(fullImageFile, true);
                // Print message.
                ReplaceLine(String.Format("Saved: {0}", fullImageFile), messageLine + 2);

                // Get Awesomium's synchronization context. You can only
                // acquire this from Awesomium's thread, but you can then
                // cache it or pass it to another thread to be used for
                // safe cross-thread calls to Awesomium. Here we just
                // demonstrate using Post to postpone the view's destruction
                // till the next update pass of the WebCore.
                SynchronizationContext syncCtx = SynchronizationContext.Current;

                // Check if a valid SynchronizationContext is available.
                if ((syncCtx != null) && (syncCtx.GetType() != typeof(SynchronizationContext)))
                {
                    // Queue the destruction of the view. Code execution
                    // will resume immediately (so we exit the event handler),
                    // and the anonymous handler will be executed in the
                    // next update pass.
                    syncCtx.Post((v) =>
                    {
                        WebView completedView = (WebView)v;

                        if (!exit)
                        {
                            return;
                        }

                        // Dispose the view.
                        completedView.Dispose();
                        savingSnapshots--;
                    }, view);
                }
            };

            // Call resize on the view. This will resize
            // and update the surface.
            view.Resize(view.Width, docHeight);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            // Initialize the WebCore with default confiuration settings.
            WebCore.Initialize(new WebConfig()
            {
                LogPath = Environment.CurrentDirectory, LogLevel = LogLevel.Verbose
            });

            // We demonstrate an easy way to hide the scrollbars by providing
            // custom CSS. Read more about how to style the scrollbars here:
            // http://www.webkit.org/blog/363/styling-scrollbars/.
            // Just consider that this setting is WebSession-wide. If you want to apply
            // a similar effect for single pages, you can use ExecuteJavascript
            // and pass: document.documentElement.style.overflow = 'hidden';
            // (Unfortunately WebKit's scrollbar does not have a DOM equivalent yet)
            using (WebSession session = WebCore.CreateWebSession(new WebPreferences()
            {
                CustomCSS = "::-webkit-scrollbar { visibility: hidden; }"
            }))
            {
                // WebView implements IDisposable. Here we demonstrate
                // wrapping it in a using statement.
                using (WebView view = WebCore.CreateWebView(1280, 960, session))
                {
                    bool finishedLoading = false;

                    Console.WriteLine("Loading: http://www.awesomium.com ...");

                    view.LoadURL(new Uri("http://www.awesomium.com"));
                    view.LoadingFrameComplete += (s, e) =>
                    {
                        Console.WriteLine(String.Format("Frame Loaded: {0}", e.FrameID));

                        // The main frame always finishes loading last for a given page load.
                        if (e.IsMainFrame)
                        {
                            finishedLoading = true;
                        }
                    };

                    while (!finishedLoading)
                    {
                        Thread.Sleep(100);
                        // A Console application does not have a synchronization
                        // context, thus auto-update won't be enabled on WebCore.
                        // We need to manually call Update here.
                        WebCore.Update();
                    }

                    // Print some more information.
                    Console.WriteLine(String.Format("Page Title: {0}", view.Title));
                    Console.WriteLine(String.Format("Loaded URL: {0}", view.Source));

                    // A BitmapSurface is assigned by default to all WebViews.
                    BitmapSurface surface = (BitmapSurface)view.Surface;
                    // Save the buffer to a PNG image.
                    surface.SaveToPNG("result.png", true);
                } // Destroy and dispose the view.
            }     // Release and dispose the session.

            // Announce.
            Console.Write("Hit any key to see the result...");
            Console.ReadKey(true);

            // Start the application associated with .png files
            // and display the file.
            Process.Start("result.png");

            // Shut down Awesomium before exiting.
            WebCore.Shutdown();
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            //AutomationSample main = new AutomationSample();
            //main.update();

            WebConfig config = WebConfig.Default;

            WebCore.Initialize(config);
            Uri url = new Uri("http://www.google.com");

            WebPreferences prefs = WebPreferences.Default;

            //prefs.ProxyConfig = "198.1.99.26:3128";
            //prefs.CustomCSS = "body { overflow:hidden; }";
            //prefs.WebSecurity = false;
            //prefs.DefaultEncoding = "UTF-8";

            using (WebSession session = WebCore.CreateWebSession(prefs))
            {
                // WebView implements IDisposable. Here we demonstrate
                // wrapping it in a using statement.
                using (WebView webView = WebCore.CreateWebView(1366, 768, session, WebViewType.Offscreen))
                {
                    bool finishedLoading  = false;
                    bool finishedResizing = false;

                    Console.WriteLine(String.Format("Loading: {0} ...", url));

                    // Load a URL.
                    webView.Source = url;

                    // This event is fired when a frame in the
                    // page finished loading.
                    webView.LoadingFrameComplete += (s, e) =>
                    {
                        Console.WriteLine(String.Format("Frame Loaded: {0}", e.FrameId));

                        // The main frame usually finishes loading last for a given page load.
                        if (e.IsMainFrame)
                        {
                            finishedLoading = true;
                        }
                    };

                    while (!finishedLoading)
                    {
                        Thread.Sleep(100);
                        // A Console application does not have a synchronization
                        // context, thus auto-update won't be enabled on WebCore.
                        // We need to manually call Update here.
                        WebCore.Update();
                    }

                    // Print some more information.
                    Console.WriteLine(String.Format("Page Title: {0}", webView.Title));
                    Console.WriteLine(String.Format("Loaded URL: {0}", webView.Source));
                    //webView.Render().SaveToPNG("result.png", true);
                    //System.Diagnostics.Process.Start("result.png");
                    BitmapSurface surface = (BitmapSurface)webView.Surface;
                    surface.SaveToPNG("result.png", true);
                    System.Diagnostics.Process.Start("result.png");
                } // Destroy and dispose the view.
            }     // Release and dispose the session.

            // Shut down Awesomium before exiting.
            WebCore.Shutdown();

            Console.WriteLine("Press any key to exit...");
            Console.Read();
        }
Exemplo n.º 4
0
        static void TakeSnapshots(WebView view)
        {
            // A BitmapSurface is assigned by default to all WebViews.
            BitmapSurface surface = (BitmapSurface)view.Surface;

            // Save the buffer to a PNG image.
            surface.SaveToPNG("result.png", true);

            // Show image.
            ShowImage("result.png");

            // We demonstrate resizing to full height.
            Console.WriteLine("Attempting to resize to full height...");

            // This JS call will normally return the full height
            // of the page loaded.
            int docHeight = (int)view.ExecuteJavascriptWithResult(PAGE_HEIGHT_FUNC);

            // ExecuteJavascriptWithResult is a synchronous call. Synchronous
            // calls may fail. We check for errors that may occur. Note that
            // if you often get a Error.TimedOut, you may need to set the
            // IWebView.SynchronousMessageTimeout property to a higher value
            // (the default is 800ms).
            Error lastError = view.GetLastError();

            // Report errors.
            if (lastError != Error.None)
            {
                Console.WriteLine(String.Format("Error: {0} occurred while getting the page's height.", lastError));
            }

            // Exit if the operation failed or the height is 0.
            if (docHeight == 0)
            {
                return;
            }

            // All predefined surfaces of Awesomium.NET,
            // support resizing. Here is a demonstration.
            surface.Resized += (s, e) =>
            {
                Console.WriteLine("Surface Resized");

                // Save the updated buffer to a new PNG image.
                surface.SaveToPNG("result2.png", true);
                // Show image.
                ShowImage("result2.png");

                // Exit the update loop and shutdown the core.
                WebCore.Shutdown();

                // Note that when Shutdown is called from
                // Awesomium's thread, anything after Shutdown
                // will not be executed since the thread exits
                // immediately.
            };

            // Call resize on the view. This will resize
            // and update the surface.
            view.Resize(view.Width, docHeight);
        }
        public void runCommands()
        {
            switch (commandNumber)
            {
            case 1:
            {
                Console.WriteLine("Typing Username...");
                webView.ExecuteJavascript("document.getElementById('input_3').value='Username'");
                break;
            }

            case 2:
            {
                Console.WriteLine("Typing Password...");
                webView.ExecuteJavascript("document.getElementById('input_4').value='Password'");
                break;
            }

            case 3:
            {
                Console.WriteLine("Typing First Name...");
                click(410, 150);
                typeKeys("John");
                break;
            }

            case 4:
            {
                Console.WriteLine("Typing Last Name...");
                click(410, 197);
                typeKeys("Doe");
                break;
            }

            case 5:
            {
                Console.WriteLine("Selecting Gender as Male...");
                webView.ExecuteJavascript("document.getElementById('input_7_0').checked=true");
                break;
            }

            case 6:
            {
                Console.WriteLine("Selecting 'Search Engine' in combo box...");
                webView.ExecuteJavascript("document.getElementById('input_8').value='Search Engine'");
                break;
            }

            default:
            {
                //webView.Render().SaveToPNG("result.png", true);
                BitmapSurface surface = (BitmapSurface)webView.Surface;
                surface.SaveToPNG("result.png", true);
                System.Diagnostics.Process.Start("result.png");
                Console.WriteLine("Done running all commands, shutting down webcore...");
                WebCore.Shutdown();
                running = false;
                break;
            }
            }
            commandNumber++;
        }