Permalink
Browse files

added tests and code for command-line arguments (fixes #12)

  • Loading branch information...
1 parent 5f907b4 commit 6597ddf0432c8ddf13b02b56a57f6c687dab7d5e @sgentle sgentle committed Jan 4, 2012
Showing with 89 additions and 14 deletions.
  1. +1 −1 Cakefile
  2. +4 −4 phantom.coffee
  3. +11 −9 phantom.js
  4. +73 −0 test/adv.coffee
  5. BIN test/test.gif
View
@@ -19,4 +19,4 @@ task "build", "coffee-compile and browserify phantom", ->
"rm shim.js"
"#{bin}/browserify shim.coffee -o shim.js"
)
-task "test", "run phantom's unit tests", -> run "#{bin}/vows --spec test/*"
+task "test", "run phantom's unit tests", -> run "#{bin}/vows --spec test/*.coffee"
View
@@ -3,8 +3,8 @@ express = require 'express'
child = require 'child_process'
phanta = []
-startPhantomProcess = (port) ->
- ps = child.spawn 'phantomjs', [__dirname+'/shim.js', port]
+startPhantomProcess = (port, args) ->
+ ps = child.spawn 'phantomjs', args.concat [__dirname+'/shim.js', port]
ps.stdout.on 'data', (data) -> console.log "phantom stdout: #{data}"
ps.stderr.on 'data', (data) ->
@@ -28,7 +28,7 @@ wrap = (ph) ->
module.exports =
- create: (cb) ->
+ create: (args..., cb) ->
app = express.createServer()
app.use express.static __dirname
@@ -38,7 +38,7 @@ module.exports =
phantom = null
- ps = startPhantomProcess app.address().port
+ ps = startPhantomProcess app.address().port, args
ps.on 'exit', (code) ->
app.close()
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -0,0 +1,73 @@
+vows = require 'vows'
+assert = require 'assert'
+express = require 'express'
+phantom = require '../phantom'
+
+
+describe = (name, bat) -> vows.describe(name).addBatch(bat).export(module)
+
+# Make coffeescript not return anything
+# This is needed because vows topics do different things if you have a return value
+t = (fn) ->
+ (args...) ->
+ fn.apply this, args
+ return
+
+
+app = express.createServer()
+app.use express.static __dirname
+
+app.get '/', (req, res) ->
+ res.send """
+ <html>
+ <head>
+ <title>Test page title</title>
+ </head>
+ <body>
+ <img src="/test.gif" />
+ </body>
+ </html>
+ """
+
+app.listen()
+
+describe "The phantom module"
+ "Can create an instance with --load-images=no":
+ topic: t -> phantom.create '--load-images=no', (p) =>
+
+ @callback null, p
+
+ "which, when you open a page":
+ topic: t (p) ->
+ test = this
+ p.createPage (page) ->
+ page.open "http://127.0.0.1:#{app.address().port}/", (status) =>
+ setTimeout =>
+ test.callback null, page, status
+ , 1500
+
+ "and check the settings object":
+ topic: t (page) ->
+ page.get 'settings', (s) => @callback null, s
+
+ "loadImages isn't set": (s) ->
+ assert.strictEqual s.loadImages, false
+
+
+ "succeeds": (_1, _2, status) ->
+ assert.equal status, 'success'
+
+ "and check a test image":
+ topic: t (page) ->
+ page.evaluate (-> document.getElementsByTagName('img')[0]), (img) => @callback null, img
+
+ "it doesn't load": (img) ->
+ assert.strictEqual img.width, 0, "width should be 0"
+ assert.strictEqual img.height, 0, "height should be 0"
+
+
+ teardown: (p) ->
+ app.close()
+ p.exit()
+
+
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6597ddf

Please sign in to comment.