Class: Selenium::WebDriver::Remote::Bridge

Inherits:
Object
  • Object
show all
Includes:
Atoms, BridgeHelper
Defined in:
lib/selenium/webdriver/remote/bridge.rb

Direct Known Subclasses

OSS::Bridge, W3C::Bridge

Constant Summary collapse

PORT =
4444
COMMANDS =
{
  new_session: [:post, 'session']
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BridgeHelper

#element_id_from, #parse_cookie_string, #unwrap_script_result

Constructor Details

#initialize(opts = {}) ⇒ Bridge

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.

Initializes the bridge with the given server URL

Parameters:

  • opts (Hash) (defaults to: {})

    options for the driver

Options Hash (opts):

  • :url (String)

    url for the remote server

  • :http_client (Object)

    an HTTP client instance that implements the same protocol as Http::Default

  • :desired_capabilities (Capabilities)

    an instance of Remote::Capabilities describing the capabilities you want


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/selenium/webdriver/remote/bridge.rb', line 77

def initialize(opts = {})
  opts = opts.dup

  http_client = opts.delete(:http_client) { Http::Default.new }
  url = opts.delete(:url) { "http://#{Platform.localhost}:#{PORT}/wd/hub" }
  opts.delete(:options)

  unless opts.empty?
    raise ArgumentError, "unknown option#{'s' if opts.size != 1}: #{opts.inspect}"
  end

  uri = url.is_a?(URI) ? url : URI.parse(url)
  uri.path += '/' unless uri.path =~ %r{\/$}

  http_client.server_url = uri

  @http = http_client
  @file_detector = nil
end

Instance Attribute Details

#capabilitiesObject (readonly)

Returns the value of attribute capabilities


33
34
35
# File 'lib/selenium/webdriver/remote/bridge.rb', line 33

def capabilities
  @capabilities
end

#contextObject

Returns the value of attribute context


32
33
34
# File 'lib/selenium/webdriver/remote/bridge.rb', line 32

def context
  @context
end

#dialectObject (readonly)

Returns the value of attribute dialect


33
34
35
# File 'lib/selenium/webdriver/remote/bridge.rb', line 33

def dialect
  @dialect
end

#file_detectorObject

Returns the value of attribute file_detector


32
33
34
# File 'lib/selenium/webdriver/remote/bridge.rb', line 32

def file_detector
  @file_detector
end

#httpObject

Returns the value of attribute http


32
33
34
# File 'lib/selenium/webdriver/remote/bridge.rb', line 32

def http
  @http
end

Class Method Details

.handshake(**opts) ⇒ OSS:Bridge, W3C::Bridge

Implements protocol handshake which:

1. Creates session with driver.
2. Sniffs response.
3. Based on the response, understands which dialect we should use.

Returns:


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/selenium/webdriver/remote/bridge.rb', line 44

def self.handshake(**opts)
  desired_capabilities = opts.delete(:desired_capabilities) { Capabilities.new }

  if desired_capabilities.is_a?(Symbol)
    unless Capabilities.respond_to?(desired_capabilities)
      raise Error::WebDriverError, "invalid desired capability: #{desired_capabilities.inspect}"
    end

    desired_capabilities = Capabilities.__send__(desired_capabilities)
  end

  bridge = new(opts)
  capabilities = bridge.create_session(desired_capabilities, opts.delete(:options))

  case bridge.dialect
  when :oss
    Remote::OSS::Bridge.new(capabilities, bridge.session_id, **opts)
  when :w3c
    Remote::W3C::Bridge.new(capabilities, bridge.session_id, **opts)
  else
    raise WebDriverError, 'cannot understand dialect'
  end
end

Instance Method Details

#browserObject


139
140
141
142
143
144
# File 'lib/selenium/webdriver/remote/bridge.rb', line 139

def browser
  @browser ||= begin
    name = @capabilities.browser_name
    name ? name.tr(' ', '_').to_sym : 'unknown'
  end
end

#create_session(desired_capabilities, options = nil) ⇒ Object

Creates session handling both OSS and W3C dialects.


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/selenium/webdriver/remote/bridge.rb', line 101

def create_session(desired_capabilities, options = nil)
  response = execute(:new_session, {}, merged_capabilities(desired_capabilities, options))

  @session_id = response['sessionId']
  oss_status = response['status']
  value = response['value']

  if value.is_a?(Hash)
    @session_id = value['sessionId'] if value.key?('sessionId')

    if value.key?('capabilities')
      value = value['capabilities']
    elsif value.key?('value')
      value = value['value']
    end
  end

  raise Error::WebDriverError, 'no sessionId in returned payload' unless @session_id

  if oss_status
    WebDriver.logger.info 'Detected OSS dialect.'
    @dialect = :oss
    Capabilities.json_create(value)
  else
    WebDriver.logger.info 'Detected W3C dialect.'
    @dialect = :w3c
    W3C::Capabilities.json_create(value)
  end
end

#session_idObject

Returns the current session ID.


135
136
137
# File 'lib/selenium/webdriver/remote/bridge.rb', line 135

def session_id
  @session_id || raise(Error::WebDriverError, 'no current session exists')
end