Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/lib/selenium/webdriver/common/service.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 object,
24
# responsible for storing a service manager configuration.
25
#
26
27
class Service
28
class << self
29
attr_reader :driver_path
30
31
def chrome(**)
32
Chrome::Service.new(**)
33
end
34
35
def firefox(**)
36
Firefox::Service.new(**)
37
end
38
39
def ie(**)
40
IE::Service.new(**)
41
end
42
alias internet_explorer ie
43
44
def edge(**)
45
Edge::Service.new(**)
46
end
47
alias microsoftedge edge
48
alias msedge edge
49
50
def safari(**)
51
Safari::Service.new(**)
52
end
53
54
def driver_path=(path)
55
Platform.assert_executable path if path.is_a?(String)
56
@driver_path = path
57
end
58
end
59
60
attr_accessor :host, :executable_path, :port, :log, :args
61
alias extra_args args
62
63
#
64
# End users should use a class method for the desired driver, rather than using this directly.
65
#
66
# @api private
67
#
68
69
def initialize(path: nil, port: nil, log: nil, args: nil)
70
port ||= self.class::DEFAULT_PORT
71
args ||= []
72
path ||= env_path
73
74
@executable_path = path
75
@host = Platform.localhost
76
@port = Integer(port)
77
@log = case log
78
when :stdout
79
$stdout
80
when :stderr
81
$stderr
82
else
83
log
84
end
85
@args = args
86
87
raise Error::WebDriverError, "invalid port: #{@port}" if @port < 1
88
end
89
90
def launch
91
@executable_path ||= env_path || find_driver_path
92
ServiceManager.new(self).tap(&:start)
93
end
94
95
def shutdown_supported
96
self.class::SHUTDOWN_SUPPORTED
97
end
98
99
def find_driver_path
100
default_options = WebDriver.const_get("#{self.class.name&.split('::')&.[](2)}::Options").new
101
DriverFinder.new(default_options, self).driver_path
102
end
103
104
def env_path
105
ENV.fetch(self.class::DRIVER_PATH_ENV_KEY, nil)
106
end
107
end # Service
108
end # WebDriver
109
end # Selenium
110
111