baudehlo / node-phantom-simple

Simple bridge to phantomjs for Node
MIT License
201 stars 70 forks source link

Page.getPage returns object without functions #131

Open Reewr opened 8 years ago

Reewr commented 8 years ago

page.getPage(name) can be used to retrieve pages that are children of another page. However, the pages that are retrieved by this function are not node-phantom-simple page instances, but a json-ifined phantom-page.

'use strict';
let driver = require('node-phantom-simple');

let onCreated = function(err, page) {
  page.onPageCreated = function(childPage) {
    childPage.evaluate(function() {
      window.name = 'myName';
    }, function() {
      page.getPage('myName', function(err, newChildPage) {
        console.log('Is null: ', newChildPage === null);
        console.log('Has render:', typeof newChildPage.render !== 'undefined'); 
        console.log(Object.getOwnPropertyNames(newChildPage));
        process.exit();
      });
    });
  };

  page.evaluate(function() {
    window.open(""); // open a blank page
  });
};

let onPhantomCreated = function(err, phantom) {
  phantom.createPage(onCreated);
};

driver.create(onPhantomCreated);

Output:

Is null:  false
Has render: false
[ 'objectName',
  'title',
  'frameTitle',
  'content',
  'frameContent',
  'url',
  'frameUrl',
  'loading',
  'loadingProgress',
  'canGoBack',
  'canGoForward',
  'plainText',
  'framePlainText',
  'libraryPath',
  'offlineStoragePath',
  'offlineStorageQuota',
  'viewportSize',
  'paperSize',
  'clipRect',
  'scrollPosition',
  'navigationLocked',
  'customHeaders',
  'zoomFactor',
  'cookies',
  'windowName',
  'pages',
  'pagesWindowName',
  'ownsPages',
  'framesName',
  'frameName',
  'framesCount',
  'focusedFrameName' ]

As you can see, the object has no actual functions, but only properties that are usually found on the phantom instance of the page.

The same fix you applied to onPageCreated will apply to this.