bbc-selenium-webdriver 1.17.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (168) hide show
  1. data/CHANGES +473 -0
  2. data/README +31 -0
  3. data/lib/selenium-client.rb +2 -0
  4. data/lib/selenium-webdriver.rb +1 -0
  5. data/lib/selenium/client.rb +37 -0
  6. data/lib/selenium/client/base.rb +118 -0
  7. data/lib/selenium/client/driver.rb +10 -0
  8. data/lib/selenium/client/errors.rb +9 -0
  9. data/lib/selenium/client/extensions.rb +118 -0
  10. data/lib/selenium/client/idiomatic.rb +488 -0
  11. data/lib/selenium/client/javascript_expression_builder.rb +116 -0
  12. data/lib/selenium/client/javascript_frameworks/jquery.rb +13 -0
  13. data/lib/selenium/client/javascript_frameworks/prototype.rb +13 -0
  14. data/lib/selenium/client/legacy_driver.rb +1720 -0
  15. data/lib/selenium/client/protocol.rb +104 -0
  16. data/lib/selenium/client/selenium_helper.rb +34 -0
  17. data/lib/selenium/rake/server_task.rb +157 -0
  18. data/lib/selenium/server.rb +223 -0
  19. data/lib/selenium/webdriver.rb +64 -0
  20. data/lib/selenium/webdriver/android.rb +9 -0
  21. data/lib/selenium/webdriver/android/bridge.rb +51 -0
  22. data/lib/selenium/webdriver/chrome.rb +26 -0
  23. data/lib/selenium/webdriver/chrome/bridge.rb +106 -0
  24. data/lib/selenium/webdriver/chrome/profile.rb +83 -0
  25. data/lib/selenium/webdriver/chrome/service.rb +74 -0
  26. data/lib/selenium/webdriver/common.rb +30 -0
  27. data/lib/selenium/webdriver/common/action_builder.rb +352 -0
  28. data/lib/selenium/webdriver/common/alert.rb +26 -0
  29. data/lib/selenium/webdriver/common/bridge_helper.rb +67 -0
  30. data/lib/selenium/webdriver/common/core_ext/base64.rb +9 -0
  31. data/lib/selenium/webdriver/common/core_ext/dir.rb +42 -0
  32. data/lib/selenium/webdriver/common/core_ext/string.rb +5 -0
  33. data/lib/selenium/webdriver/common/driver.rb +301 -0
  34. data/lib/selenium/webdriver/common/driver_extensions/has_input_devices.rb +39 -0
  35. data/lib/selenium/webdriver/common/driver_extensions/rotatable.rb +44 -0
  36. data/lib/selenium/webdriver/common/driver_extensions/takes_screenshot.rb +43 -0
  37. data/lib/selenium/webdriver/common/driver_extensions/uploads_files.rb +46 -0
  38. data/lib/selenium/webdriver/common/element.rb +262 -0
  39. data/lib/selenium/webdriver/common/error.rb +212 -0
  40. data/lib/selenium/webdriver/common/file_reaper.rb +47 -0
  41. data/lib/selenium/webdriver/common/keyboard.rb +52 -0
  42. data/lib/selenium/webdriver/common/keys.rb +109 -0
  43. data/lib/selenium/webdriver/common/mouse.rb +65 -0
  44. data/lib/selenium/webdriver/common/navigation.rb +43 -0
  45. data/lib/selenium/webdriver/common/options.rb +128 -0
  46. data/lib/selenium/webdriver/common/platform.rb +166 -0
  47. data/lib/selenium/webdriver/common/port_prober.rb +25 -0
  48. data/lib/selenium/webdriver/common/profile_helper.rb +67 -0
  49. data/lib/selenium/webdriver/common/proxy.rb +120 -0
  50. data/lib/selenium/webdriver/common/search_context.rb +92 -0
  51. data/lib/selenium/webdriver/common/socket_poller.rb +100 -0
  52. data/lib/selenium/webdriver/common/target_locator.rb +81 -0
  53. data/lib/selenium/webdriver/common/timeouts.rb +29 -0
  54. data/lib/selenium/webdriver/common/wait.rb +60 -0
  55. data/lib/selenium/webdriver/common/window.rb +96 -0
  56. data/lib/selenium/webdriver/common/zipper.rb +82 -0
  57. data/lib/selenium/webdriver/firefox.rb +37 -0
  58. data/lib/selenium/webdriver/firefox/binary.rb +161 -0
  59. data/lib/selenium/webdriver/firefox/bridge.rb +58 -0
  60. data/lib/selenium/webdriver/firefox/extension.rb +52 -0
  61. data/lib/selenium/webdriver/firefox/extension/webdriver.xpi +0 -0
  62. data/lib/selenium/webdriver/firefox/launcher.rb +104 -0
  63. data/lib/selenium/webdriver/firefox/native/linux/amd64/x_ignore_nofocus.so +0 -0
  64. data/lib/selenium/webdriver/firefox/native/linux/x86/x_ignore_nofocus.so +0 -0
  65. data/lib/selenium/webdriver/firefox/profile.rb +315 -0
  66. data/lib/selenium/webdriver/firefox/profiles_ini.rb +62 -0
  67. data/lib/selenium/webdriver/firefox/socket_lock.rb +61 -0
  68. data/lib/selenium/webdriver/firefox/util.rb +29 -0
  69. data/lib/selenium/webdriver/ie.rb +19 -0
  70. data/lib/selenium/webdriver/ie/bridge.rb +60 -0
  71. data/lib/selenium/webdriver/ie/native/win32/IEDriver.dll +0 -0
  72. data/lib/selenium/webdriver/ie/native/x64/IEDriver.dll +0 -0
  73. data/lib/selenium/webdriver/ie/server.rb +64 -0
  74. data/lib/selenium/webdriver/iphone.rb +9 -0
  75. data/lib/selenium/webdriver/iphone/bridge.rb +50 -0
  76. data/lib/selenium/webdriver/opera.rb +24 -0
  77. data/lib/selenium/webdriver/opera/bridge.rb +113 -0
  78. data/lib/selenium/webdriver/opera/service.rb +49 -0
  79. data/lib/selenium/webdriver/remote.rb +21 -0
  80. data/lib/selenium/webdriver/remote/bridge.rb +473 -0
  81. data/lib/selenium/webdriver/remote/capabilities.rb +241 -0
  82. data/lib/selenium/webdriver/remote/commands.rb +181 -0
  83. data/lib/selenium/webdriver/remote/http/common.rb +74 -0
  84. data/lib/selenium/webdriver/remote/http/curb.rb +79 -0
  85. data/lib/selenium/webdriver/remote/http/default.rb +102 -0
  86. data/lib/selenium/webdriver/remote/http/persistent.rb +38 -0
  87. data/lib/selenium/webdriver/remote/response.rb +99 -0
  88. data/lib/selenium/webdriver/remote/server_error.rb +17 -0
  89. data/lib/selenium/webdriver/support.rb +4 -0
  90. data/lib/selenium/webdriver/support/abstract_event_listener.rb +28 -0
  91. data/lib/selenium/webdriver/support/block_event_listener.rb +17 -0
  92. data/lib/selenium/webdriver/support/event_firing_bridge.rb +112 -0
  93. data/lib/selenium/webdriver/support/select.rb +293 -0
  94. data/spec/integration/selenium/client/api/backward_compatible_api_spec.rb +22 -0
  95. data/spec/integration/selenium/client/api/browser_xpath_library_spec.rb +17 -0
  96. data/spec/integration/selenium/client/api/click_spec.rb +39 -0
  97. data/spec/integration/selenium/client/api/cookie_spec.rb +39 -0
  98. data/spec/integration/selenium/client/api/element_spec.rb +14 -0
  99. data/spec/integration/selenium/client/api/highlight_located_element_spec.rb +19 -0
  100. data/spec/integration/selenium/client/api/retrieve_last_remote_control_logs_spec.rb +33 -0
  101. data/spec/integration/selenium/client/api/screenshot_spec.rb +29 -0
  102. data/spec/integration/selenium/client/api/select_window_spec.rb +46 -0
  103. data/spec/integration/selenium/client/api/start_stop_spec.rb +9 -0
  104. data/spec/integration/selenium/client/api/wait_for_ajax_spec.rb +27 -0
  105. data/spec/integration/selenium/client/api/wait_for_element_spec.rb +56 -0
  106. data/spec/integration/selenium/client/api/wait_for_field_value_spec.rb +52 -0
  107. data/spec/integration/selenium/client/api/wait_for_text_spec.rb +98 -0
  108. data/spec/integration/selenium/client/sample-app/public/jquery-1.3.2.js +4376 -0
  109. data/spec/integration/selenium/client/sample-app/public/jquery.html +55 -0
  110. data/spec/integration/selenium/client/sample-app/public/prototype-1.6.0.3.js +4320 -0
  111. data/spec/integration/selenium/client/sample-app/public/prototype.html +59 -0
  112. data/spec/integration/selenium/client/sample-app/sample_app.rb +32 -0
  113. data/spec/integration/selenium/client/spec_helper.rb +125 -0
  114. data/spec/integration/selenium/webdriver/chrome/driver_spec.rb +28 -0
  115. data/spec/integration/selenium/webdriver/chrome/profile_spec.rb +42 -0
  116. data/spec/integration/selenium/webdriver/driver_spec.rb +253 -0
  117. data/spec/integration/selenium/webdriver/element_spec.rb +185 -0
  118. data/spec/integration/selenium/webdriver/error_spec.rb +30 -0
  119. data/spec/integration/selenium/webdriver/firefox/driver_spec.rb +21 -0
  120. data/spec/integration/selenium/webdriver/firefox/profile_spec.rb +141 -0
  121. data/spec/integration/selenium/webdriver/keyboard_spec.rb +57 -0
  122. data/spec/integration/selenium/webdriver/mouse_spec.rb +55 -0
  123. data/spec/integration/selenium/webdriver/navigation_spec.rb +44 -0
  124. data/spec/integration/selenium/webdriver/opera/driver_spec.rb +47 -0
  125. data/spec/integration/selenium/webdriver/options_spec.rb +49 -0
  126. data/spec/integration/selenium/webdriver/remote/element_spec.rb +24 -0
  127. data/spec/integration/selenium/webdriver/spec_helper.rb +44 -0
  128. data/spec/integration/selenium/webdriver/spec_support.rb +13 -0
  129. data/spec/integration/selenium/webdriver/spec_support/guards.rb +86 -0
  130. data/spec/integration/selenium/webdriver/spec_support/helpers.rb +46 -0
  131. data/spec/integration/selenium/webdriver/spec_support/jruby_test_environment.rb +29 -0
  132. data/spec/integration/selenium/webdriver/spec_support/rack_server.rb +123 -0
  133. data/spec/integration/selenium/webdriver/spec_support/test_environment.rb +199 -0
  134. data/spec/integration/selenium/webdriver/target_locator_spec.rb +170 -0
  135. data/spec/integration/selenium/webdriver/timeout_spec.rb +56 -0
  136. data/spec/integration/selenium/webdriver/window_spec.rb +56 -0
  137. data/spec/integration/selenium/webdriver/zipper_spec.rb +66 -0
  138. data/spec/unit/selenium/client/base_spec.rb +239 -0
  139. data/spec/unit/selenium/client/extensions_spec.rb +174 -0
  140. data/spec/unit/selenium/client/idiomatic_spec.rb +500 -0
  141. data/spec/unit/selenium/client/javascript_expression_builder_spec.rb +79 -0
  142. data/spec/unit/selenium/client/javascript_frameworks/jquery_spec.rb +10 -0
  143. data/spec/unit/selenium/client/javascript_frameworks/prototype_spec.rb +10 -0
  144. data/spec/unit/selenium/client/protocol_spec.rb +124 -0
  145. data/spec/unit/selenium/client/selenium_helper_spec.rb +56 -0
  146. data/spec/unit/selenium/client/spec_helper.rb +24 -0
  147. data/spec/unit/selenium/rake/task_spec.rb +79 -0
  148. data/spec/unit/selenium/server_spec.rb +131 -0
  149. data/spec/unit/selenium/webdriver/action_builder_spec.rb +90 -0
  150. data/spec/unit/selenium/webdriver/android/bridge_spec.rb +31 -0
  151. data/spec/unit/selenium/webdriver/chrome/bridge_spec.rb +94 -0
  152. data/spec/unit/selenium/webdriver/chrome/profile_spec.rb +57 -0
  153. data/spec/unit/selenium/webdriver/chrome/service_spec.rb +44 -0
  154. data/spec/unit/selenium/webdriver/error_spec.rb +41 -0
  155. data/spec/unit/selenium/webdriver/firefox/bridge_spec.rb +17 -0
  156. data/spec/unit/selenium/webdriver/iphone/bridge_spec.rb +30 -0
  157. data/spec/unit/selenium/webdriver/proxy_spec.rb +87 -0
  158. data/spec/unit/selenium/webdriver/remote/bridge_spec.rb +16 -0
  159. data/spec/unit/selenium/webdriver/remote/capabilities_spec.rb +111 -0
  160. data/spec/unit/selenium/webdriver/remote/http/common_spec.rb +24 -0
  161. data/spec/unit/selenium/webdriver/remote/http/default_spec.rb +46 -0
  162. data/spec/unit/selenium/webdriver/search_context_spec.rb +61 -0
  163. data/spec/unit/selenium/webdriver/socket_poller_spec.rb +68 -0
  164. data/spec/unit/selenium/webdriver/spec_helper.rb +6 -0
  165. data/spec/unit/selenium/webdriver/support/event_firing_spec.rb +111 -0
  166. data/spec/unit/selenium/webdriver/support/select_spec.rb +290 -0
  167. data/spec/unit/selenium/webdriver/wait_spec.rb +40 -0
  168. metadata +215 -0
