Eric Hartford
Eric Hartford

Reputation: 17952

Running node-phantom in mocha

I have a simple test here I expected to work. The idea is to get phantom working in mocha's tdd. (by the way I have tried both node-phantom and phantomjs-node with no success.)

nodephantom = require 'node-phantom'
assert    = require("chai").assert

host = 'http://google.com'

phantom = null
page = null

suite "Mocha Node-Phantom Loading Google:", ->

  suiteSetup (done) ->
    nodephantom.create (err,p) ->
      phantom = p
      done()

  test "Google page should load", (done) ->
    phantom.createPage (p) ->
      page = p
      page.open host, ->
        assert.match page.content(), /google/, "Page is loaded"
        done()

  suiteTeardown ->
    phantom.exit()

I execute the code as follows:

mocha -u tdd -R tap -b sometest.coffee

I get the following error:

1..1
not ok 1 Mocha Node-Phantom Loading Google: "before all" hook
  Error: global leak detected: location
      at Runner.checkGlobals (/home/ericstob/b/nodejs/lib/node_modules/mocha/lib/runner.js:104:21)
      at Runner.<anonymous> (/home/ericstob/b/nodejs/lib/node_modules/mocha/lib/runner.js:44:44)
      at Runner.EventEmitter.emit (events.js:88:17)
      at Runner.hook (/home/ericstob/b/nodejs/lib/node_modules/mocha/lib/runner.js:170:12)
      at done (/home/ericstob/b/nodejs/lib/node_modules/mocha/lib/runnable.js:134:5)
      at Runnable.run.duration (/home/ericstob/b/nodejs/lib/node_modules/mocha/lib/runnable.js:146:9)
      at phantom.createPage.page (/home/ericstob/b/seo/t/sometest.coffee:18:16)
      at SocketNamespace.module.exports.create (/home/ericstob/b/nodejs/lib/node_modules/node-phantom/node-phantom.js:156:4)
      at SocketNamespace.EventEmitter.emit [as $emit] (events.js:115:20)
      at connect (/home/ericstob/b/nodejs/lib/node_modules/node-phantom/node_modules/socket.io/lib/namespace.js:292:10)
      at /home/ericstob/b/nodejs/lib/node_modules/node-phantom/node_modules/socket.io/lib/namespace.js:308:13
      at SocketNamespace.authorize (/home/ericstob/b/nodejs/lib/node_modules/node-phantom/node_modules/socket.io/lib/namespace.js:252:5)
      at SocketNamespace.handlePacket (/home/ericstob/b/nodejs/lib/node_modules/node-phantom/node_modules/socket.io/lib/namespace.js:302:14)
      at Manager.handleClient (/home/ericstob/b/nodejs/lib/node_modules/node-phantom/node_modules/socket.io/lib/manager.js:697:32)
      at Manager.handleUpgrade (/home/ericstob/b/nodejs/lib/node_modules/node-phantom/node_modules/socket.io/lib/manager.js:618:8)
      at Server.<anonymous> (/home/ericstob/b/nodejs/lib/node_modules/node-phantom/node_modules/socket.io/lib/manager.js:123:10)
      at Server.EventEmitter.emit (events.js:115:20)
      at Socket.socket.ondata (http.js:1710:14)
      at TCP.onread (net.js:403:27)

When I remove phantom from this test, it works as expected.

assert    = require("chai").assert

is_decimal = (val) -> assert.match String(val), /^-?[0-9]*\.?[0-9]+$/, String(val) + ' is a decimal.'
is_hex = (val) -> assert.match String(val), /^(0[xX])?[a-fA-F0-9]+$/, String(val) + ' is a hexadecimal.'

suite "Mocha Minimal Test:", ->

  suiteSetup (done) ->
    done()

  test "Assertions pass", (done) ->
    is_decimal 1.5
    is_hex "0x2fc3"
    done()

  test "Assertions fail", (done) ->
    is_decimal 'dog'
    done()

  suiteTeardown ->
    {}

results:

$ mocha -u tdd -R tap -b mintest.coffee
1..2
ok 1 Mocha Minimal Test: Assertions pass
not ok 2 Mocha Minimal Test: Assertions fail
  'dog' is a decimal.: expected 'dog' to match /^-?[0-9]*\.?[0-9]+$/

So I know the basic structure of my test is correct and mocha is happy. But there is something about it that node-phantom is unhappy with.

I just want to get phantomjs working within this framework. Can anyone help me?

Upvotes: 0

Views: 937

Answers (1)

Eric Hartford
Eric Hartford

Reputation: 17952

The question I asked is answered here:

The answer is that Mocha will freak out if a library declares globals. I needed to include a -globals argument like this:

mocha -u tdd -R tap --globals location -b sometest.coffee

in order to permit the location variable to be declared by phantom.

also, I had to change my code slightly because node-phantom does not support page.content()

test "Google page should load", (done) ->
  phantom.createPage (err,p) ->
    page = p
    page.open host, ->
      page.evaluate( 
        -> return document.documentElement.innerHTML
        (err, result) ->
          assert.match result, /google/, "Page is loaded"
          done()
      )

Upvotes: 1

Related Questions