 
   onLoadSuccess: ->
     @injectJS()
-    @runMocha()
+    @waitForRunMocha()
 
   onLoadFailed: ->
     @fail "Failed to load the page. Check the url: #{@url}"

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

      @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.runner = mocha.run()
      if mocha.phantomjs.runner
        mocha.phantomjs.runner.on 'end', ->
