Changeset 18938

Show
Ignore:
Timestamp:
09/07/08 15:07:28 (3 months ago)
Author:
yugui
Message:

now configurable by config/selenium_rc.yml.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/ruby/selenium_rc_spec/lib/spec/selenium_rc.rb

    r11214 r18938  
    11require 'spec/rails' 
    22require 'selenium' 
     3require 'yaml' 
     4require 'erb' 
    35 
    46module Spec 
    57  module SeleniumRc 
     8    CONFIG_PATH = RAILS_ROOT + '/config/selenium_rc.yml' 
     9    DEFAULT_CONFIG = { 
     10      :app => { 
     11        :host => 'localhost', 
     12        :binding => '127.0.0.1', 
     13        :port => 4545, 
     14        :environment => 'test', 
     15      }.freeze, 
     16      :seleniumrc => { 
     17        :host => 'localhost', 
     18        :port => 4444, 
     19        :browsers => %w[ *firefox ].freeze 
     20      }.freeze 
     21    }.freeze 
     22 
     23    CONFIG = Module.new.module_eval do 
     24      if File.exist?(CONFIG_PATH) 
     25        yaml = File.open(CONFIG_PATH) {|f| 
     26          ERB.new(f.read).result(binding) 
     27        } 
     28        user_config = YAML.load(yaml).symbolize_keys 
     29        DEFAULT_CONFIG.merge \ 
     30          :app => user_config[:app].symbolize_keys, 
     31          :seleniumrc => user_config[:seleniumrc].symbolize_keys 
     32      else 
     33        DEFAULT_CONFIG 
     34      end 
     35    end 
     36 
    637    module Example 
    738      class SeleniumRcExampleGroup < Spec::Rails::Example::RailsExampleGroup 
     
    1950        def create_server 
    2051          @server_pid = Process.fork do 
    21             exec File.join(RAILS_ROOT, *%w[ script server ]), *%w[-p 4545 -e test] 
     52            config = CONFIG[:app] 
     53            host, port, env = config[:binding], config[:port], config[:environment] 
     54            server = File.join(RAILS_ROOT, *%w[ script server ]) 
     55            exec server, *%W[-b #{host} -p #{port} -e #{env}] 
    2256          end 
    2357          catch(:server_connected) { 
     
    3064        end 
    3165        def create_proxy 
    32           @@selenium = Selenium::SeleniumDriver.new('localhost', 4444, "*firefox", 'http://localhost:4545') 
     66          rc_config = CONFIG[:seleniumrc] 
     67          host, port, browser = rc_config[:host], rc_config[:port], rc_config[:browsers].first 
     68          app_url = "http://#{CONFIG[:app][:host]}:#{CONFIG[:app][:port]}" 
     69          @@selenium = Selenium::SeleniumDriver.new(host, port, browser, app_url) 
    3370          @@selenium.start 
    3471          @@selenium.set_context("test_new") 
     
    4481        private 
    4582        def server_connectable? 
    46           TCPSocket.open('localhost', 4545) { 
     83          host = CONFIG[:app][:host] 
     84          TCPSocket.open(CONFIG[:app][:host], CONFIG[:app][:port]) { 
    4785            return true 
    4886          } 
     
    125163          wait_for_page_to_load(timeout) 
    126164        end 
     165        def clicks_and_waits(locator, timeout = 3000) 
     166          click(locator) 
     167          wait_for_page_to_load(timeout) 
     168        end 
     169 
     170        # by http://twitter.com/nazoking/statuses/83616812 
     171        def waits_for_ajax_loaded(timeout = 3000) 
     172          wait_for_condition(<<-JAVASCRIPT, timeout) 
     173            selenium.page().currentWindow.Ajax.activeRequestCount==0 
     174          JAVASCRIPT 
     175        end 
    127176      end 
    128177    end