@@ -0,0 +1,104 @@
1
+ module Selenium
2
+ module Client
3
+
4
+ HTTP_HEADERS = { 'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8' }
5
+
6
+ # Module in charge of handling Selenium over-the-wire HTTP protocol
7
+ module Protocol
8
+ attr_reader :session_id
9
+
10
+ def remote_control_command(verb, args=[])
11
+ timeout(@default_timeout_in_seconds) do
12
+ status, response = http_post(http_request_for(verb, args))
13
+ raise CommandError, response unless status == "OK"
14
+ response[3..-1] # strip "OK," from response
15
+ end
16
+ end
17
+
18
+ def string_command(verb, args=[])
19
+ remote_control_command(verb, args)
20
+ end
21
+
22
+ def string_array_command(verb, args=[])
23
+ csv = string_command(verb, args)
24
+ token = ""
25
+ tokens = []
26
+ escape = false
27
+ csv.split(//).each do |letter|
28
+ if escape
29
+ token += letter
30
+ escape = false
31
+ next
32
+ end
33
+ case letter
34
+ when '\\'
35
+ escape = true
36
+ when ','
37
+ tokens << token
38
+ token = ""
39
+ else
40
+ token += letter
41
+ end
42
+ end
43
+ tokens << token
44
+ return tokens
45
+ end
46
+
47
+ def number_command(verb, args)
48
+ string_command verb, args
49
+ end
50
+
51
+ def number_array_command(verb, args)
52
+ string_array_command verb, args
53
+ end
54
+
55
+ def boolean_command(verb, args=[])
56
+ parse_boolean_value string_command(verb, args)
57
+ end
58
+
59
+ def boolean_array_command(verb, args)
60
+ string_array_command(verb, args).collect {|value| parse_boolean_value(value)}
61
+ end
62
+
63
+ protected
64
+
65
+ def parse_boolean_value(value)
66
+ if ("true" == value)
67
+ return true
68
+ elsif ("false" == value)
69
+ return false
70
+ end
71
+ raise ProtocolError, "Invalid Selenese boolean value that is neither 'true' nor 'false': got '#{value}'"
72
+ end
73
+
74
+ def http_request_for(verb, args)
75
+ data = "cmd=#{CGI::escape(verb)}"
76
+ args.each_with_index do |arg, index|
77
+ data << "&#{index.succ}=#{CGI::escape(arg.to_s)}"
78
+ end
79
+ data << "&sessionId=#{session_id}" unless session_id.nil?
80
+ data
81
+ end
82
+
83
+ def http_post(data)
84
+ start = Time.now
85
+ called_from = caller.detect{|line| line !~ /(selenium-client|vendor|usr\/lib\/ruby|\(eval\))/i}
86
+ http = Net::HTTP.new(@host, @port)
87
+ http.open_timeout = default_timeout_in_seconds
88
+ http.read_timeout = default_timeout_in_seconds
89
+ response = http.post('/selenium-server/driver/', data, HTTP_HEADERS)
90
+ if response.body !~ /^OK/
91
+ puts "#{start} selenium-client received failure from selenium server:"
92
+ puts "requested:"
93
+ puts "\t" + CGI::unescape(data.split('&').join("\n\t"))
94
+ puts "received:"
95
+ puts "\t#{response.body.inspect}"
96
+ puts "\tcalled from #{called_from}"
97
+ end
98
+ [ response.body[0..1], response.body ]
99
+ end
100
+
101
+ end
102
+
103
+ end
104
+ end
@@ -0,0 +1,34 @@
1
+ # Defines a mixin module that you can use to write Selenium tests
2
+ # without typing "@selenium." in front of every command. Every
3
+ # call to a missing method will be automatically sent to the @selenium
4
+ # object.
5
+ module Selenium
6
+ module Client
7
+
8
+ module SeleniumHelper
9
+
10
+ # Overrides default open method to actually delegates to @selenium
11
+ def open(url)
12
+ @selenium.open url
13
+ end
14
+
15
+ # Overrides default type method to actually delegates to @selenium
16
+ def type(locator, value)
17
+ @selenium.type locator, value
18
+ end
19
+
20
+ # Overrides default select method to actually delegates to @selenium
21
+ def select(input_locator, option_locator)
22
+ @selenium.select input_locator, option_locator
23
+ end
24
+
25
+ # Delegates to @selenium on method missing
26
+ def method_missing(method_name, *args)
27
+ return super unless @selenium.respond_to?(method_name)
28
+
29
+ @selenium.send(method_name, *args)
30
+ end
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,157 @@
1
+ require 'selenium/server'
2
+ require 'rake'
3
+
4
+ module Selenium
5
+ module Rake
6
+
7
+ class MissingJarFileError < StandardError
8
+ end
9
+
10
+ #
11
+ # Defines rake tasks for starting, stopping and restarting the Selenium server.
12
+ #
13
+ # Usage:
14
+ #
15
+ # require 'selenium/rake/server_task'
16
+ #
17
+ # Selenium::Rake::ServerTask.new do |t|
18
+ # t.jar = "/path/to/selenium-server-standalone.jar"
19
+ # t.port = 4444
20
+ # t.opts = %w[-some options]
21
+ # end
22
+ #
23
+ # Alternatively, you can have the task download a specific version of the server:
24
+ #
25
+ # Selenium::Rake::ServerTask.new(:server) do |t|
26
+ # t.version = '2.6.0'
27
+ # end
28
+ #
29
+ # or the latest version
30
+ #
31
+ # Selenium::Rake::ServerTask.new(:server) do |t|
32
+ # t.version = :latest
33
+ # end
34
+ #
35
+ #
36
+ # Tasks defined:
37
+ #
38
+ # rake selenium:server:start
39
+ # rake selenium:server:stop
40
+ # rake selenium:server:restart
41
+ #
42
+
43
+ class ServerTask
44
+ include ::Rake::DSL if defined?(::Rake::DSL)
45
+
46
+ #
47
+ # Path to the selenium server jar
48
+ #
49
+
50
+ attr_accessor :jar
51
+
52
+ #
53
+ # Port to use for the server.
54
+ # Default: 4444
55
+ #
56
+ #
57
+
58
+ attr_accessor :port
59
+
60
+ #
61
+ # Timeout in seconds for the server to start/stop.
62
+ # Default: 30
63
+ #
64
+
65
+ attr_accessor :timeout
66
+
67
+ #
68
+ # Whether we should detach from the server process.
69
+ # Default: true
70
+ #
71
+
72
+ attr_accessor :background
73
+ alias_method :background?, :background
74
+
75
+ #
76
+ # Configure logging. Pass a log file path or a boolean.
77
+ # Default: true
78
+ #
79
+ # true - log to stdout/stderr
80
+ # false - no logging
81
+ # String - log to the specified file
82
+ #
83
+
84
+ attr_accessor :log
85
+
86
+ #
87
+ # Add additional options passed to the server jar.
88
+ #
89
+
90
+ attr_accessor :opts
91
+
92
+ #
93
+ # Specify the version of the server jar to download
94
+ #
95
+
96
+ attr_accessor :version
97
+
98
+
99
+ def initialize(prefix = "selenium:server")
100
+ @jar = nil
101
+ @prefix = prefix
102
+ @port = 4444
103
+ @timeout = 30
104
+ @background = true
105
+ @log = true
106
+ @opts = []
107
+ @version = nil
108
+
109
+ yield self if block_given?
110
+
111
+ if @version
112
+ @jar = Selenium::Server.download(@version)
113
+ end
114
+
115
+ unless @jar
116
+ raise MissingJarFileError, "must provide path to the selenium server jar"
117
+ end
118
+
119
+ @server = Selenium::Server.new(@jar, :port => @port,
120
+ :timeout => @timeout,
121
+ :background => @background,
122
+ :log => @log )
123
+
124
+ @server << @opts
125
+
126
+ define_start_task
127
+ define_stop_task
128
+ define_restart_task
129
+ end
130
+
131
+ private
132
+
133
+ def define_start_task
134
+ desc "Start the Selenium server"
135
+ task "#{@prefix}:start" do
136
+ @server.start
137
+ end
138
+ end
139
+
140
+ def define_stop_task
141
+ desc 'Stop the Selenium server'
142
+ task "#{@prefix}:stop" do
143
+ @server.stop
144
+ end
145
+ end
146
+
147
+ def define_restart_task
148
+ desc 'Restart the Selenium server'
149
+ task "#{@prefix}:restart" do
150
+ @server.stop
151
+ @server.start
152
+ end
153
+ end
154
+
155
+ end # ServerTask
156
+ end # Rake
157
+ end # Selenium
@@ -0,0 +1,223 @@
1
+ require 'childprocess'
2
+ require 'selenium/webdriver/common/socket_poller'
3
+ require 'net/http'
4
+
5
+ module Selenium
6
+
7
+ #
8
+ # Wraps the remote server jar
9
+ #
10
+ # Usage:
11
+ #
12
+ # server = Selenium::Server.new('/path/to/selenium-server-standalone.jar')
13
+ # server.start
14
+ #
15
+ # Automatically download the given version:
16
+ #
17
+ # server = Selenium::Server.get '2.6.0'
18
+ # server.start
19
+ #
20
+ # or the latest version:
21
+ #
22
+ # server = Selenium::Server.get :latest
23
+ # server.start
24
+ #
25
+ # Run the server in the background:
26
+ #
27
+ # server = Selenium::Server.new(jar, :background => true)
28
+ # server.start
29
+ #
30
+ # Add additional arguments:
31
+ #
32
+ # server = Selenium::Server.new(jar)
33
+ # server << ["--additional", "args"]
34
+ # server.start
35
+ #
36
+
37
+ class Server
38
+ class Error < StandardError; end
39
+
40
+ CL_RESET = WebDriver::Platform.windows? ? '' : "\r\e[0K"
41
+
42
+ def self.get(required_version, opts = {})
43
+ new(download(required_version), opts)
44
+ end
45
+
46
+ #
47
+ # Download the given version of the selenium-server-standalone jar.
48
+ #
49
+
50
+ def self.download(required_version)
51
+ required_version = latest if required_version == :latest
52
+ download_file_name = "selenium-server-standalone-#{required_version}.jar"
53
+
54
+ if File.exists? download_file_name
55
+ return download_file_name
56
+ end
57
+
58
+ begin
59
+ open(download_file_name, "wb") do |destination|
60
+ net_http.start("selenium.googlecode.com") do |http|
61
+ resp = http.request_get("/files/#{download_file_name}") do |response|
62
+ total = response.content_length
63
+ progress = 0
64
+ segment_count = 0
65
+
66
+ response.read_body do |segment|
67
+ progress += segment.length
68
+ segment_count += 1
69
+
70
+ if segment_count % 15 == 0
71
+ percent = (progress.to_f / total.to_f) * 100
72
+ print "#{CL_RESET}Downloading #{download_file_name}: #{percent.to_i}% (#{progress} / #{total})"
73
+ segment_count = 0
74
+ end
75
+
76
+ destination.write(segment)
77
+ end
78
+ end
79
+
80
+ unless resp.kind_of? Net::HTTPSuccess
81
+ raise Error, "#{resp.code} for #{download_file_name}"
82
+ end
83
+ end
84
+ end
85
+ rescue
86
+ FileUtils.rm download_file_name if File.exists? download_file_name
87
+ raise
88
+ end
89
+
90
+ download_file_name
91
+ end
92
+
93
+ #
94
+ # Ask Google Code what the latest selenium-server-standalone version is.
95
+ #
96
+
97
+ def self.latest
98
+ net_http.start("code.google.com") do |http|
99
+ resp = http.get("/p/selenium/downloads/list")
100
+ resp.body.to_s[/selenium-server-standalone-(\d+.\d+.\d+).jar/, 1]
101
+ end
102
+ end
103
+
104
+ #
105
+ # @param [String] jar Path to the server jar.
106
+ # @param [Hash] opts the options to create the server process with
107
+ #
108
+ # @option opts [Integer] :port Port the server should listen on (default: 4444).
109
+ # @option opts [Integer] :timeout Seconds to wait for server launch/shutdown (default: 30)
110
+ # @option opts [true,false] :background Run the server in the background (default: false)
111
+ # @option opts [true,false,String] :log Either a path to a log file,
112
+ # or true to pass server log to stdout.
113
+ # @raise [Errno::ENOENT] if the jar file does not exist
114
+ #
115
+
116
+ def initialize(jar, opts = {})
117
+ raise Errno::ENOENT, jar unless File.exist?(jar)
118
+
119
+ @jar = jar
120
+ @host = "127.0.0.1"
121
+ @port = opts.fetch(:port, 4444)
122
+ @timeout = opts.fetch(:timeout, 30)
123
+ @background = opts.fetch(:background, false)
124
+ @log = opts[:log]
125
+
126
+ @additional_args = []
127
+ end
128
+
129
+ def start
130
+ process.start
131
+ poll_for_service
132
+
133
+ process.wait unless @background
134
+ end
135
+
136
+ def stop
137
+ begin
138
+ Net::HTTP.get(@host, "/selenium-server/driver/?cmd=shutDownSeleniumServer", @port)
139
+ rescue Errno::ECONNREFUSED
140
+ end
141
+
142
+ stop_process if @process
143
+ poll_for_shutdown
144
+
145
+ @log_file.close if @log_file
146
+ end
147
+
148
+ def webdriver_url
149
+ "http://#{@host}:#{@port}/wd/hub"
150
+ end
151
+
152
+ def <<(arg)
153
+ if arg.kind_of?(Array)
154
+ @additional_args += arg
155
+ else
156
+ @additional_args << arg.to_s
157
+ end
158
+ end
159
+
160
+ private
161
+
162
+ def self.net_http
163
+ if ENV['http_proxy']
164
+ http_proxy = ENV['http_proxy']
165
+ http_proxy = "http://#{http_proxy}" unless http_proxy =~ /^http:\/\//i
166
+ uri = URI.parse(http_proxy)
167
+
168
+ Net::HTTP::Proxy(uri.host, uri.port)
169
+ else
170
+ Net::HTTP
171
+ end
172
+ end
173
+
174
+ def stop_process
175
+ return unless @process.alive?
176
+
177
+ begin
178
+ @process.poll_for_exit(5)
179
+ rescue ChildProcess::TimeoutError
180
+ @process.stop
181
+ end
182
+ rescue Errno::ECHILD
183
+ # already dead
184
+ ensure
185
+ @process = nil
186
+ end
187
+
188
+ def process
189
+ @process ||= (
190
+ cp = ChildProcess.build("java", "-jar", @jar, "-port", @port.to_s, *@additional_args)
191
+ io = cp.io
192
+
193
+ if @log.kind_of?(String) && !@background
194
+ @log_file = File.open(@log, "w")
195
+ io.stdout = io.stderr = @log_file
196
+ elsif @log
197
+ io.inherit!
198
+ end
199
+
200
+ cp.detach = @background
201
+
202
+ cp
203
+ )
204
+ end
205
+
206
+ def poll_for_service
207
+ unless socket.connected?
208
+ raise Error, "remote server not launched in #{@timeout} seconds"
209
+ end
210
+ end
211
+
212
+ def poll_for_shutdown
213
+ unless socket.closed?
214
+ raise Error, "remote server not stopped in #{@timeout} seconds"
215
+ end
216
+ end
217
+
218
+ def socket
219
+ @socket ||= WebDriver::SocketPoller.new(@host, @port, @timeout)
220
+ end
221
+
222
+ end # Server
223
+ end # Selenium