       @waitForMocha()
     else
       @fail "Failed to start mocha."
-  
+
   waitForMocha: =>
     ended = @page.evaluate -> mocha.phantomjs?.ended
     if ended then @finish() else setTimeout @waitForMocha, 100
 
+  waitForRunMocha: =>
+    started = @page.evaluate -> mocha.phantomjs?.started
+    if started then @runMocha() else setTimeout @waitForRunMocha, 100
+
   runner: (reporter) ->
     try
       mocha.setup reporter: reporter
-      mocha.phantomjs = failures: 0, ended: false, run: false
       mocha.phantomjs.runner = mocha.run()
       if mocha.phantomjs.runner
         mocha.phantomjs.runner.on 'end', ->

  injectJS: ->
    if @page.evaluate(-> window.mocha?)
      @page.injectJs 'mocha-phantomjs/core_extensions.js'
      @page.evaluate @customizeProcessStdout, @customizeOptions()
      @page.evaluate @customizeConsole, @customizeOptions()
    else


  onLoadSuccess: ->
    @injectJS()
    @waitForRunMocha()

  onLoadFailed: ->
    @fail "Failed to load the page. Check the url: #{@url}"
