Как я могу настроить внешние JS файлы и скрипты для запуска HTML с помощью Phantom JS в автономном режиме? - PullRequest
0 голосов
/ 04 апреля 2020

Я пытаюсь отрендерить html с помощью Phantom JS в режиме без головы, используя код Java. Файл HTML отлично отображается в браузере. Когда я пытаюсь запустить код, я получаю следующую ошибку, которая связана с внешними javascript файлами, которые включены в HTML. Может кто-нибудь, пожалуйста, дайте мне знать, как я могу настроить Phantom JS для использования этих внешних javascript файлов?

Ниже приведены подробности

Java Код

DesiredCapabilities caps = new DesiredCapabilities();
        caps.setJavascriptEnabled(true);                
        caps.setCapability("takesScreenshot", true);  
        caps.setCapability(
                                PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
                                "C:\\Users\\nithi\\Downloads\\phantomjs-2.1.1-windows\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe"
                            );
         String filePath = "D:\\Work\\workspace\\canvastest\\Input\\BhaEditor\\canvas.html";
         File path=new File("C:\\Users\\nithi\\Downloads\\phantomjs-2.1.1-windows\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
         System.setProperty("phantomjs.binary.path",path.getAbsolutePath());
         WebDriver driver= new PhantomJSDriver(caps);
         driver.navigate().to("file:///" + filePath);
         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
         System.out.println(driver.getPageSource());
         System.out.println("Output="+driver.findElement(By.id("canvasImage")).getText());
         driver.quit();

HTML образец кода

    <!DOCTYPE html>
                <html>        
                <head>    
                    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
                    <meta http-equiv="cache-control" content="no-cache">
                    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
                    <script src="geotoolkit/geotoolkit.adv.js"></script> 
         <script src="geotoolkit/geotoolkit.data.adv.js"></script>
            <script src="geotoolkit/geotoolkit.controls.adv.js"></script>
            <script src="geotoolkit/geotoolkit.pdf.adv.js"></script>
            <script src="geotoolkit/geotoolkit.widgets.adv.js"></script> 
            <script src="geotoolkit/geotoolkit.svg.adv.js"></script>
            <script src="geotoolkit/geotoolkit.deviation.adv.js"></script>        
            <script src="geotoolkit/geotoolkit.schematics.adv.js"></script>          
            <script src="geotoolkit/geotoolkit.schematics.widgets.adv.js"></script>
            <script src="geotoolkit/geotoolkit.gauges.adv.js"></script>       
                </head>
            <body>
                <div id="geotoolkit-plot-host" class="geotoolkit-plot-host">
                    <div id="schematic-container-left">
                        <canvas id="demo-canvas" class="geotoolkit-plot" width="500" height="756"></canvas>
                    </div>        
                </div>
            <script type="text/javascript">
var _heldComponent;
        var _placeholder;
        var _components;
        var _widget;
        var _drilling;
        var loader;
        var _schematicsPlot;
        var _schematicsCanvas;
            function Demo() {        
                        _schematicsCanvas = document.getElementById('demo-canvas');
                        _removedComponents = [];

                        _drilling = [
                            'bent sub'
                        ];

                        _heldComponent = null;
                        _placeholder = {
                            'name': 'empty',
                            'description': 'empty',
                            'length': 25,
                            'diameter': {
                                'outer': 0.5,
                                'inner': 0
                            }
                        }

                    registry = new geotoolkit.schematics.factory.ComponentNodeFactoryRegistry();
                    loader = new geotoolkit.schematics.factory.SVGComponentsLoader({
                        'registry': registry,
                        'path': null
                    });                           
                }
              document.addEventListener("DOMContentLoaded", function(event) { 
                     Demo();
                });

            </script>
            </body>
    </html>

Журналы ошибок

Apr 04, 2020 1:47:50 AM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: executable: C:\Users\nithi\Downloads\phantomjs-2.1.1-windows\phantomjs-2.1.1-windows\bin\phantomjs.exe
Apr 04, 2020 1:47:50 AM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: port: 31170
Apr 04, 2020 1:47:50 AM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: arguments: [--webdriver=31170, --webdriver-logfile=D:\Work\workspace\canvastest\phantomjsdriver.log]
Apr 04, 2020 1:47:50 AM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: environment: {}
[INFO  - 2020-04-03T22:47:53.325Z] GhostDriver - Main - running on port 31170
[INFO  - 2020-04-03T22:47:54.053Z] Session [27a40de0-75fd-11ea-ae92-47484e75e7b2] - page.settings - {"XSSAuditingEnabled":false,"javascriptCanCloseWindows":true,"javascriptCanOpenWindows":true,"javascriptEnabled":true,"loadImages":true,"localToRemoteUrlAccessEnabled":false,"userAgent":"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1","webSecurityEnabled":true}
[INFO  - 2020-04-03T22:47:54.053Z] Session [27a40de0-75fd-11ea-ae92-47484e75e7b2] - page.customHeaders:  - {}
[INFO  - 2020-04-03T22:47:54.053Z] Session [27a40de0-75fd-11ea-ae92-47484e75e7b2] - Session.negotiatedCapabilities - {"browserName":"phantomjs","version":"2.1.1","driverName":"ghostdriver","driverVersion":"1.2.0","platform":"windows-10-32bit","javascriptEnabled":true,"takesScreenshot":true,"handlesAlerts":false,"databaseEnabled":false,"locationContextEnabled":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"cssSelectorsEnabled":true,"webStorageEnabled":false,"rotatable":false,"acceptSslCerts":false,"nativeEvents":true,"proxy":{"proxyType":"direct"}}
[INFO  - 2020-04-03T22:47:54.053Z] SessionManagerReqHand - _postNewSessionCommand - New Session Created: 27a40de0-75fd-11ea-ae92-47484e75e7b2
Apr 04, 2020 1:47:54 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
[ERROR - 2020-04-03T22:47:54.136Z] Session [27a40de0-75fd-11ea-ae92-47484e75e7b2] - page.onError - msg: ReferenceError: Can't find variable: geotoolkit

  phantomjs://platform/console++.js:263 in error
[ERROR - 2020-04-03T22:47:54.137Z] Session [27a40de0-75fd-11ea-ae92-47484e75e7b2] - page.onError - stack:
  BHAEditorDemo (file:///D:%5CWork%5Cworkspace%5Ccanvastest%5CInput%5CBhaEditor%5Ccanvas.html:186)
  (anonymous function) (file:///D:%5CWork%5Cworkspace%5Ccanvastest%5CInput%5CBhaEditor%5Ccanvas.html:258)

  phantomjs://platform/console++.js:263 in error
...