niklasvh / html2canvas

Screenshots with JavaScript
https://html2canvas.hertzen.com/
MIT License
30.39k stars 4.78k forks source link

html2canvas is not defined with latest version, but ok with v0.4.1 - 7.9.2013 #1542

Open xinwangTW opened 6 years ago

xinwangTW commented 6 years ago

Please make sure you are testing with the latest release of html2canvas. Old versions are not supported and issues reported for them will be closed.

Please follow the general troubleshooting steps first:

Bug reports:

Please replace this line with a brief summary of your issue AND if possible an example on jsfiddle.

Specifications:

code as below public static void TakeSnapshotNew() {

        var fileName = ScenarioContext.Current.ScenarioInfo.Title.ToIdentifier() + DateTime.Now.ToString("HH_mm_ss") + "JS" + ".png";
        var fileLocation = Path.Combine(Configuration.SCREEN_SHOT_LOCATION, fileName);
        GetPageScreenshotbyHtml2canvas(Driver, fileLocation);

    }

    private static void GetPageScreenshotbyHtml2canvas(IWebDriver driver, string imageFileName)
    {
        var js = driver as IJavaScriptExecutor;

        var html2CanvasLib = File.ReadAllText(@"html2canvas.min.js");

        //var baseExecutingAssemblyDir = Path.GetDirectoryName(new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath);
        //var pathhtml2canvas = Path.Combine(baseExecutingAssemblyDir ?? string.Empty, string.Format("./Data/{0}.js", "html2canvas.min"));
        //var html2CanvasLib = File.ReadAllText(pathhtml2canvas);

        // * 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 =
        @"
    //error occurs html2canvas is not defined    
        **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);
    }

}

}

ohundre commented 6 years ago

me too

drachs commented 6 years ago

Anybody know why this is happening yet? Trying to check out the library but it's not working.

Sandy-Hut commented 5 years ago

Same here, nothing's working with latest version!!, when I use requireJS for script loading.

zawster commented 4 years ago

I am also facing this issue. When I was using v0.4.1 it was working fine but now I am using the latest version(1.0.0-rc.5) and it is not working. Anyone has a solution to it.