1

Is there any way to call casperjs methods like capture when function is called from evaluate context ?

Explanation : i want to be able to write js scripts (qunit) that can run both in a "real" browser or in casper.

Sample :

function screenshot()(
//i'm runing in a "real" browser ? Then only console.log 
//i'm running in casper ? Then call capser.capture()

I tried this with closure but fails :

var casper = require('casper').create();
casper.start('http://google.fr/');

casper.evaluate(function(o) {
o.capture('/tmp/google.png', {
    top: 100,
    left: 100,
    width: 500,
    height: 400
});
}, {o: this});

casper.run()


TypeError: JSON.stringify cannot serialize cyclic structures.                   
  :/modules/webpage.js:249
  /Users/macbookpro/js:576 in evaluate
  /Users/macbookpro/js/testClosure.js:11

I know there's a way to use use console.log as a message bus but i'm searching for a better solution.

Thanks

2 Answers 2

3

In PhantomJS (and thus also CasperJS), evaluate runs in a jailed environment. Only primitive objects, something you can serialize via JSON.stringify and JSON.parse is accepted.

The usual practice is to run the screen capture from your main script. You can still trigger the capture from other place, including within evaluate, you just need to communicate it back to the main script. Check out PhantomJS included run-qunit.js example which detects the completion of the tests by monitoring the existence of a particular DOM element.

1
  • Ok so answer is : evaluate can't access to main capserJS objects. I'll try to find another way to let the DOM context talk with Casper, using return structure or console.log callback.
    – jseguillon
    Jan 3, 2013 at 9:10
2

There's no way to run casper methods within evaluate(). Here's your code, fixed:

var casper = require('casper').create();

casper.start('http://google.fr/', function() {
    this.capture('google.png', {
        top: 100,
        left: 100,
        width: 500,
        height: 400
    });
});

casper.run()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.