Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Taking a screenshot of a single page app in with Javascript

Background

I run UI tests on an application I work on by simulating user input with JS. The JS runs a bunch of mouse and keyboard triggers in sequence on an iframe in which the tested app is running and I can watch for breaks. I'd like to set it up so that it can take screenshots of the current state periodically.

Already Considered

I know this is a duplicate question, I'm asking this again now in case there have been any developments in this space recently.

I've looked at http://html2canvas.hertzen.com/ to take screenshots, and while it works for the most part it seems to have trouble when there's a lot of CSS3 and webfonts usage.

I'm also not free to use a server side method because I'm working on a single page backbone app and it doesn't store state based on URLs (for the most part).

I even had a look at the mozilla specific background:-moz-element() property, but that doesn't work when the page you want is in an iframe.

I think what comes closest to what I need is phantomJS which allows you to manipulate the page and take a screenshot. I'd like to avoid this because I'd use the ability to see the test as I'm running it otherwise.

So, does anyone know of anything I could use here? It can even be exclusive to one browser (Firefox or Chrome) as long as it runs on windows. It doesn't need to run on all clients, just on mine so it's ok even if I have to setup a special environment as long as the screenshots can be saved via JS (only on Firefox or Chrome though, so no ActiveX).

TL;DR Need Javascript or HTML5 API to save screenshots of an app running in an iframe. No paid solutions please.

like image 582
JohnP Avatar asked Mar 14 '13 18:03

JohnP


People also ask

How do I take a screenshot using JavaScript?

A screenshot of any element in JavaScript can be taken using the html2canvas library. This library can be downloaded from its official website.

How do I take a screenshot of a specific app?

Most Android devices should be able to take screen grabs by holding down the power and volume down buttons, though holding the power and home buttons (if your device has a physical button) may also work.

Can I automate a screenshot?

Automated Screenshot testing will help you capture screenshots in bulk through different desktops and mobile devices running on various OS in a single go! It is facilitated by instrumented UI test cases to help you realize how seamlessly your web app renders through different screen sizes using different browsers.


2 Answers

It looks like that you need exactly what we have developed. We can create total accurate screenshot from a single page app (including the state).

Take a look at http://usersnap.com

full disclosure: I'm the co-founder of Usersnap.

like image 63
Joe Avatar answered Oct 20 '22 00:10

Joe


Went with phantomJS in the end. While html2canvas is great for basic usage, it doesn't come through too well when you have a lot of CSS3 and webfonts.

The stable version of phantomJS has some documented problems with webfonts though - https://code.google.com/p/phantomjs/issues/detail?id=592

Thankfully though this user has built a version with font support that works out of the box - http://arunoda.me/blog/phantomjs-webfonts-build.html

Also, it's worth noting that if you're serving web fonts from your own server (locally), it might still fail. Switching to the google hosted url will usually fix this. Might be something to do with the content-type.

like image 22
JohnP Avatar answered Oct 20 '22 01:10

JohnP