Class: JavaScriptTestTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/hanoi/javascript_test_task.rb

Constant Summary collapse

BROWSERS =
%w( chrome safari firefox ie konqueror opera webkit ).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = :test) {|_self| ... } ⇒ JavaScriptTestTask

Returns a new instance of JavaScriptTestTask.

Yields:

  • (_self)

Yield Parameters:



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/hanoi/javascript_test_task.rb', line 5

def initialize(name = :test)
  @name = name
  @tests = []
  @browsers = []

  @queue = Queue.new

  @server = WEBrick::HTTPServer.new(:Port => 4711) # TODO: make port configurable
  @server.mount_proc("/results") do |req, res|
    @queue.push(req)
    res.body = "OK"
  end
  @server.mount("/response", BasicServlet)
  @server.mount("/slow", SlowServlet)
  @server.mount("/down", DownServlet)
  @server.mount("/inspect", InspectionServlet)
  yield self if block_given?
  define
end

Instance Attribute Details

#sources_directoryObject (readonly)

Returns the value of attribute sources_directory.



3
4
5
# File 'lib/hanoi/javascript_test_task.rb', line 3

def sources_directory
  @sources_directory
end

Instance Method Details

#browser(browser) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/hanoi/javascript_test_task.rb', line 88

def browser(browser)
  browser =
    case(browser)
      when :chrome
        Chrome.new
      when :firefox
        Firefox.new
      when :safari
        Safari.new
      when :ie
        InternetExplorer.new
      when :konqueror
        Konqueror.new
      when :opera
        Opera.new
      when :webkit
        Webkit.new
      else
        browser
    end

  @browsers << browser
end

#defineObject



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
# File 'lib/hanoi/javascript_test_task.rb', line 25

def define
  task @name do
    trap("INT") { @server.shutdown; exit }
    t = Thread.new { @server.start }

    # run all combinations of browsers and tests
    @browsers.each do |browser|
      if browser.runnable?
        t0 = Time.now
        test_suite_results = TestSuiteResults.new

        browser.setup
        puts "\nStarted tests in #{browser}."

        @tests.each do |test|
          browser.visit(get_url(test))
          results = TestResults.new(@queue.pop.query, test[:url])
          print results
          test_suite_results << results
        end

        print "\nFinished in #{Time.now - t0} seconds."
        print test_suite_results
        browser.teardown
      else
        puts "\nSkipping #{browser}, not supported on this OS or not installed."
      end
    end

    destroy_temp_directory
    @server.shutdown
    t.join
  end
end

#get_url(test) ⇒ Object



60
61
62
63
64
# File 'lib/hanoi/javascript_test_task.rb', line 60

def get_url(test)
  params = "resultsURL=http://localhost:4711/results&t=" + ("%.6f" % Time.now.to_f)
  params << "&tests=#{test[:testcases]}" unless test[:testcases] == :all
  "http://localhost:4711#{test[:url]}?#{params}"
end

#mount(path, dir = nil) ⇒ Object



74
75
76
77
78
79
# File 'lib/hanoi/javascript_test_task.rb', line 74

def mount(path, dir = nil)
  dir = current_directory + path unless dir

  # don't cache anything in our tests
  @server.mount(path, NonCachingFileHandler, dir)
end

#run(url, testcases = :all) ⇒ Object

test should be specified as a hash of the form => “url”, :testcases => “testFoo,testBar”. specifying :testcases is optional



84
85
86
# File 'lib/hanoi/javascript_test_task.rb', line 84

def run(url, testcases = :all)
  @tests << { :url => url, :testcases => testcases }
end

#setup(sources_directory, test_cases, browsers) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/hanoi/javascript_test_task.rb', line 66

def setup(sources_directory, test_cases, browsers)
  @sources_directory = sources_directory
  test_cases = setup_tests(test_cases)
  run_test_cases(test_cases)
  setup_mount_paths
  setup_browsers(browsers)
end