Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 5139

Show
Ignore:
Timestamp:
09/18/06 10:33:32 (2 years ago)
Author:
madrobby
Message:

Add JavaScriptTest namespace wrapper

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/javascript_test/CHANGELOG

    r5138 r5139  
    11*SVN* 
     2 
     3* Add JavaScriptTest namespace wrapper 
    24 
    35* Added test for JavaScript test runner 
  • plugins/javascript_test/lib/javascript_test.rb

    r5138 r5139  
    33require 'webrick' 
    44 
    5 class Browser 
    6   def supported?; true; end 
    7   def setup ; end 
    8   def open(url) ; end 
    9   def teardown ; end 
    10  
    11   def host 
    12     require 'rbconfig' 
    13     Config::CONFIG['host'] 
    14   end 
    15    
    16   def macos? 
    17     host.include?('darwin') 
    18   end 
    19    
    20   def windows? 
    21     host.include?('mswin') 
    22   end 
    23    
    24   def linux? 
    25     host.include?('linux') 
    26   end 
    27    
    28   def applescript(script) 
    29     raise "Can't run AppleScript on #{host}" unless macos? 
    30     system "osascript -e '#{script}' 2>&1 >/dev/null" 
    31   end 
    32 end 
    33  
    34 class FirefoxBrowser < Browser 
    35   def initialize(path='c:\Program Files\Mozilla Firefox\firefox.exe') 
    36     @path = path 
    37   end 
    38  
    39   def visit(url) 
    40     applescript('tell application "Firefox" to Get URL "' + url + '"') if macos?  
    41     system("#{@path} #{url}") if windows?  
    42     system("firefox #{url}") if linux? 
    43   end 
    44  
    45   def to_s 
    46     "Firefox" 
    47   end 
    48 end 
    49  
    50 class SafariBrowser < Browser 
    51   def supported? 
    52     macos? 
    53   end 
    54    
    55   def setup 
    56     applescript('tell application "Safari" to make new document') 
    57   end 
    58    
    59   def visit(url) 
    60     applescript('tell application "Safari" to set URL of front document to "' + url + '"') 
    61   end 
    62  
    63   def teardown 
    64     #applescript('tell application "Safari" to close front document') 
    65   end 
    66  
    67   def to_s 
    68     "Safari" 
    69   end 
    70 end 
    71  
    72 class IEBrowser < Browser 
    73   def initialize(path='C:\Program Files\Internet Explorer\IEXPLORE.EXE') 
    74     @path = path 
    75   end 
    76    
    77   def setup 
    78     if windows? 
    79       puts %{ 
    80         MAJOR ANNOYANCE on Windows. 
    81         You have to shut down the Internet Explorer manually after each test 
    82         for the script to proceed. 
    83         Any suggestions on fixing this is GREATLY appreaciated! 
    84         Thank you for your understanding. 
    85       } 
    86     end 
    87   end 
    88  
    89   def supported? 
    90     windows? 
    91   end 
    92    
    93   def visit(url) 
    94     system("#{@path} #{url}") if windows?  
    95   end 
    96  
    97   def to_s 
    98     "Internet Explorer" 
    99   end 
    100 end 
    101  
    102 class KonquerorBrowser < Browser 
    103   def supported? 
    104     linux? 
    105   end 
    106    
    107   def visit(url) 
    108     system("kfmclient openURL #{url}") 
    109   end 
    110    
    111   def to_s 
    112     "Konqueror" 
    113   end 
    114 end 
    115  
    116 # shut up, webrick :-) 
    117 class ::WEBrick::HTTPServer 
    118   def access_log(config, req, res) 
    119     # nop 
    120   end 
    121 end 
    122 class ::WEBrick::BasicLog 
    123   def log(level, data) 
    124     # nop 
    125   end 
    126 end 
    127  
    128 class NonCachingFileHandler < WEBrick::HTTPServlet::FileHandler 
    129   def do_GET(req, res) 
    130     super 
    131     res['etag'] = nil 
    132     res['last-modified'] = Time.now + 1000 
    133     res['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0"' 
    134     res['Pragma'] = 'no-cache' 
    135     res['Expires'] = Time.now - 1000 
    136   end 
    137 end 
    138  
    139 class JavaScriptTestrunner 
    140  
    141   def initialize(name=:test) 
    142     @name = name 
    143     @tests = [] 
    144     @browsers = [] 
    145     @result = true 
    146  
    147     @queue = Queue.new 
    148  
    149     result = [] 
    150  
    151     @server = WEBrick::HTTPServer.new(:Port => 4711) # TODO: make port configurable 
    152     @server.mount_proc("/results") do |req, res| 
    153       @queue.push(req.query['result']) 
    154       res.body = "OK" 
    155     end 
    156     yield self if block_given? 
    157      
    158     define 
    159   end 
    160    
    161   def successful? 
    162     @result 
    163   end 
    164  
    165   def define 
    166     trap("INT") { @server.shutdown } 
    167     t = Thread.new { @server.start } 
    168      
    169     # run all combinations of browsers and tests 
    170     @browsers.each do |browser| 
    171       if browser.supported? 
    172         browser.setup 
    173         @tests.each do |test| 
    174           browser.visit("http://localhost:4711#{test}?resultsURL=http://localhost:4711/results&t=" + ("%.6f" % Time.now.to_f)) 
    175           result = @queue.pop 
    176           puts "#{test} on #{browser}: #{result}" 
    177           @result = false unless result == 'SUCCESS' 
     5class JavaScriptTest 
     6   
     7  class Browser 
     8    def supported?; true; end 
     9    def setup ; end 
     10    def open(url) ; end 
     11    def teardown ; end 
     12   
     13    def host 
     14      require 'rbconfig' 
     15      Config::CONFIG['host'] 
     16    end 
     17     
     18    def macos? 
     19      host.include?('darwin') 
     20    end 
     21     
     22    def windows? 
     23      host.include?('mswin') 
     24    end 
     25     
     26    def linux? 
     27      host.include?('linux') 
     28    end 
     29     
     30    def applescript(script) 
     31      raise "Can't run AppleScript on #{host}" unless macos? 
     32      system "osascript -e '#{script}' 2>&1 >/dev/null" 
     33    end 
     34  end 
     35   
     36  class FirefoxBrowser < Browser 
     37    def initialize(path='c:\Program Files\Mozilla Firefox\firefox.exe') 
     38      @path = path 
     39    end 
     40   
     41    def visit(url) 
     42      applescript('tell application "Firefox" to Get URL "' + url + '"') if macos?  
     43      system("#{@path} #{url}") if windows?  
     44      system("firefox #{url}") if linux? 
     45    end 
     46   
     47    def to_s 
     48      "Firefox" 
     49    end 
     50  end 
     51   
     52  class SafariBrowser < Browser 
     53    def supported? 
     54      macos? 
     55    end 
     56     
     57    def setup 
     58      applescript('tell application "Safari" to make new document') 
     59    end 
     60     
     61    def visit(url) 
     62      applescript('tell application "Safari" to set URL of front document to "' + url + '"') 
     63    end 
     64   
     65    def teardown 
     66      #applescript('tell application "Safari" to close front document') 
     67    end 
     68   
     69    def to_s 
     70      "Safari" 
     71    end 
     72  end 
     73   
     74  class IEBrowser < Browser 
     75    def initialize(path='C:\Program Files\Internet Explorer\IEXPLORE.EXE') 
     76      @path = path 
     77    end 
     78     
     79    def setup 
     80      if windows? 
     81        puts %{ 
     82          MAJOR ANNOYANCE on Windows. 
     83          You have to shut down the Internet Explorer manually after each test 
     84          for the script to proceed. 
     85          Any suggestions on fixing this is GREATLY appreaciated! 
     86          Thank you for your understanding. 
     87        } 
     88      end 
     89    end 
     90   
     91    def supported? 
     92      windows? 
     93    end 
     94     
     95    def visit(url) 
     96      system("#{@path} #{url}") if windows?  
     97    end 
     98   
     99    def to_s 
     100      "Internet Explorer" 
     101    end 
     102  end 
     103   
     104  class KonquerorBrowser < Browser 
     105    def supported? 
     106      linux? 
     107    end 
     108     
     109    def visit(url) 
     110      system("kfmclient openURL #{url}") 
     111    end 
     112     
     113    def to_s 
     114      "Konqueror" 
     115    end 
     116  end 
     117   
     118  # shut up, webrick :-) 
     119  class ::WEBrick::HTTPServer 
     120    def access_log(config, req, res) 
     121      # nop 
     122    end 
     123  end 
     124  class ::WEBrick::BasicLog 
     125    def log(level, data) 
     126      # nop 
     127    end 
     128  end 
     129   
     130  class NonCachingFileHandler < WEBrick::HTTPServlet::FileHandler 
     131    def do_GET(req, res) 
     132      super 
     133      res['etag'] = nil 
     134      res['last-modified'] = Time.now + 1000 
     135      res['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0"' 
     136      res['Pragma'] = 'no-cache' 
     137      res['Expires'] = Time.now - 1000 
     138    end 
     139  end 
     140   
     141  class Runner 
     142   
     143    def initialize(name=:test) 
     144      @name = name 
     145      @tests = [] 
     146      @browsers = [] 
     147      @result = true 
     148   
     149      @queue = Queue.new 
     150   
     151      result = [] 
     152   
     153      @server = WEBrick::HTTPServer.new(:Port => 4711) # TODO: make port configurable 
     154      @server.mount_proc("/results") do |req, res| 
     155        @queue.push(req.query['result']) 
     156        res.body = "OK" 
     157      end 
     158      yield self if block_given? 
     159       
     160      define 
     161    end 
     162     
     163    def successful? 
     164      @result 
     165    end 
     166   
     167    def define 
     168      trap("INT") { @server.shutdown } 
     169      t = Thread.new { @server.start } 
     170       
     171      # run all combinations of browsers and tests 
     172      @browsers.each do |browser| 
     173        if browser.supported? 
     174          browser.setup 
     175          @tests.each do |test| 
     176            browser.visit("http://localhost:4711#{test}?resultsURL=http://localhost:4711/results&t=" + ("%.6f" % Time.now.to_f)) 
     177            result = @queue.pop 
     178            puts "#{test} on #{browser}: #{result}" 
     179            @result = false unless result == 'SUCCESS' 
     180          end 
     181          browser.teardown 
     182        else 
     183          puts "Skipping #{browser}, not supported on this OS" 
    178184        end 
    179185        browser.teardown 
    180       else 
    181         puts "Skipping #{browser}, not supported on this OS" 
    182       end 
    183       browser.teardown 
    184     end 
    185      
    186     @server.shutdown 
    187     t.join 
     186      end 
     187       
     188      @server.shutdown 
     189      t.join 
     190    end 
     191   
     192    def mount(path, dir=nil) 
     193      dir ||= (Dir.pwd + path) 
     194   
     195      @server.mount(path, NonCachingFileHandler, dir) 
     196    end 
     197   
     198    # test should be specified as a url 
     199    def run(test) 
     200      url = "/test/javascript/#{test}_test.html" 
     201      unless File.exists?(RAILS_ROOT+url) 
     202        raise "Missing test file #{url} for #{test}" 
     203      end 
     204      @tests << url 
     205    end 
     206   
     207    def browser(browser) 
     208      browser = 
     209        case(browser) 
     210          when :firefox 
     211            FirefoxBrowser.new 
     212          when :safari 
     213            SafariBrowser.new 
     214          when :ie 
     215            IEBrowser.new 
     216          when :konqueror 
     217            KonquerorBrowser.new 
     218          else 
     219            browser 
     220        end 
     221   
     222      @browsers<<browser 
     223    end 
    188224  end 
    189225 
    190   def mount(path, dir=nil) 
    191     dir ||= (Dir.pwd + path) 
    192  
    193     @server.mount(path, NonCachingFileHandler, dir) 
    194   end 
    195  
    196   # test should be specified as a url 
    197   def run(test) 
    198     url = "/test/javascript/#{test}_test.html" 
    199     unless File.exists?(RAILS_ROOT+url) 
    200       raise "Missing test file #{url} for #{test}" 
    201     end 
    202     @tests << url 
    203   end 
    204  
    205   def browser(browser) 
    206     browser = 
    207       case(browser) 
    208         when :firefox 
    209           FirefoxBrowser.new 
    210         when :safari 
    211           SafariBrowser.new 
    212         when :ie 
    213           IEBrowser.new 
    214         when :konqueror 
    215           KonquerorBrowser.new 
    216         else 
    217           browser 
    218       end 
    219  
    220     @browsers<<browser 
    221   end 
    222226end 
  • plugins/javascript_test/tasks/javascript_test.rake

    r5072 r5139  
    11desc "Run tests for JavaScripts" 
    22task 'test:javascripts' => :environment do 
    3   JavaScriptTestrunner.new do |t|  
     3  JavaScriptTest::Runner.new do |t|  
    44     
    55    t.mount("/", RAILS_ROOT) 
  • plugins/javascript_test/test/javascript_test_test.rb

    r5138 r5139  
    77class JavascriptTestTest < Test::Unit::TestCase 
    88   
    9   def test_javascript_testrunner 
    10     runner = JavaScriptTestrunner.new do |t|  
     9  def test_javascript_test_runner 
     10    runner = JavaScriptTest::Runner.new do |t|  
    1111      t.mount("/test", RAILS_ROOT+'/test') 
    1212      t.mount('/test/javascript/assets', RAILS_ROOT+'/../assets')