Class: JavaScriptTestTask

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

Constant Summary collapse

BROWSERS =
%w( firefox chromium ).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/zfben_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/zfben_hanoi/javascript_test_task.rb', line 3

def sources_directory
  @sources_directory
end

Instance Method Details

#browser(browser) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/zfben_hanoi/javascript_test_task.rb', line 90

def browser(browser)
  browser =
    case(browser)
      when :firefox
        Firefox.new
      when :chromium
        Chromium.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
59
60
# File 'lib/zfben_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 unless @test_suite_results.failed?
      else
        puts "\nSkipping #{browser}, not supported on this OS or not installed."
      end
    end

    destroy_temp_directory
    @server.shutdown
    t.join

    exit 1 if @test_suite_results.failed?
  end
end

#get_url(test) ⇒ Object



62
63
64
65
66
# File 'lib/zfben_hanoi/javascript_test_task.rb', line 62

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



76
77
78
79
80
81
# File 'lib/zfben_hanoi/javascript_test_task.rb', line 76

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



86
87
88
# File 'lib/zfben_hanoi/javascript_test_task.rb', line 86

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

#setup(sources_directory, test_cases, browsers) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/zfben_hanoi/javascript_test_task.rb', line 68

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