Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/common/service_manager.rb
1865 views
1
# frozen_string_literal: true
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
# Base class implementing default behavior of service_manager object,
24
# responsible for starting and stopping driver implementations.
25
#
26
# @api private
27
#
28
class ServiceManager
29
START_TIMEOUT = 20
30
SOCKET_LOCK_TIMEOUT = 45
31
STOP_TIMEOUT = 20
32
33
#
34
# End users should use a class method for the desired driver, rather than using this directly.
35
#
36
# @api private
37
#
38
39
def initialize(config)
40
@executable_path = config.executable_path
41
@host = Platform.localhost
42
@port = config.port
43
@io = config.log
44
@extra_args = config.args
45
@shutdown_supported = config.shutdown_supported
46
47
raise Error::WebDriverError, "invalid port: #{@port}" if @port < 1
48
end
49
50
def start
51
raise "already started: #{uri.inspect} #{@executable_path.inspect}" if process_running?
52
53
Platform.exit_hook { stop } # make sure we don't leave the server running
54
55
socket_lock.locked do
56
find_free_port
57
start_process
58
connect_until_stable
59
end
60
end
61
62
def stop
63
return unless @shutdown_supported
64
return if process_exited?
65
66
stop_server
67
@process.poll_for_exit STOP_TIMEOUT
68
rescue ChildProcess::TimeoutError, Errno::ECONNREFUSED
69
nil # noop
70
ensure
71
stop_process
72
end
73
74
def uri
75
@uri ||= URI.parse("http://#{@host}:#{@port}")
76
end
77
78
private
79
80
def build_process(*command)
81
WebDriver.logger.debug("Executing Process #{command}", id: :driver_service)
82
@process = ChildProcess.build(*command)
83
@io ||= WebDriver.logger.io if WebDriver.logger.debug?
84
@process.io = @io if @io
85
86
@process
87
end
88
89
def connect_to_server
90
Net::HTTP.start(@host, @port) do |http|
91
http.open_timeout = STOP_TIMEOUT / 2
92
http.read_timeout = STOP_TIMEOUT / 2
93
94
yield http
95
end
96
end
97
98
def find_free_port
99
@port = PortProber.above(@port)
100
end
101
102
def start_process
103
@process = build_process(@executable_path, "--port=#{@port}", *@extra_args)
104
@process.start
105
end
106
107
def stop_process
108
return if process_exited?
109
110
@process.stop STOP_TIMEOUT
111
end
112
113
def stop_server
114
connect_to_server do |http|
115
headers = WebDriver::Remote::Http::Common::DEFAULT_HEADERS.dup
116
WebDriver.logger.debug('Sending shutdown request to server', id: :driver_service)
117
http.get('/shutdown', headers)
118
end
119
end
120
121
def process_running?
122
defined?(@process) && @process&.alive?
123
end
124
125
def process_exited?
126
@process.nil? || @process.exited?
127
end
128
129
def connect_until_stable
130
socket_poller = SocketPoller.new @host, @port, START_TIMEOUT
131
return if socket_poller.connected?
132
133
raise Error::WebDriverError, cannot_connect_error_text
134
end
135
136
def cannot_connect_error_text
137
"unable to connect to #{@executable_path} #{@host}:#{@port}"
138
end
139
140
def socket_lock
141
@socket_lock ||= SocketLock.new(@port - 1, SOCKET_LOCK_TIMEOUT)
142
end
143
end # Service
144
end # WebDriver
145
end # Selenium
146
147