Class: Async::WebDriver::Bridge::Driver

Inherits:
Object
  • Object
show all
Defined in:
lib/async/webdriver/bridge/driver.rb

Overview

Represents an instance of a locally running driver (usually with a process group).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Driver

Initialize a driver wrapper.



13
14
15
16
17
# File 'lib/async/webdriver/bridge/driver.rb', line 13

def initialize(**options)
	@options = options
	@count = 0
	@closed = false
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



24
25
26
# File 'lib/async/webdriver/bridge/driver.rb', line 24

def count
  @count
end

#statusObject (readonly)

Returns the value of attribute status.



27
28
29
# File 'lib/async/webdriver/bridge/driver.rb', line 27

def status
  @status
end

Instance Method Details

#clientObject



71
72
73
# File 'lib/async/webdriver/bridge/driver.rb', line 71

def client
	Client.open(self.endpoint)
end

#closeObject

Mark the driver as closed.



40
41
42
# File 'lib/async/webdriver/bridge/driver.rb', line 40

def close
	@closed = true
end

#closed?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/async/webdriver/bridge/driver.rb', line 35

def closed?
	@closed
end

#concurrencyObject



20
21
22
# File 'lib/async/webdriver/bridge/driver.rb', line 20

def concurrency
	@options.fetch(:concurrency, 128)
end

#endpointObject



66
67
68
# File 'lib/async/webdriver/bridge/driver.rb', line 66

def endpoint
	Async::HTTP::Endpoint.parse("http://localhost", port: self.port)
end

#ephemeral_portObject

Generate a port number for the driver to listen on if it was not specified.



51
52
53
54
55
56
57
58
# File 'lib/async/webdriver/bridge/driver.rb', line 51

def ephemeral_port
	address = ::Addrinfo.tcp("localhost", 0)
	
	address.bind do |socket|
		# We assume that it's unlikely the port will be reused any time soon...
		return socket.local_address.ip_port
	end
end

#portObject



61
62
63
# File 'lib/async/webdriver/bridge/driver.rb', line 61

def port
	@port ||= @options.fetch(:port, self.ephemeral_port)
end

#reusable?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/async/webdriver/bridge/driver.rb', line 45

def reusable?
	@options.fetch(:reusable, !@closed)
end

#start(retries: 100) ⇒ Object

Start the driver.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/async/webdriver/bridge/driver.rb', line 77

def start(retries: 100)
	endpoint = self.endpoint
	
	Console.debug(self, "Waiting for driver to start...", endpoint: endpoint)
	count = 0
	
	Async::HTTP::Client.open(endpoint) do |client|
		begin
			response = client.get("/status")
			@status = JSON.parse(response.read)["value"]
			Console.debug(self, "Successfully connected to driver.", status: @status)
		rescue Errno::ECONNREFUSED
			if count < retries
				count += 1
				sleep(0.01 * count)
				Console.debug(self, "Driver not ready, retrying...")
				retry
			else
				raise
			end
		end
	end
end

#The status of the driver after a connection has been established.=(statusofthedriverafteraconnectionhasbeenestablished. = (value)) ⇒ Object



27
# File 'lib/async/webdriver/bridge/driver.rb', line 27

attr :status

#viable?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/async/webdriver/bridge/driver.rb', line 30

def viable?
	!@closed
end