Skip to content

Commit 03f3fb2

Browse files
committed
rb: Extract Chrome service into a parent class that can be shared
1 parent 051c8b1 commit 03f3fb2

File tree

3 files changed

+107
-62
lines changed

3 files changed

+107
-62
lines changed

rb/lib/selenium/webdriver/chrome/service.rb

Lines changed: 3 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,9 @@ module Chrome
2525
# @api private
2626
#
2727

28-
class Service
29-
START_TIMEOUT = 20
30-
SOCKET_LOCK_TIMEOUT = 45
31-
STOP_TIMEOUT = 5
32-
DEFAULT_PORT = 9515
33-
MISSING_TEXT = "Unable to find the chromedriver executable. Please download the server from http://chromedriver.storage.googleapis.com/index.html and place it somewhere on your PATH. More info at https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver."
28+
class Service < WebDriver::Service
29+
DEFAULT_PORT = 9515
30+
MISSING_TEXT = "Unable to find the chromedriver executable. Please download the server from http://chromedriver.storage.googleapis.com/index.html and place it somewhere on your PATH. More info at https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver."
3431

3532
def self.executable_path
3633
@executable_path ||= (
@@ -42,58 +39,12 @@ def self.executable_path
4239
)
4340
end
4441

45-
def self.executable_path=(path)
46-
Platform.assert_executable path
47-
@executable_path = path
48-
end
49-
5042
def self.default_service(*extra_args)
5143
new executable_path, DEFAULT_PORT, *extra_args
5244
end
5345

54-
def initialize(executable_path, port, *extra_args)
55-
@executable_path = executable_path
56-
@host = Platform.localhost
57-
@port = Integer(port)
58-
59-
raise Error::WebDriverError, "invalid port: #{@port}" if @port < 1
60-
61-
@extra_args = extra_args
62-
end
63-
64-
def start
65-
Platform.exit_hook { stop } # make sure we don't leave the server running
66-
67-
socket_lock.locked do
68-
find_free_port
69-
start_process
70-
connect_until_stable
71-
end
72-
end
73-
74-
def stop
75-
return if @process.nil? || @process.exited?
76-
77-
Net::HTTP.start(@host, @port) do |http|
78-
http.open_timeout = STOP_TIMEOUT / 2
79-
http.read_timeout = STOP_TIMEOUT / 2
80-
81-
http.get("/shutdown")
82-
end
83-
ensure
84-
stop_process
85-
end
86-
87-
def uri
88-
URI.parse "http://#{@host}:#{@port}"
89-
end
90-
9146
private
9247

93-
def find_free_port
94-
@port = PortProber.above @port
95-
end
96-
9748
def start_process
9849
server_command = [@executable_path, "--port=#{@port}", *@extra_args]
9950
@process = ChildProcess.build(*server_command)
@@ -102,12 +53,6 @@ def start_process
10253
@process.start
10354
end
10455

105-
def stop_process
106-
@process.poll_for_exit STOP_TIMEOUT
107-
rescue ChildProcess::TimeoutError
108-
@process.stop STOP_TIMEOUT
109-
end
110-
11156
def connect_until_stable
11257
socket_poller = SocketPoller.new @host, @port, START_TIMEOUT
11358

@@ -116,10 +61,6 @@ def connect_until_stable
11661
end
11762
end
11863

119-
def socket_lock
120-
@socket_lock ||= SocketLock.new(@port - 1, SOCKET_LOCK_TIMEOUT)
121-
end
122-
12364
end # Service
12465
end # Chrome
12566
end # WebDriver

rb/lib/selenium/webdriver/common.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
require 'selenium/webdriver/common/proxy'
2424
require 'selenium/webdriver/common/log_entry'
2525
require 'selenium/webdriver/common/file_reaper'
26+
require 'selenium/webdriver/common/service'
2627
require 'selenium/webdriver/common/socket_lock'
2728
require 'selenium/webdriver/common/socket_poller'
2829
require 'selenium/webdriver/common/port_prober'
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# encoding: utf-8
2+
#
3+
# Licensed to the Software Freedom Conservancy (SFC) under one
4+
# or more contributor license agreements. See the NOTICE file
5+
# distributed with this work for additional information
6+
# regarding copyright ownership. The SFC licenses this file
7+
# to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance
9+
# with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing,
14+
# software distributed under the License is distributed on an
15+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
# KIND, either express or implied. See the License for the
17+
# specific language governing permissions and limitations
18+
# under the License.
19+
20+
module Selenium
21+
module WebDriver
22+
23+
#
24+
# @api private
25+
#
26+
27+
class Service
28+
START_TIMEOUT = 20
29+
SOCKET_LOCK_TIMEOUT = 45
30+
STOP_TIMEOUT = 5
31+
32+
def self.executable_path=(path)
33+
Platform.assert_executable path
34+
@executable_path = path
35+
end
36+
37+
def initialize(executable_path, port, *extra_args)
38+
@executable_path = executable_path
39+
@host = Platform.localhost
40+
@port = Integer(port)
41+
@extra_args = extra_args
42+
43+
raise Error::WebDriverError, "invalid port: #{@port}" if @port < 1
44+
end
45+
46+
def start
47+
Platform.exit_hook { stop } # make sure we don't leave the server running
48+
49+
socket_lock.locked do
50+
find_free_port
51+
start_process
52+
connect_until_stable
53+
end
54+
end
55+
56+
def stop
57+
return if @process.nil? || @process.exited?
58+
59+
stop_server
60+
ensure
61+
stop_process
62+
end
63+
64+
def uri
65+
@uri ||= URI.parse("http://#{@host}:#{@port}")
66+
end
67+
68+
private
69+
70+
def find_free_port
71+
@port = PortProber.above(@port)
72+
end
73+
74+
def start_process
75+
raise NotImplementedError, "subclass responsibility"
76+
end
77+
78+
def stop_server
79+
Net::HTTP.start(@host, @port) do |http|
80+
http.open_timeout = STOP_TIMEOUT / 2
81+
http.read_timeout = STOP_TIMEOUT / 2
82+
83+
http.get("/shutdown")
84+
end
85+
end
86+
87+
def stop_process
88+
@process.poll_for_exit STOP_TIMEOUT
89+
rescue ChildProcess::TimeoutError
90+
@process.stop STOP_TIMEOUT
91+
end
92+
93+
def connect_until_stable
94+
raise NotImplementedError, "subclass responsibility"
95+
end
96+
97+
def socket_lock
98+
@socket_lock ||= SocketLock.new(@port - 1, SOCKET_LOCK_TIMEOUT)
99+
end
100+
101+
end # Chrome
102+
end # WebDriver
103+
end # Service

0 commit comments

Comments
 (0)