0
votes

I am trying to get the onPrompt callback working with PhantomJS.

Just for testing I have a basic angular application that prompts the user on initialization and displays that data on the page.

It works fine when I enter the information into the prompt manually, but it will not work when using the PhantomJS onPrompt callback.

Here's the angular app:

angular
 .module('app')
 .controller('test', TestController)


 function TestController() { 
   var vm = this; 
   vm.$onInit = onInit;
   vm.testData = '';

   function onInit() {
     vm.testData = prompt('Name?');
   }
 }

This is the code I running with PhantomJS

var page = require('webpage').create();
  page.open('http://localhost:3000', function() { 
  console.log('test')

  page.onPrompt = function(msg, defaultVal) {
    console.log("MESSAGE", msg)
    return "Dog";
  };
  page.render('test.pdf');
  phantom.exit();
  });

I would expect to get a console.log that says "MESSAGE" and the text from the prompt and also a screenshot of my page with "Dog" displayed.

I get a screenshot of a blank page and no console log.

I would ideally like to use this callback with node webshot as an option. webshot phantom callbacks

Thanks for the help.

1

1 Answers

0
votes

There is a context in which interaction with the actual interaction with the page takes place - usually within the page.evaluate(..) function. The onPrompt callback will execute within that context and will not print to the console as you expect - I think you need to marshal the output back to the console by setting the onConsoleMessage callback first:

page.onConsoleMessage = function (msg) {
    console.log(msg);
};

And then it should print to the console as you expect :)