Confused why `render` returns false #11995

Open
twolfson opened this Issue Feb 20, 2014 · 4 comments

3 participants

@twolfson

I have been debugging a script of mine and boiled down the problem to a strange timing issue. A locally hosted website loaded via .open returns a status of success but occasionally returns false when render is immediately called. When I introduce a timeout, it works all the time.

I have reproduced this with google.com. Below is an example:

// screenshot.js
var page = require('webpage').create();
page.open('http://google.com/', function (status) {
    console.log('status', status);
    console.log('render output', page.render('a.png'));
    phantom.exit();
});
todd at Euclid in ~/github/twolfson.com/test/perceptual-tests on dev/fix.up.perceptual.tests*
$ rm a.png; phantomjs screenshot.js; ls a.png
status success
render output true
a.png

todd at Euclid in ~/github/twolfson.com/test/perceptual-tests on dev/fix.up.perceptual.tests*
$ rm a.png; phantomjs screenshot.js; ls a.png
status success
render output false
ls: cannot access a.png: No such file or directory
// screenshot.js with timeout
var page = require('webpage').create();
page.open('http://google.com/', function (status) {
    console.log('status', status);
    setTimeout(function () {
      console.log('render output', page.render('a.png'));
      phantom.exit();
    }, 100);
});

Reference information: Linux Mint 14, PhantomJS 1.9.7

@damienalexandre

I'm also using the timeout trick, everything was fine until I tried to "render" this url: https://gist.github.com/kennr/9698576. I have no idea what's going on but an empty file is created and the render method return false, even after a 5sec timeout.

Remote debug show me the correct HTML / CSS stuffs, and rendering with the "png" option produce a big full transparent file... :neckbeard:

Linux Mint 16, PhantomJS 1.9.7.

@Joozt83

It needs to load the external images thats why render can return false I suggest putting in an interval instead of a set timeout

For example my script screenshot.js renders the stdin to stdout with an interval of 1 sec and max of 5 min to prevent an endless loop

// create the page and sytem vars
var page = require('webpage').create();
var system = require('system');

// get the input for the page from stdin
page.content = system.stdin.read();

// create a counter to prevent endless loops
var counter = 0;

// create the interval function
setInterval(function(){

    // running for more than 5 minutes?
    // report the error
    if (counter > 300) phantom.exit(5);

    // check if the render was successfull
    if (page.render('/dev/stdout', 'png')) phantom.exit(0);

    // increment the counter
    counter++;
}, 1000);
@twolfson

If that were the case, then I would expect it to behave more like a web browser which loads placeholder images in its place. Then, the render should still be truthy, but with different return information.

@Joozt83

I agree that this is weird behaviour.

But in my case when I load a big html page with images and set the timeout to 1 millisecond It returns false first x times.

When loading a simple html string it will render first time always.

@bchavet bchavet referenced this issue in cjpetrus/url2img May 5, 2016
Merged

Fix some stability and compatibility problems #4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment