diff --git a/index.js b/index.js index 5a1aeb2..b87fd17 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,6 @@ var path = require( "path" ); var phantomjs = require( "phantomjs-prebuilt" ); var childProcess = require( "child_process" ); var chalk = require( "chalk" ); -var binPath = phantomjs.path; var argv = require( "minimist" )( process.argv.slice(2) ); var pluginName = "glyphhanger"; @@ -68,7 +67,7 @@ function phantomGlyphhanger( urls ) { console.log( prefix + urls.join( "\n" + prefix ) ); } - childProcess.execFile( binPath, childArgs.concat( urls ), function( error, stdout, stderr ) { + childProcess.execFile( phantomjs.path, childArgs.concat( urls ), function( error, stdout, stderr ) { if( error ) { throw error; } @@ -80,7 +79,7 @@ function phantomGlyphhanger( urls ) { if( !argv.spider ) { phantomGlyphhanger( argv._ ); } else { - childProcess.execFile( binPath, urlsChildArgs, function( error, stdout, stderr ) { + childProcess.execFile( phantomjs.path, urlsChildArgs, function( error, stdout, stderr ) { if( error ) { throw error; } diff --git a/phantomjs-glyphhanger.js b/phantomjs-glyphhanger.js index b5f92e0..d0e0ced 100644 --- a/phantomjs-glyphhanger.js +++ b/phantomjs-glyphhanger.js @@ -7,23 +7,34 @@ var args = require( "system" ).args; var pluginName = "glyphhanger"; function requestUrl( url ) { - var page = webpage.create(); + return new Rsvp.Promise(function( resolve, reject ) { + var page = webpage.create(); - page.onConsoleMessage = function( msg ) { - console.log( pluginName + " phantom console:", msg ); - }; + page.onConsoleMessage = function( msg ) { + console.log( pluginName + " phantom console:", msg ); + }; - return new Rsvp.Promise(function( resolve, reject ) { - page.open( url, function( status ) { - if ( status === "success" && page.injectJs( "node_modules/characterset/lib/characterset.js" ) && page.injectJs( "glyphhanger.js" ) ) { - resolve( page.evaluate( function() { + page.onLoadFinished = function( status ) { + if( status !== "success" ) { + reject( "onLoadFinished error", status ); + } + if( page.injectJs( "node_modules/characterset/lib/characterset.js" ) && + page.injectJs( "glyphhanger.js" ) ) { + + resolve( page.evaluate( function() { var hanger = new GlyphHanger(); hanger.init( document.body ); return hanger.getGlyphs(); }) ); } else { + reject( "injectJs error" ); + } + }; + + page.open( url, function( status ) { + if( status !== "success" ) { reject( url, status ); } }); diff --git a/test/test.html b/test/test.html index e26b066..4c1dd54 100644 --- a/test/test.html +++ b/test/test.html @@ -1,11 +1,24 @@ - - - - - - - This is a another test of the glyph logger x. ¯\_(ツ)_/¯ - + + + + + + + + abc + \ No newline at end of file diff --git a/test/test.js b/test/test.js index cd7e7c1..f6c57b9 100644 --- a/test/test.js +++ b/test/test.js @@ -3,6 +3,9 @@ require( "jsdom-global" )(); var assert = require( "assert" ); var GlyphHanger = require( "../glyphhanger.js" ); var GlyphHangerSpider = require( "../glyphhanger-spider.js" ); +var path = require( "path" ); +var phantomjs = require( "phantomjs-prebuilt" ); +var childProcess = require( "child_process" ); describe( "glyphhanger", function() { describe( "Simple node", function() { @@ -77,6 +80,19 @@ describe( "glyphhanger", function() { assert.equal( "\\uD83D\\uDCA9\\uD83D\\uDE0E", gh.toString() ); }); }); + + describe( "integration test: onload and DOMContentLoaded content", function() { + var args = [ path.join( __dirname, "..", "phantomjs-glyphhanger.js" ), false, false, "", path.join( __dirname, "test.html" ) ]; + + it( "should have 9 distinct glyphs", function( done ) { + this.timeout( 6000 ); + childProcess.execFile( phantomjs.path, args, function( error, stdout, stderr ) { + + assert.equal( "abcdefghi", stdout.trim() ); + done(); + }); + }) + }); }); describe( "glyphhanger-spider", function() { @@ -103,4 +119,23 @@ describe( "glyphhanger-spider", function() { assert.deepEqual( [ "firstlink.html", "secondlink.html" ], urls ); }); }); + + describe( "Integration test: find links", function() { + var args = [ path.join( __dirname, "..", "phantomjs-urls.js" ), path.join( __dirname, "urls.html" ) ]; + + it( "should have 3 links", function( done ) { + this.timeout( 6000 ); + childProcess.execFile( phantomjs.path, args, function( error, stdout, stderr ) { + + var expecting = [ + "file://" + path.join( __dirname, "test.html" ), + "file://" + path.join( __dirname, "test2.html" ), + "file://" + path.join( __dirname, "test3.html" ) + ]; + + assert.equal( expecting.join( "\n" ), stdout.trim() ); + done(); + }); + }) + }); }); \ No newline at end of file diff --git a/test/urls.html b/test/urls.html new file mode 100644 index 0000000..7a33f5d --- /dev/null +++ b/test/urls.html @@ -0,0 +1,13 @@ + + + + + + + + + Test + Test 2 + Test 3 + + \ No newline at end of file