1

I want to check my angular.js application what's looks like. I have problem with phantomjs. I want to make it to look in my /etc/hosts file first

phantonjs script.js http://local.app/some/path some/path.html

Is this possible ?

Next is the content of script.js

 "use strict";
var fs = require('fs');
var system = require('system');

    var args = system.args;
    var url = args[1];
    var fileName = args[2] || 'result.html';

    function waitFor(testFx, onReady, timeOutMillis) {
        var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 8000, //< Default Max Timout is 3s
            start = new Date().getTime(),
            condition = false,
            interval = setInterval(function() {
                if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
                    // If not time-out yet and condition not yet fulfilled
                    condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
                } else {
                    if(!condition) {
                        // If condition still not fulfilled (timeout but condition is 'false')
                        console.log("'waitFor()' timeout");
                        phantom.exit(1);
                    } else {
                        // Condition fulfilled (timeout and/or condition is 'true')
                        console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
                        typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
                        clearInterval(interval); //< Stop this interval
                    }
                }
            }, 100); //< repeat check every 250ms
    };


    var page = require('webpage').create();

    // Open Twitter on 'sencha' profile and, onPageLoad, do...
    page.open(url, function (status) {
        // Check for page load success
        if (status !== "success") {
            console.log("Unable to access network");
        } else {
        console.log("Rendering...");
            // Wait for 'signin-dropdown' to be visible
            waitFor(function() {
                // Check in the page if a specific element is now visible
                return page.evaluate(function() {
                    return window.prerenderReady;
                });
            }, function() {
               console.log("Page rendered and saved to " + fileName);
            fs.write(fileName, page.content, 'w');
               phantom.exit();
            });
        }
    });
ArK
  • 20,698
  • 67
  • 109
  • 136
  • Possibly relevant [question](https://stackoverflow.com/questions/3582671/how-to-open-a-local-disk-file-with-javascript) – user5226582 Apr 08 '16 at 10:34
  • It should work, because hosts file is processed by operating system networking, not by PhantomJS. What is the exact problem? Have you tried it? – Vaviloff Apr 08 '16 at 15:53
  • Yes I try it but phantom just say that can't open url. So I thinking that the problem is with etc/hosts. The idea is to make prerendered pages of my application. This work perfect with our dev system which have fully qualify domain name, but when I try to test on my local environment it's fail. I use vagrant with debian installed. – Borislav Stankov Apr 11 '16 at 07:27

0 Answers0