aboutsummaryrefslogtreecommitdiffstats
path: root/tests/dom_tests/phantom_runner.coffee
blob: 265c994e87b953c8677f1ea10cb9fff6c9899c43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
system = require 'system'
fs = require 'fs'
path = require 'path'
page = require('webpage').create()

page.settings.userAgent = 'phantom'

# ensure that the elements we test the link hints on are actually visible
page.viewportSize =
  width: 900
  height: 600

page.onConsoleMessage = (msg) ->
  console.log msg

page.onError = (msg, trace) ->
  console.log(msg)
  trace.forEach (item) ->
    console.log('  ', item.file, ':', item.line)

page.onResourceError = (resourceError) ->
  console.log(resourceError.errorString)

page.onCallback = (request) ->
  switch request.request
    when "keyboard"
      switch request.key
        when "escape"
          page.sendEvent "keydown", page.event.key.Escape
          page.sendEvent "keyup", page.event.key.Escape
        when "tab"
          page.sendEvent "keydown", page.event.key.Tab
          page.sendEvent "keyup", page.event.key.Tab
        when "shift-down"
          page.sendEvent "keydown", page.event.key.Shift
        when "shift-up"
          page.sendEvent "keyup", page.event.key.Shift
        when "ctrl-down"
          page.sendEvent "keydown", page.event.key.Control
        when "ctrl-up"
          page.sendEvent "keyup", page.event.key.Control
        else
          page.sendEvent "keypress", request.key

testfile = path.join(path.dirname(system.args[0]), 'dom_tests.html')
page.open testfile, (status) ->
  if status != 'success'
    console.log 'Unable to load tests.'
    phantom.exit 1

  runTests = ->
    testsFailed = page.evaluate ->
      Tests.run()
      return Tests.testsFailed

    if system.args[1] == '--coverage'
      data = page.evaluate -> JSON.stringify _$jscoverage
      fs.write dirname + 'dom_tests_coverage.json', data, 'w'

    if testsFailed > 0
      phantom.exit 1
    else
      phantom.exit 0

  # We add a short delay to allow asynchronous initialization (that is, initialization which happens on
  # "nextTick") to complete.
  setTimeout runTests, 10