require 'test/unit/testresult' require File.dirname(__FILE__) + '/../helpers/selenium_helper' require File.dirname(__FILE__) + '/../selenium_test_helper' require File.dirname(__FILE__) + '/../selenium_test_suites' Test::Unit.run = true class SeleniumController < ApplicationController include SeleniumHelper before_filter :require_tests, :only => [ :test, :test_suite ] # This should probably be replaced with the rails dependency loading stuff def require_tests Test::Unit::TestCase.remove_subclasses SeleniumTestSuite.clear files = Dir.glob(File.join(RAILS_ROOT, 'test/selenium/*_test.rb')) << File.join(RAILS_ROOT, 'test/selenium/suites.rb') files.each do |file| $".delete file require file if File.exists?(file) end test_unit_subclasses.each do |klass| klass.class_eval { include SeleniumTestHelper } end end def setup load_fixtures render :text => 'Setup complete' end def teardown render :text => 'Teardown complete' end def test_suite @test_suites = SeleniumTestSuite.names @test_suite_name = params[:name] test_methods = [] tests = {} if SeleniumTestSuite[@test_suite_name] test_methods.concat SeleniumTestSuite[@test_suite_name] else test_unit_subclasses.each do |klass| test_methods.concat klass.suite.tests end end test_methods.each do |method| key = method.class.name.gsub(/SeleniumTest$/, '').underscore method_name = method.method_name.to_s next if method_name == 'default_test' (tests[key] ||= []) << method_name.gsub(/^test_/, '') end render :file => view_path('TestSuite'), :locals => { :tests => tests } end def test klass = "#{params[:class].camelize}SeleniumTest".constantize method = "test_#{params[:method]}" tester = klass.new(method) result = Test::Unit::TestResult.new without_key_checks do tester.run(result) {} end if result.passed? render :file => view_path('Test'), :locals => { :name => "#{klass}##{method}", :commands => tester.selenium_commands } else raise result.instance_variable_get('@errors').first.exception end end def index redirect_to :action => :file, :file => 'TestRunner.html' end def file selenium_file = selenium_path(params[:file]) send_file selenium_file, :type => selenium_file.ends_with?('.css') ? 'text/css' : 'text/html', :disposition => 'inline' end def user_extensions file = File.join(RAILS_ROOT, 'test/selenium/user-extensions.js') if File.exists?(file) send_file file, :type => 'text/javascript', :disposition => 'inline' else render :text => "Could not find #{file}", :status => 404 end end def styles send_file view_path('styles.css'), :type => 'text/css', :disposition => 'inline' end def post_results logger.info params.inspect File.open(File.join(RAILS_ROOT, "selenium_test_results_port_#{request.port}.log"), 'w') do |f| f.write <<-END Finished in #{params[:totalTime]} seconds #{params[:numCommandPasses] + params[:numCommandFailures] + params[:numCommandErrors]} commands, #{params[:numCommandFailures]} failures, #{params[:numCommandErrors]} errors #{params[:numTestPasses] + params[:numTestFailures]} tests, #{params[:numTestFailures]} failures END end logger.info params.inspect Process.kill 9, Process.pid end end