Class: Selenium::WebDriver::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/selenium/webdriver/common/service.rb

Overview

Base class implementing default behavior of service object, responsible for starting and stopping driver implementations.

Constant Summary collapse

START_TIMEOUT =
20
SOCKET_LOCK_TIMEOUT =
45
STOP_TIMEOUT =
20

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil, port: nil, args: nil) ⇒ Service

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

End users should use a class method for the desired driver, rather than using this directly.



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/selenium/webdriver/common/service.rb', line 89

def initialize(path: nil, port: nil, args: nil)
  path ||= self.class.driver_path
  port ||= self.class.default_port
  args ||= []

  @executable_path = binary_path(path)
  @host = Platform.localhost
  @port = Integer(port)

  @extra_args = args.is_a?(Hash) ? extract_service_args(args) : args

  raise Error::WebDriverError, "invalid port: #{@port}" if @port < 1
end

Class Attribute Details

.default_portObject (readonly)

Returns the value of attribute default_port.



38
39
40
# File 'lib/selenium/webdriver/common/service.rb', line 38

def default_port
  @default_port
end

.driver_pathObject

Returns the value of attribute driver_path.



38
39
40
# File 'lib/selenium/webdriver/common/service.rb', line 38

def driver_path
  @driver_path
end

.executableObject (readonly)

Returns the value of attribute executable.



38
39
40
# File 'lib/selenium/webdriver/common/service.rb', line 38

def executable
  @executable
end

.missing_textObject (readonly)

Returns the value of attribute missing_text.



38
39
40
# File 'lib/selenium/webdriver/common/service.rb', line 38

def missing_text
  @missing_text
end

.shutdown_supportedObject (readonly)

Returns the value of attribute shutdown_supported.



38
39
40
# File 'lib/selenium/webdriver/common/service.rb', line 38

def shutdown_supported
  @shutdown_supported
end

Instance Attribute Details

#executable_pathObject (readonly)

Returns the value of attribute executable_path.



81
82
83
# File 'lib/selenium/webdriver/common/service.rb', line 81

def executable_path
  @executable_path
end

#hostObject

Returns the value of attribute host.



80
81
82
# File 'lib/selenium/webdriver/common/service.rb', line 80

def host
  @host
end

Class Method Details

.chrome(**opts) ⇒ Object



40
41
42
# File 'lib/selenium/webdriver/common/service.rb', line 40

def chrome(**opts)
  Chrome::Service.new(**opts)
end

.edge(**opts) ⇒ Object



66
67
68
# File 'lib/selenium/webdriver/common/service.rb', line 66

def edge(**opts)
  Edge::Service.new(**opts)
end

.firefox(**opts) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/selenium/webdriver/common/service.rb', line 44

def firefox(**opts)
  binary_path = Firefox::Binary.path
  args = opts.delete(:args)
  case args
  when Hash
    args[:binary] ||= binary_path
    opts[:args] = args
  when Array
    opts[:args] = ["--binary=#{binary_path}"]
    opts[:args] += args
  else
    opts[:args] = ["--binary=#{binary_path}"]
  end

  Firefox::Service.new(**opts)
end

.ie(**opts) ⇒ Object Also known as: internet_explorer



61
62
63
# File 'lib/selenium/webdriver/common/service.rb', line 61

def ie(**opts)
  IE::Service.new(**opts)
end

.safari(**opts) ⇒ Object



70
71
72
# File 'lib/selenium/webdriver/common/service.rb', line 70

def safari(**opts)
  Safari::Service.new(**opts)
end

Instance Method Details

#startObject



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/selenium/webdriver/common/service.rb', line 103

def start
  raise "already started: #{uri.inspect} #{@executable_path.inspect}" if process_running?

  Platform.exit_hook(&method(:stop)) # make sure we don't leave the server running

  socket_lock.locked do
    find_free_port
    start_process
    connect_until_stable
  end
end

#stopObject



115
116
117
118
119
120
121
122
123
124
# File 'lib/selenium/webdriver/common/service.rb', line 115

def stop
  return unless self.class.shutdown_supported

  stop_server
  @process.poll_for_exit STOP_TIMEOUT
rescue ChildProcess::TimeoutError
  nil # noop
ensure
  stop_process
end

#uriObject



126
127
128
# File 'lib/selenium/webdriver/common/service.rb', line 126

def uri
  @uri ||= URI.parse("http://#{@host}:#{@port}")
end