#!/usr/bin/env ruby
#
# == DWatir
#
# DWatir is a bit of software distributed as a Rails plugin which provides DRb
# access to remote WATiR objects, potentially running on other physical (or
# virtual) machines running any combintation of browsers and operating systems
# supported by WATiR[http://wtr.rubyforge.org] or its permutations
# (SafariWatir[http://safariwatir.rubyforge.org/],
# FireWatir[http://code.google.com/p/firewatir/], etc.)
#
# For more information, please build and view the rdoc documentation by running:
#
# rake rdoc
#
# == Installation
#
# From your $RAILS_APP directory, run
#
# script/plugin install { http://dwatir.rubyforge.org/svn/tags/dwatir_current }[http://dwatir.rubyforge.org/svn/tags/dwatir_current]
#
# == Plugin Structure
#
# DWatir is built on Rinda[http://segment7.net/projects/ruby/drb/rinda/ringserver.html]
# and when using DWatir, first run bin/run_registry.rb once to run a registry
# process, and then run bin/run_server.rb one or more times (on the local
# machine, or on a network-connected machine) to serve up DRb-remoted WATiR browser
# objects. The bin/run_client.rb script serves as an example of how to use
# the registry to locate and use DWatir objects to remote-control browser windows.
#
# Running each of the bin/run*rb scripts will print out a help message on
# which arguments are expected by the script.
#
# Copyright: (c) 2007 by Dane G. Avilla
#
# Version: 0.1.0
#
# Licence: Ruby license
#
# Thanks to the Rails community.
#
require 'rinda/ring'
module Watir
#
# This class provides a simple interface to the Watir::DWatir
# distributed web browser remote control system.
#
# To use this file, first run 'run_registry.rb', then run 'run_server.rb'
# at least one time. Then, with those processes running, run this
# file to see an example of how DWatir may be used to connect to distributed
# browsers for cross-platform testing of web applications.
#
# For more information, please build and view the rdoc documentation by running:
#
# rake rdoc
#
# from $DWATIR_PLUGIN_DIR.
#
class DWatir
attr_accessor :browsers
def initialize(rinda_registry_ip, drb_interface_ip = nil)
# Start DRb on a network interface which can "see" the Rinda registry
# process for DWatir. Look for an IP specified as an argument. If none
# exists, use the first IP address found by Ruby.
drb_ip = drb_interface_ip || Socket.getaddrinfo(Socket.gethostname, 80)[0][3]
drb_url = "druby://#{drb_ip}:0"
drb = DRb.start_service(drb_url)
rinda_url = rinda_registry_ip.split(":")
rinda_ip = rinda_url[0]
port = rinda_url[1] || Rinda::Ring_PORT
ring_server = Rinda::RingFinger.new([rinda_ip], port)
@browsers = []
# For each tuple in the ring server, create a new browser object and
# save a reference to that object in the @browsers variable.
ring_server.each { |ts|
ts.read_all([:name, :WatirServer, nil, nil]).each { |dwatir|
@browsers << dwatir[2].new_browser
}
}
end
end
end
#
# Running this file ('ruby dwatir.rb') will execute a short test with all
# registered and running DWatir browsers.
#
if __FILE__ == $0
if ARGV.size < 1
puts "Usage: run_server.rb rinda_registry_ip[:port] [drb_interface_ip]"
puts "Example: run_server.rb 10.211.55.2"
puts "Example: run_server.rb 10.211.55.2:12345 10.211.55.3"
return -1
end
watir = Watir::DWatir.new(*ARGV)
threads = []
watir.browsers.each { |b|
threads << Thread.new do
b.goto("http://www.google.com")
b.text_field(:name, 'q').set("DWatir")
b.button(:name, "btnI").click
b.close
end
}
threads.each {|t| t.join}
end