I need a way to take a screenshot during a test which uses QUnit and Karma to run inside PhantomJS 2.0.1
I've found this command:
window.top.callPhantom('render');
That doesn't throw any error but doesn't seem to work, or at least, I don't know where to look for the taken screenshot.
Any clue?
My Karma entry for a customized phantomjs that takes snapshots looked like this:
Found a way!
Solution
I had to edit my custom PhantomJS custom launcher adding an option:
As you can see, we are defining the
onCallback
option, it will be injected inside the script launched byphantomjs
. The script, then, will contain:Now, we are able to use
callPhantom
to ask PhantomJS to run the content of ouronCallback
function and use all the native PhantomJS methods.Usage
Now, you can use in your tests the function:
To take a screenshot that will be saved in the root directory of your application.
Additionally, if you define the
fname
you'll be able to define a custom path and file name to your screenshot.Pack all together for ease of use
I've created an handy function to use inside my tests. The onCallback function is reduced to the minimum necessary, in this way all the logic is managed inside my test environment:
karma.conf.js
helper
Credits
I got this script from this answer, adapted it and found by myself where to put it to make it work with karma.