2

I have a function called clickMore:

function clickMore(max, i){
   i = i || 0;
   if ((max == null || i < max) && this.visible(moreButton)) { // synchronous
      // asynchronous steps...
      this.thenClick(moreButton);  // sometimes the click is not properly dispatched
      this.echo('click');
      this.waitUntilVisible(loadingButton);
      this.waitUntilVisible(moreButton, null, function onTimeout(){
         // only placeholder so that the script doesn't "die" here if the end is reached
      });
      this.then(function(){

         //this.capture("business_"+i+".png");   //captures a screenshot of the page
         clickMore.call(this, max, i+1); // recursion
      });
   }
}

I would like to call that function from spooky here:

spooky.then(function(){
              clickMore.call(spooky);
          })

I've looked through the Spooky docs, and know that I'll probably need to use a function tuple, but not sure how to implement. How can I go about doing this?

UPDATE:

Tried using a function tuple from the SpookyJS documentation with no luck:

spooky.then([{
   clickMore: clickMore
}, function(){
    clickMore.call(spooky);
}]);
novicePrgrmr
  • 18,647
  • 31
  • 81
  • 103

1 Answers1

1

Functions passed into spooky.then will execute in the Casper/PhantomJS JavaScript environment. They do not have access to Spooky's Node.js JS environment.

spooky.then([{
   clickMore: clickMore
}, function(){
    clickMore.call(spooky); // <- spooky is not defined here
}]);

Please have another look at what the Introduction wiki page says about JavaScript Environments and the fact that they are isolated.

lawnsea
  • 6,463
  • 1
  • 24
  • 19
  • You didn't actually answer the question. – Artjom B. Jul 16 '14 at 22:38
  • Sure I did. The provided code will not work. I explained why and pointed to the relevant documentation. – lawnsea Jul 21 '14 at 05:25
  • Well, according to the update in the question, the provided code didn't work. I understand that you wanted to point out what the error is, but this does not provide a solution to this problem. This rather shows what the root of the problem is. – Artjom B. Jul 21 '14 at 05:52