Last active
          August 29, 2015 14:00 
        
      - 
      
- 
        Save Stanback/11068255 to your computer and use it in GitHub Desktop. 
    PhantomJS test of the XContentReady event
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | /* | |
| * Simple test of PhantomJS and the XContentReady event. | |
| * | |
| * This is the basic pattern used for the ember-prerender project: https://github.com/zipfworks/ember-prerender | |
| * More info on the XContentReady event: https://github.com/n-fuse/the-XContentReady-Event/ | |
| * | |
| * Usage: phantomjs phantom_test.js | |
| */ | |
| var page = require('webpage').create(); | |
| page.onInitialized = function() { | |
| console.log("Initialized the page"); | |
| page.evaluate(function() { | |
| document.addEventListener('XContentReady', function() { | |
| window.callPhantom("Received XContentReady event!"); | |
| }, false); | |
| }); | |
| }; | |
| page.onCallback = function(msg) { | |
| console.log("Phantom:", msg); | |
| phantom.exit(0); | |
| }; | |
| page.onConsoleMessage = function(msg) { | |
| console.log("Page:", msg); | |
| }; | |
| page.content = "<html>\n" + | |
| " <body>\n" + | |
| " <script>\n" + | |
| " var e;\n" + | |
| " //e = new CustomEvent('XContentReady', { bubbles: true, cancelable: false });\n" + | |
| " e = document.createEvent('Event');\n" + | |
| " e.initEvent('XContentReady', true, false);\n" + | |
| " console.log(\"Dispatching XContentReady event...\");\n" + | |
| " document.dispatchEvent(e);\n" + | |
| " </script>\n" + | |
| " </body>\n" + | |
| "</html>"; | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
PhantomJS (as of version 1.9.7) doesn't seem to know about CustomEvent.
Using document.createEvent(...) and event.initEvent(...) works as expected.