Created
September 15, 2013 17:57
-
-
Save dzharii/6572995 to your computer and use it in GitHub Desktop.
When WebDriver tests started via Windows Remote Desktop session, the page screenshots become corrupted in Internet Explorer and show just a black screen.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*************************************************************************************************** | |
| * Alternative way to create full page screenshot via Selenium WebDriver with html2canvas.js | |
| * ========================================================================================= | |
| * | |
| * When WebDriver tests started via Windows Remote Desktop session, | |
| * The page screenshots become corrupted in Internet Explorer and | |
| * show just a black screen. | |
| * | |
| ***************************************************************************************************/ | |
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.IO; | |
| using OpenQA.Selenium; | |
| using OpenQA.Selenium.Firefox; | |
| using OpenQA.Selenium.IE; | |
| /*************************************************************************************************** | |
| Prerequisites: | |
| 1. Create .NET C# project in Visual Studio | |
| 2. Download html2canvas.min.js and include it into the project | |
| https://github.com/niklasvh/html2canvas/blob/master/build/html2canvas.min.js | |
| 3. Mark in the VS html2canvas.min.js properties, set “Copy to Output Directory = Copy if newer“ | |
| 4. Enjoy | |
| ***************************************************************************************************/ | |
| namespace Html2CanvasWebDriver | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| IWebDriver driver = new FirefoxDriver(); | |
| //IWebDriver driver = new InternetExplorerDriver(); | |
| driver.Navigate().GoToUrl("http://twitter.com"); | |
| GetPageScreenshot(driver, "image01.png"); | |
| driver.Navigate().GoToUrl("https://www.google.com.ua/search?client=opera&q=selenium%20webdriver"); | |
| GetPageScreenshot(driver, "image02.png"); | |
| driver.Navigate().GoToUrl("https://habrahabr.ru"); | |
| GetPageScreenshot(driver, "image03.png"); | |
| } | |
| private static void GetPageScreenshot(IWebDriver driver, string imageFileName) | |
| { | |
| var js = driver as IJavaScriptExecutor; | |
| var html2CanvasLib = File.ReadAllText(@"html2canvas.min.js"); | |
| // * html2canvas.min.js is huge to keep it in code. | |
| // * The lines bellow reads the original source and inject it to the | |
| // * JavaScript Injection template | |
| string html2CanvasInjectionTemplate = string.Format | |
| (@" | |
| function injectHtml2Canvas() | |
| {{ | |
| {0} | |
| }} | |
| var script = document.createElement('script'); | |
| script.appendChild(document.createTextNode('('+ injectHtml2Canvas +')();')); | |
| (document.body || document.head || document.documentElement).appendChild(script); | |
| ", html2CanvasLib); | |
| // * Checks if html2canvas.min.js already injected into the page. | |
| // * Injects the source code if required | |
| string checkHtml2CanvasLoaded = @"return window.html2canvas != undefined;"; | |
| bool isHtml2CanvasLoaded = Convert.ToBoolean(js.ExecuteScript(checkHtml2CanvasLoaded)); | |
| if (!isHtml2CanvasLoaded) | |
| { | |
| js.ExecuteScript(html2CanvasInjectionTemplate); | |
| } | |
| // * Sends a command to html2canvas to create a webpage screenshot. | |
| // * The screenshot data, encoded in Base64 will be stored into global variable | |
| // * [[ window.Html2canvas_dataURL ]] | |
| string createScreenshotScript = | |
| @" | |
| window.html2canvas_dataURL = ''; | |
| html2canvas(document.body, { | |
| onrendered: function(canvas) { | |
| dataURL = canvas.toDataURL('image/png'); | |
| dataURL = dataURL.replace('data:image/png;base64,', ''); | |
| window.html2canvas_dataURL = dataURL; | |
| } | |
| }); | |
| "; | |
| js.ExecuteScript(createScreenshotScript); | |
| // * This is async operation. So we have to wait until it is done. | |
| string getImageBase64Script = @"return window.html2canvas_dataURL;"; | |
| string imageInBase64 = ""; | |
| while (imageInBase64 == "") | |
| { | |
| imageInBase64 = js.ExecuteScript(getImageBase64Script) as string; | |
| if (imageInBase64 == "") System.Threading.Thread.Sleep(10); | |
| } | |
| // * Write image to the file | |
| byte[] fileContent = Convert.FromBase64String(imageInBase64); | |
| File.WriteAllBytes(imageFileName, fileContent); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi dzharii, value of imageInBase64 is always null, is above code runs correct on your side?
I run it in VS 2017.