Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete screenshot (no scroll) #328

Closed
chipay opened this issue Nov 2, 2015 · 5 comments
Closed

Complete screenshot (no scroll) #328

chipay opened this issue Nov 2, 2015 · 5 comments

Comments

@chipay
Copy link

chipay commented Nov 2, 2015

I've been playing around with the screenshot functionality but I couldn't make it print the whole content.
My use case is to compare the whole site snapshot for differences (similar to the phantomcss library). Casperjs allows to capture the whole scrollable screen in a png. Is there a way of doing the same with nightmare? If not, I'll see if it can be added easily but it may take some time as I just started getting into nightmare.

@DWboutin
Copy link

DWboutin commented Nov 4, 2015

^ +1

@audable
Copy link

audable commented Nov 4, 2015

+1

@rosshinkley
Copy link
Contributor

You can ask Nightmare to get the dimensions of the body element (using scrollHeight/scrollWidth), and then use Nightmare.viewport to set the viewport dimensions. After that, you should be able to take a screenshot of the entire page. I'm not sure this will work 100% of the time, but it's what I've got working for me and I thought I'd share.

An example might be useful:

var Nightmare = require('nightmare'),
    vo = require('vo');

function * run() {
    var nightmare = new Nightmare({
        show: false,
        width: 1024,
        height: 768
    });
    var dimensions = yield nightmare.goto('http://www.gmail.com')
        .wait('body')
        .evaluate(function() {
            var body = document.querySelector('body');
            return { 
                height: body.scrollHeight,
                width:body.scrollWidth
            }
        });

    console.dir(dimensions);
    yield nightmare.viewport(dimensions.width, dimensions.height)
        .wait(1000)
        .screenshot(require('path')
            .join(__dirname, 'gmail.png'));

    yield nightmare.end();
}   

vo(run)(function() {
    console.log('done');
});

A couple of notes and questions:

  1. The width (and to a lesser extent, height) of the original Nightmare instance should be reasonably big, especially if your site is responsive. Make sure the width is near the threshold you're targeting your screenshot for.
  2. show has to be false. If Electron actually renders to a real screen and the page scrolls off the bottom, the viewport will render with the scrollbar as it appears on screen. I think this is because show:false renders in the framebuffer which has no screen dimensions to honor, but I'm not sure. Anyone know for certain?
  3. The wait call after setting Nightmare.viewport seems to be mandatory. (Not waiting stretches the original viewport to the body dimensions obtained in evaluate.) I am not clear on why this is, but I once again think it has to do with rendering time in the framebuffer. (Aside: I think 1s is probably far too long, but I'm guessing a mininum time would be machine-dependent.) A cursory look through the Electron project didn't yield anything, but admittedly, I didn't spend a whole lot of time on it. Again, anyone know for sure?

@matthewmueller
Copy link
Contributor

thanks @rosshinkley for the example :-)

@Leonardonline
Copy link

  1. show has to be false. If Electron actually renders to a real screen and the page scrolls off the bottom, the viewport will render with the scrollbar as it appears on screen. I think this is because show:false renders in the framebuffer which has no screen dimensions to honor, but I'm not sure. Anyone know for certain?

Adding enableLargerThanScreen: true, to the Nightmare object allow to take screenshot bigger than screen size.

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

No branches or pull requests

6 participants