Async::WebDriver SourceAsyncWebDriverSession

class Session

A session represents a single browser session, potentially with multiple windows. It is the primary interface for interacting with a browser.

begin
	bridge = Async::WebDriver::Bridge::Pool.start(Async::WebDriver::Bridge::Chrome.new)
	session = bridge.session
	session.navigate_to("https://google.com")
	# ...
ensure
	bridge&.close
end

Definitions

def self.open(endpoint, *arguments, **options)

Open a new session.

Signature

parameter endpoint Async::HTTP::Endpoint

The endpoint to connect to.

yields {|session| ...}

The session will be closed automatically if you provide a block.

parameter session Session

The session.

returns Session

The session if no block is given.

Implementation

def self.open(endpoint, *arguments, **options)
	client = self.new(
		Async::HTTP::Client.open(endpoint),
		*arguments,
		**options
	)
	
	return client unless block_given?
	
	begin
		yield client
	ensure
		client.close
	end
end

def initialize(delegate, id, capabilities, **options)

Initialize the session.

Signature

parameter delegate Protocol::HTTP::Middleware

The underlying HTTP client (or wrapper).

parameter id String

The session identifier.

parameter capabilities Hash

The capabilities of the session.

Implementation

def initialize(delegate, id, capabilities, **options)
	@delegate = delegate
	@id = id
	@capabilities = capabilities
	
	@options = options
end

attr :delegate

Signature

attribute Protocol::HTTP::Middleware

The underlying HTTP client (or wrapper).

attr :id

Signature

attribute String

The session identifier.

attr :capabilities

Signature

attribute Hash

The capabilities of the session.

def request_path(path = nil)

The path used for making requests to the web driver bridge.

Signature

parameter path String | Nil

The path to append to the request path.

returns String

The path used for making requests to the web driver bridge.

Implementation

def request_path(path = nil)
	if path
		"/session/#{@id}/#{path}"
	else
		"/session/#{@id}"
	end
end

def close

Close the session.

Implementation

def close
	if @delegate
		self.delete
		@delegate = nil
	end
end

def session

Signature

returns Session

The session.

Implementation

def session
	self
end

def current_scope

Signature

returns Session

The current scope.

Implementation

def current_scope
	self
end

def execute(script, *arguments)

Execute a script in the current document.

Signature

parameter script String

The script to execute.

parameter arguments Array

The arguments to pass to the script.

returns Object

The result of the script.

Implementation

def execute(script, *arguments)
	post("execute/sync", {script: script, args: arguments})
end

def execute_async(script, *arguments)

Execute a script in the current document asynchronously.

Signature

parameter script String

The script to execute.

parameter arguments Array

The arguments to pass to the script.

returns Object

The result of the script.

Implementation

def execute_async(script, *arguments)
	post("execute/async", {script: script, args: arguments})
end

def reset!

Reset the session to a clean state.

Implementation

def reset!
	# Go to a blank page (in theory this should also invalidate any Element instances):
	self.navigate_to("about:blank")
	
	# Clear cookies and local storage:
	self.delete_all_cookies
	
	# This does not work consistently:
	# self.execute("localStorage.clear();")
	
	# Detach the session instance from the underlying HTTP client:
	@delegate = nil
end