watirgrid 0.0.7 → 0.0.8.pre

Sign up to get free protection for your applications and to get access to all the features.
data/lib/watirgrid.rb CHANGED
@@ -1,167 +1,167 @@
1
- require 'rubygems'
2
- require 'controller'
3
- require 'provider'
4
-
5
- module Watir
6
-
7
- ##
8
- # Extend Watir with a Grid class which
9
- # implements a grid of browsers by connecting to a tuplespace
10
- # and instatiating remote browser objects on nominated providers.
11
- class Grid
12
-
13
- attr_accessor :drb_server_uri, :ring_server, :browsers, :tuples
14
-
15
- def initialize(params = {})
16
- @drb_server_host = params[:drb_server_host] || external_interface
17
- @drb_server_port = params[:drb_server_port] || 0
18
- @ring_server_host = params[:ring_server_host] || external_interface
19
- @ring_server_port = params[:ring_server_port] || Rinda::Ring_PORT
20
- @renewer = params[:renewer] || Rinda::SimpleRenewer.new
21
-
22
- logfile = params[:logfile] || STDOUT
23
- @log = Logger.new(logfile, 'daily')
24
- @log.level = params[:loglevel] || Logger::ERROR
25
- @log.datetime_format = "%Y-%m-%d %H:%M:%S "
26
-
27
- @browsers = []
28
- @tuples = []
29
- end
30
-
31
- ##
32
- # Start required services
33
- def start(params = {})
34
- start_drb_server
35
- find_ring_server
36
- get_tuples(params)
37
- end
38
-
39
- ##
40
- # Return the size (quantity) of browsers started on the grid
41
- def size
42
- @browsers.size
43
- end
44
-
45
- ##
46
- # Write tuple back to tuplespace when finished using it
47
- def release_tuples
48
- @tuples.each { |tuple| @ring_server.write(tuple) }
49
- end
50
-
51
- private
52
-
53
- ##
54
- # Get the external facing interface for this server
55
- def external_interface
56
- begin
57
- UDPSocket.open {|s| s.connect('watir.com', 1); s.addr.last }
58
- rescue
59
- '127.0.0.1'
60
- end
61
- end
62
-
63
- ##
64
- # Start the DRb Server
65
- def start_drb_server
66
- drb_server = DRb.start_service(
67
- "druby://#{@drb_server_host}:#{@drb_server_port}")
68
- @drb_server_uri = drb_server.uri
69
- @log.info("DRb server started on : #{@drb_server_uri}")
70
- end
71
-
72
- ##
73
- # Locate the Rinda Ring Server via a UDP broadcast
74
- def find_ring_server
75
- @ring_server = Rinda::RingFinger.new(
76
- @ring_server_host, @ring_server_port)
77
- @ring_server = @ring_server.lookup_ring_any
78
- @log.info("Ring server found on : druby://#{@ring_server_host}:#{@ring_server_port}")
79
- end
80
-
81
- ##
82
- # Get all tuple spaces on ringserver
83
- def get_tuples(params = {})
84
- quantity = calculate_quantity(params[:quantity])
85
- read_tuples(params)
86
- @log.info("Found #{@tuples.size} tuples.")
87
- if @tuples.size > -1 then
88
- @tuples[0..quantity].each do |tuple|
89
- if params[:hostnames]
90
- filter_tuple_by_hostname(tuple, params)
91
- else
92
- add_tuple_to_browsers(tuple)
93
- take_tuple(tuple) if params[:take_all] == true
94
- end
95
- end
96
- end
97
- end
98
-
99
- ##
100
- # Sets the quantity (upper limit of array) of tuples to retrieve
101
- # This is because some users prefer not to specify a zero based
102
- # index when asking for n browsers
103
- def calculate_quantity(quantity)
104
- if (quantity.nil? or quantity == 0) then
105
- quantity = -1
106
- else
107
- quantity -= 1
108
- end
109
- end
110
-
111
- ##
112
- # Read all tuples filtered by architecture and browser type
113
- # then populate the tuples accessor
114
- def read_tuples(params={})
115
- @tuples = @ring_server.read_all([
116
- :WatirGrid,
117
- nil, # watir provider
118
- nil, # browser front object
119
- nil, # provider description
120
- nil, # hostname
121
- params[:architecture],
122
- params[:browser_type],
123
- params[:uuid]
124
- ])
125
- end
126
-
127
- ##
128
- # Filter tuple by hostnames
129
- def filter_tuple_by_hostname(tuple, params={})
130
- hostname = tuple[4]
131
- if (params[:hostnames][hostname]) then
132
- add_tuple_to_browsers(tuple)
133
- take_tuple(tuple) if params[:take_all] == true
134
- end
135
- end
136
-
137
- ##
138
- # Add a tuple to the browsers accessor
139
- def add_tuple_to_browsers(tuple)
140
- @browsers << tuple_to_hash(tuple)
141
- end
142
-
143
- ##
144
- # Take a tuple from the tuple space
145
- def take_tuple(tuple)
146
- @ring_server.take(tuple)
147
- end
148
-
149
- ##
150
- # Convert tuple into a hash for easier handling
151
- def tuple_to_hash(tuple)
152
- tuple_hash = {}
153
- tuple_hash[:name] = tuple[0]
154
- tuple_hash[:class] = tuple[1]
155
- tuple_hash[:object] = tuple[2]
156
- tuple_hash[:description] = tuple[3]
157
- tuple_hash[:hostname] = tuple[4]
158
- tuple_hash[:architecture] = tuple[5]
159
- tuple_hash[:browser_type] = tuple[6]
160
- tuple_hash[:uuid] = tuple[7]
161
- tuple_hash
162
- end
163
-
164
- end
165
-
166
- end
167
-
1
+ require 'rubygems'
2
+ require 'controller'
3
+ require 'provider'
4
+
5
+ module Watir
6
+
7
+ ##
8
+ # Extend Watir with a Grid class which
9
+ # implements a grid of browsers by connecting to a tuplespace
10
+ # and instatiating remote browser objects on nominated providers.
11
+ class Grid
12
+
13
+ attr_accessor :drb_server_uri, :ring_server, :browsers, :tuples
14
+
15
+ def initialize(params = {})
16
+ @drb_server_host = params[:drb_server_host] || external_interface
17
+ @drb_server_port = params[:drb_server_port] || 0
18
+ @ring_server_host = params[:ring_server_host] || external_interface
19
+ @ring_server_port = params[:ring_server_port] || Rinda::Ring_PORT
20
+ @renewer = params[:renewer] || Rinda::SimpleRenewer.new
21
+
22
+ logfile = params[:logfile] || STDOUT
23
+ @log = Logger.new(logfile, 'daily')
24
+ @log.level = params[:loglevel] || Logger::ERROR
25
+ @log.datetime_format = "%Y-%m-%d %H:%M:%S "
26
+
27
+ @browsers = []
28
+ @tuples = []
29
+ end
30
+
31
+ ##
32
+ # Start required services
33
+ def start(params = {})
34
+ start_drb_server
35
+ find_ring_server
36
+ get_tuples(params)
37
+ end
38
+
39
+ ##
40
+ # Return the size (quantity) of browsers started on the grid
41
+ def size
42
+ @browsers.size
43
+ end
44
+
45
+ ##
46
+ # Write tuple back to tuplespace when finished using it
47
+ def release_tuples
48
+ @tuples.each { |tuple| @ring_server.write(tuple) }
49
+ end
50
+
51
+ private
52
+
53
+ ##
54
+ # Get the external facing interface for this server
55
+ def external_interface
56
+ begin
57
+ UDPSocket.open {|s| s.connect('watir.com', 1); s.addr.last }
58
+ rescue
59
+ '127.0.0.1'
60
+ end
61
+ end
62
+
63
+ ##
64
+ # Start the DRb Server
65
+ def start_drb_server
66
+ drb_server = DRb.start_service(
67
+ "druby://#{@drb_server_host}:#{@drb_server_port}")
68
+ @drb_server_uri = drb_server.uri
69
+ @log.info("DRb server started on : #{@drb_server_uri}")
70
+ end
71
+
72
+ ##
73
+ # Locate the Rinda Ring Server via a UDP broadcast
74
+ def find_ring_server
75
+ @ring_server = Rinda::RingFinger.new(
76
+ @ring_server_host, @ring_server_port)
77
+ @ring_server = @ring_server.lookup_ring_any
78
+ @log.info("Ring server found on : druby://#{@ring_server_host}:#{@ring_server_port}")
79
+ end
80
+
81
+ ##
82
+ # Get all tuple spaces on ringserver
83
+ def get_tuples(params = {})
84
+ quantity = calculate_quantity(params[:quantity])
85
+ read_tuples(params)
86
+ @log.info("Found #{@tuples.size} tuples.")
87
+ if @tuples.size > -1 then
88
+ @tuples[0..quantity].each do |tuple|
89
+ if params[:hostnames]
90
+ filter_tuple_by_hostname(tuple, params)
91
+ else
92
+ add_tuple_to_browsers(tuple)
93
+ take_tuple(tuple) if params[:take_all] == true
94
+ end
95
+ end
96
+ end
97
+ end
98
+
99
+ ##
100
+ # Sets the quantity (upper limit of array) of tuples to retrieve
101
+ # This is because some users prefer not to specify a zero based
102
+ # index when asking for n browsers
103
+ def calculate_quantity(quantity)
104
+ if (quantity.nil? or quantity == 0) then
105
+ quantity = -1
106
+ else
107
+ quantity -= 1
108
+ end
109
+ end
110
+
111
+ ##
112
+ # Read all tuples filtered by architecture and browser type
113
+ # then populate the tuples accessor
114
+ def read_tuples(params={})
115
+ @tuples = @ring_server.read_all([
116
+ :WatirGrid,
117
+ nil, # watir provider
118
+ nil, # browser front object
119
+ nil, # provider description
120
+ nil, # hostname
121
+ params[:architecture],
122
+ params[:browser_type],
123
+ params[:uuid]
124
+ ])
125
+ end
126
+
127
+ ##
128
+ # Filter tuple by hostnames
129
+ def filter_tuple_by_hostname(tuple, params={})
130
+ hostname = tuple[4]
131
+ if (params[:hostnames][hostname]) then
132
+ add_tuple_to_browsers(tuple)
133
+ take_tuple(tuple) if params[:take_all] == true
134
+ end
135
+ end
136
+
137
+ ##
138
+ # Add a tuple to the browsers accessor
139
+ def add_tuple_to_browsers(tuple)
140
+ @browsers << tuple_to_hash(tuple)
141
+ end
142
+
143
+ ##
144
+ # Take a tuple from the tuple space
145
+ def take_tuple(tuple)
146
+ @ring_server.take(tuple)
147
+ end
148
+
149
+ ##
150
+ # Convert tuple into a hash for easier handling
151
+ def tuple_to_hash(tuple)
152
+ tuple_hash = {}
153
+ tuple_hash[:name] = tuple[0]
154
+ tuple_hash[:class] = tuple[1]
155
+ tuple_hash[:object] = tuple[2]
156
+ tuple_hash[:description] = tuple[3]
157
+ tuple_hash[:hostname] = tuple[4]
158
+ tuple_hash[:architecture] = tuple[5]
159
+ tuple_hash[:browser_type] = tuple[6]
160
+ tuple_hash[:uuid] = tuple[7]
161
+ tuple_hash
162
+ end
163
+
164
+ end
165
+
166
+ end
167
+
data/spec/grid_spec.rb CHANGED
@@ -1,148 +1,148 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe 'WatirGrid' do
4
- before(:all) do
5
- controller = Controller.new(
6
- :ring_server_port => 12351,
7
- :loglevel => Logger::ERROR)
8
- controller.start
9
- 1.upto(5) do
10
- provider = Provider.new(
11
- :ring_server_port => 12351,
12
- :loglevel => Logger::ERROR, :browser_type => 'safari')
13
- provider.start
14
- end
15
- end
16
-
17
- it 'should return how many grid are available in the tuplespace' do
18
- grid = Watir::Grid.new(:ring_server_port => 12351,
19
- :ring_server_host => '127.0.0.1')
20
- grid.start(:read_all => true)
21
- grid.size.should == 5
22
- end
23
-
24
- it 'should read any 2 grid in the tuplespace' do
25
- grid = Watir::Grid.new(:ring_server_port => 12351,
26
- :ring_server_host => '127.0.0.1')
27
- grid.start(:quantity => 2, :read_all => true)
28
- grid.size.should == 2
29
- end
30
-
31
- it 'should take any 1 browser in the tuplespace' do
32
- grid = Watir::Grid.new(:ring_server_port => 12351,
33
- :ring_server_host => '127.0.0.1')
34
- grid.start(:quantity => 1, :take_all => true)
35
- grid.size.should == 1
36
- end
37
-
38
- it 'should take all grid remaining in tuplespace' do
39
- grid = Watir::Grid.new(:ring_server_port => 12351,
40
- :ring_server_host => '127.0.0.1')
41
- grid.start(:take_all => true)
42
- grid.size.should == 4
43
- end
44
-
45
- it 'should find no more grid in the tuplespace' do
46
- grid = Watir::Grid.new(:ring_server_port => 12351,
47
- :ring_server_host => '127.0.0.1')
48
- grid.start(:read_all => true)
49
- grid.size.should == 0
50
- end
51
-
52
- it 'should register 4 new grid in the tuplespace' do
53
- 1.upto(4) do
54
- provider = Provider.new(:ring_server_port => 12351,
55
- :loglevel => Logger::ERROR, :browser_type => 'safari')
56
- provider.start
57
- end
58
- end
59
-
60
- it 'should take any 1 browser based on browser type' do
61
- grid = Watir::Grid.new(:ring_server_port => 12351,
62
- :ring_server_host => '127.0.0.1')
63
- grid.start(:quantity => 1,
64
- :take_all => true, :browser_type => 'safari')
65
- grid.size.should == 1
66
- end
67
-
68
- it 'should fail to find any grid based on a specific browser type' do
69
- grid = Watir::Grid.new(:ring_server_port => 12351,
70
- :ring_server_host => '127.0.0.1')
71
- grid.start(:quantity => 1,
72
- :take_all => true, :browser_type => 'firefox')
73
- grid.size.should == 0
74
- end
75
-
76
- it 'should fail to find any grid based on a unknown browser type' do
77
- grid = Watir::Grid.new(:ring_server_port => 12351,
78
- :ring_server_host => '127.0.0.1')
79
- grid.start(:quantity => 1,
80
- :take_all => true, :browser_type => 'penguin')
81
- grid.size.should == 0
82
- end
83
-
84
- it 'should take any 1 browser based on specific architecture type' do
85
- grid = Watir::Grid.new(:ring_server_port => 12351,
86
- :ring_server_host => '127.0.0.1')
87
- grid.start(:quantity => 1,
88
- :take_all => true, :architecture => Config::CONFIG['arch'])
89
- grid.size.should == 1
90
- end
91
-
92
- it 'should fail to find any grid based on unknown architecture type' do
93
- grid = Watir::Grid.new(:ring_server_port => 12351,
94
- :ring_server_host => '127.0.0.1')
95
- grid.start(:quantity => 1,
96
- :take_all => true, :architecture => 'geos-1992')
97
- grid.size.should == 0
98
- end
99
-
100
- it 'should take any 1 browser based on specific hostname' do
101
- hostname = `hostname`.strip
102
- grid = Watir::Grid.new(:ring_server_port => 12351,
103
- :ring_server_host => '127.0.0.1')
104
- grid.start(:quantity => 1,
105
- :take_all => true,
106
- :hostnames => { hostname => "127.0.0.1"}
107
- )
108
- grid.size.should == 1
109
- end
110
-
111
- it 'should fail to find any grid based on unknown hostname' do
112
- grid = Watir::Grid.new(:ring_server_port => 12351,
113
- :ring_server_host => '127.0.0.1')
114
- grid.start(:quantity => 1,
115
- :take_all => true, :hostnames => {
116
- "tokyo" => "127.0.0.1"})
117
- grid.size.should == 0
118
- end
119
-
120
- it 'should take the last browser and execute some watir commands' do
121
- grid = Watir::Grid.new(:ring_server_port => 12351,
122
- :ring_server_host => '127.0.0.1')
123
- grid.start(:quantity => 1, :take_all => true)
124
- threads = []
125
- grid.browsers.each do |browser|
126
- threads << Thread.new do
127
- browser[:hostname].should == `hostname`.strip
128
- browser[:architecture].should == Config::CONFIG['arch']
129
- browser[:browser_type].should == 'safari'
130
- b = browser[:object].new_browser
131
- b.goto("http://www.google.com")
132
- b.text_field(:name, 'q').set("watirgrid")
133
- b.button(:name, "btnI").click
134
- end
135
- end
136
- threads.each {|thread| thread.join}
137
- grid.size.should == 1
138
- end
139
-
140
- it 'should find no more tuples in the tuplespace' do
141
- grid = Watir::Grid.new(:ring_server_port => 12351,
142
- :ring_server_host => '127.0.0.1')
143
- grid.start(:read_all => true)
144
- grid.size.should == 0
145
- end
146
-
147
- end
148
-
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe 'WatirGrid' do
4
+ before(:all) do
5
+ controller = Controller.new(
6
+ :ring_server_port => 12351,
7
+ :loglevel => Logger::ERROR)
8
+ controller.start
9
+ 1.upto(5) do
10
+ provider = Provider.new(
11
+ :ring_server_port => 12351,
12
+ :loglevel => Logger::ERROR, :browser_type => 'safari')
13
+ provider.start
14
+ end
15
+ end
16
+
17
+ it 'should return how many grid are available in the tuplespace' do
18
+ grid = Watir::Grid.new(:ring_server_port => 12351,
19
+ :ring_server_host => '127.0.0.1')
20
+ grid.start(:read_all => true)
21
+ grid.size.should == 5
22
+ end
23
+
24
+ it 'should read any 2 grid in the tuplespace' do
25
+ grid = Watir::Grid.new(:ring_server_port => 12351,
26
+ :ring_server_host => '127.0.0.1')
27
+ grid.start(:quantity => 2, :read_all => true)
28
+ grid.size.should == 2
29
+ end
30
+
31
+ it 'should take any 1 browser in the tuplespace' do
32
+ grid = Watir::Grid.new(:ring_server_port => 12351,
33
+ :ring_server_host => '127.0.0.1')
34
+ grid.start(:quantity => 1, :take_all => true)
35
+ grid.size.should == 1
36
+ end
37
+
38
+ it 'should take all grid remaining in tuplespace' do
39
+ grid = Watir::Grid.new(:ring_server_port => 12351,
40
+ :ring_server_host => '127.0.0.1')
41
+ grid.start(:take_all => true)
42
+ grid.size.should == 4
43
+ end
44
+
45
+ it 'should find no more grid in the tuplespace' do
46
+ grid = Watir::Grid.new(:ring_server_port => 12351,
47
+ :ring_server_host => '127.0.0.1')
48
+ grid.start(:read_all => true)
49
+ grid.size.should == 0
50
+ end
51
+
52
+ it 'should register 4 new grid in the tuplespace' do
53
+ 1.upto(4) do
54
+ provider = Provider.new(:ring_server_port => 12351,
55
+ :loglevel => Logger::ERROR, :browser_type => 'safari')
56
+ provider.start
57
+ end
58
+ end
59
+
60
+ it 'should take any 1 browser based on browser type' do
61
+ grid = Watir::Grid.new(:ring_server_port => 12351,
62
+ :ring_server_host => '127.0.0.1')
63
+ grid.start(:quantity => 1,
64
+ :take_all => true, :browser_type => 'safari')
65
+ grid.size.should == 1
66
+ end
67
+
68
+ it 'should fail to find any grid based on a specific browser type' do
69
+ grid = Watir::Grid.new(:ring_server_port => 12351,
70
+ :ring_server_host => '127.0.0.1')
71
+ grid.start(:quantity => 1,
72
+ :take_all => true, :browser_type => 'firefox')
73
+ grid.size.should == 0
74
+ end
75
+
76
+ it 'should fail to find any grid based on a unknown browser type' do
77
+ grid = Watir::Grid.new(:ring_server_port => 12351,
78
+ :ring_server_host => '127.0.0.1')
79
+ grid.start(:quantity => 1,
80
+ :take_all => true, :browser_type => 'penguin')
81
+ grid.size.should == 0
82
+ end
83
+
84
+ it 'should take any 1 browser based on specific architecture type' do
85
+ grid = Watir::Grid.new(:ring_server_port => 12351,
86
+ :ring_server_host => '127.0.0.1')
87
+ grid.start(:quantity => 1,
88
+ :take_all => true, :architecture => Config::CONFIG['arch'])
89
+ grid.size.should == 1
90
+ end
91
+
92
+ it 'should fail to find any grid based on unknown architecture type' do
93
+ grid = Watir::Grid.new(:ring_server_port => 12351,
94
+ :ring_server_host => '127.0.0.1')
95
+ grid.start(:quantity => 1,
96
+ :take_all => true, :architecture => 'geos-1992')
97
+ grid.size.should == 0
98
+ end
99
+
100
+ it 'should take any 1 browser based on specific hostname' do
101
+ hostname = `hostname`.strip
102
+ grid = Watir::Grid.new(:ring_server_port => 12351,
103
+ :ring_server_host => '127.0.0.1')
104
+ grid.start(:quantity => 1,
105
+ :take_all => true,
106
+ :hostnames => { hostname => "127.0.0.1"}
107
+ )
108
+ grid.size.should == 1
109
+ end
110
+
111
+ it 'should fail to find any grid based on unknown hostname' do
112
+ grid = Watir::Grid.new(:ring_server_port => 12351,
113
+ :ring_server_host => '127.0.0.1')
114
+ grid.start(:quantity => 1,
115
+ :take_all => true, :hostnames => {
116
+ "tokyo" => "127.0.0.1"})
117
+ grid.size.should == 0
118
+ end
119
+
120
+ it 'should take the last browser and execute some watir commands' do
121
+ grid = Watir::Grid.new(:ring_server_port => 12351,
122
+ :ring_server_host => '127.0.0.1')
123
+ grid.start(:quantity => 1, :take_all => true)
124
+ threads = []
125
+ grid.browsers.each do |browser|
126
+ threads << Thread.new do
127
+ browser[:hostname].should == `hostname`.strip
128
+ browser[:architecture].should == Config::CONFIG['arch']
129
+ browser[:browser_type].should == 'safari'
130
+ b = browser[:object].new_browser
131
+ b.goto("http://www.google.com")
132
+ b.text_field(:name, 'q').set("watirgrid")
133
+ b.button(:name, "btnI").click
134
+ end
135
+ end
136
+ threads.each {|thread| thread.join}
137
+ grid.size.should == 1
138
+ end
139
+
140
+ it 'should find no more tuples in the tuplespace' do
141
+ grid = Watir::Grid.new(:ring_server_port => 12351,
142
+ :ring_server_host => '127.0.0.1')
143
+ grid.start(:read_all => true)
144
+ grid.size.should == 0
145
+ end
146
+
147
+ end
148
+
data/spec/spec.opts CHANGED
@@ -1 +1 @@
1
- --color
1
+ --color
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,9 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__))
2
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
- require 'watirgrid'
4
- require 'spec'
5
- require 'spec/autorun'
6
-
7
- Spec::Runner.configure do |config|
8
-
9
- end
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'watirgrid'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end