トップ «前の日記(2005-03-24) 最新 次の日記(2005-03-26)» 編集

3 日坊主日記


2005-03-25 [長年日記]

_ [Rails][Selenium] with Selenium

Selenium で test したいということで仕込んでみました。

  • lib/webrick_server_with_selenium.rb:
 require 'webrick_server'
 require 'selenium'

 class SeleneseDispatchServlet < DispatchServlet
   def self.dispatch(options = {})
     Socket.do_not_reverse_lookup = true # patch for OS X

     server = WEBrick::HTTPServer.new(:Port => options[:port].to_i, :ServerType => options[:server_type], :BindAddress => options[:ip],
       :Logger => WEBrick::BasicLog.new(nil, WEBrick::BasicLog::WARN), #comment out to enable server logging
       :AccessLog => {} #comment out to enable access logging
     )
     server.mount('/', DispatchServlet, options)

     @timeout = options[:timeout].to_i || 1000
     @in_queue = Queue.new
     @out_queue = Queue.new

     # Selenium's static files and dynamic handler
     server.mount("/selenium-driver", Selenium::NonCachingFileHandler, File.expand_path(File.dirname(__FILE__) + "/selenium-driver"))
     server.mount_proc("/selenium-driver/driver") do |req, res|
       res["Cache-control"] = "no-cache"
       res["Pragma"] = "no-cache"
       res["Expires"] = "-1"

       # Only handle "GET" for now
       if "GET" == req.request_method
         command_result = req.query['commandResult']
         selenium_start = req.query['seleniumStart']
         # puts "--> #{command_result} , #{selenium_start}"
         @in_queue.push(command_result) unless selenium_start
         get_reply = @out_queue.pop
         res.body = get_reply
       end
     end

     trap("INT") { server.shutdown }
     server.start
   end

   def self.proxy
     Selenium::SeleneseInterpreter.new(@in_queue, @out_queue, @timeout)
   end

 end
  • script/server:
 #!/usr/local/bin/ruby

 require 'webrick'
 require 'optparse'

 OPTIONS = {
   :port        => 3000,
   :ip          => "127.0.0.1",
   :environment => "development",
   :server_root => File.expand_path(File.dirname(__FILE__) + "/../public/"),
   :server_type => WEBrick::SimpleServer
 }

 ARGV.options do |opts|
   script_name = File.basename($0)
   opts.banner = "Usage: ruby #{script_name} [options]"

   opts.separator ""

   opts.on("-p", "--port=port", Integer,
           "Runs Rails on the specified port.",
           "Default: 3000") { |OPTIONS[:port]| }
   opts.on("-b", "--binding=ip", String,
           "Binds Rails to the specified ip.",
           "Default: 127.0.0.1") { |OPTIONS[:ip]| }
   opts.on("-i", "--index=controller", String,
           "Specifies an index controller that requests for root will go to (instead of congratulations screen)."
           ) { |OPTIONS[:index_controller]| }
   opts.on("-e", "--environment=name", String,
           "Specifies the environment to run this server under (test/development/production).",
           "Default: development") { |OPTIONS[:environment]| }
   opts.on("-d", "--daemon",
           "Make Rails run as a Daemon (only works if fork is available -- meaning on *nix)."
           ) { OPTIONS[:server_type] = WEBrick::Daemon }
   opts.on("-t", "--test",
           "Driven Selenium."
           ) { |OPTIONS[:selenese]| }

   opts.separator ""

   opts.on("-h", "--help",
           "Show this help message.") { puts opts; exit }

   opts.parse!
 end

 ENV["RAILS_ENV"] = OPTIONS[:environment]
 require File.dirname(__FILE__) + "/../config/environment"
 require 'webrick_server_with_selenium'

 OPTIONS['working_directory'] = File.expand_path(RAILS_ROOT)

 puts "=> Rails application started on http://#{OPTIONS[:ip]}:#{OPTIONS[:port]}"
 if OPTIONS[:selenese]
   OPTIONS[:server_type] = Thread
   SeleneseDispatchServlet.dispatch(OPTIONS)
   require 'test/selenese_runner'
 else
   DispatchServlet.dispatch(OPTIONS)
 end
  • test/selenese_runner.rb:
 selenium = SeleneseDispatchServlet.proxy
 browser = Selenium::WindowsIEBrowserLauncher.new
 browser.launch("http://#{OPTIONS[:ip]}:#{OPTIONS[:port]}/selenium-driver/SeleneseRunner.html")

 # Send some commands to the browser
 puts selenium.open('/login')
 i = Iconv.open("UTF-8", "Shift_JIS")
 u = i.iconv('衣山')
 puts selenium.verify_value('shop_name', u)
 puts selenium.test_complete()

 browser.close

Selenium イイ!

Selenium 側もひとつ修正。 UTF-8 な query が通らないので (uri.rb) encodeURI が必要。

  • SeleneseRunner.html:
  function nextCommand() {

     var xmlHttp = XmlHttp.create();

     try {
         xmlHttp.open("GET", "driver?commandResult=" + encodeURI(postResult), false);
         xmlHttp.send(null);
     } catch(e) {
         return null;
     }
     return extractCommand(xmlHttp);
  }
本日のツッコミ(全1件) [ツッコミを入れる]
# amebraObesots (2007-10-06 17:49)

hey there awesome site!<br> hey hey hey<br>this is me and me only yo yo<br>123tytytyywywyw

[]