       callback = options
 
     beforeEach = ->
-      if template?
-        initialized = false
-        setFixtureHTML(JST["test_helpers/fixtures/#{template}"]())
-      else
-        initialized = true
+      ready = (callback) ->
+        if template?
+          addEventListener "trix-initialize", handler = ({target}) ->
+            removeEventListener("trix-initialize", handler)
+            if target.hasAttribute("autofocus")
+              target.editor.setSelectedRange(0)
+            callback(target)
+
+          setFixtureHTML(JST["test_helpers/fixtures/#{template}"]())
+        else
+          callback()
       setup?()
 
     afterEach = ->

helpers = Trix.TestHelpers

initialized = false
initializedCallbacks = []

document.addEventListener "trix-initialize", ->
  initialized = true
  callback() while callback = initializedCallbacks.shift()

ready = (callback) ->
  if initialized
    callback()
  else
    initializedCallbacks.push(callback)

setFixtureHTML = (html) ->
  element = findOrCreateTrixContainer()

    QUnit.test name, (assert) ->
      doneAsync = assert.async()

      ready (element) ->
        done = (expectedDocumentValue) ->
          if element?
            if expectedDocumentValue
              assert.equal element.editor.getDocument().toString(), expectedDocumentValue
            requestAnimationFrame(doneAsync)
          else
            doneAsync()

        if callback.length is 0
          callback()
