class Selenium::WebDriver::Remote::Bridge

Low level bridge to the remote server, through which the rest of the API works.

@api private

@api private

Constants

QUIT_ERRORS

Attributes

capabilities[R]
context[RW]
file_detector[RW]
http[RW]

Public Class Methods

command(name, verb, url) click to toggle source

Defines a wrapper method for a command, which ultimately calls execute.

@param name [Symbol]

name of the resulting method

@param url [String]

a URL template, which can include some arguments, much like the definitions on the server.
the :session_id parameter is implicitly handled, but the remainder will become required method arguments.

@param verb [Symbol]

the appropriate http verb, such as :get, :post, or :delete
# File lib/selenium/webdriver/remote/bridge.rb, line 28
def self.command(name, verb, url)
  COMMANDS[name] = [verb, url.freeze]
end
new(opts = {}) click to toggle source

Initializes the bridge with the given server URL.

@param url [String] url for the remote server @param http_client [Object] an HTTP client instance that implements the same protocol as Http::Default @param desired_capabilities [Capabilities] an instance of Remote::Capabilities describing the capabilities you want

# File lib/selenium/webdriver/remote/bridge.rb, line 43
def initialize(opts = {})
  opts = opts.dup

  http_client          = opts.delete(:http_client) { Http::Persistent.new }
  desired_capabilities = opts.delete(:desired_capabilities) { Capabilities.firefox }
  url                  = opts.delete(:url) { "http://#{Platform.localhost}:4444/wd/hub" }

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

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

    desired_capabilities = Capabilities.send(desired_capabilities)
  end

  uri = url.kind_of?(URI) ? url : URI.parse(url)
  uri.path += "/" unless uri.path =~ /\/$/

  http_client.server_url = uri

  @http          = http_client
  @capabilities  = create_session(desired_capabilities)
end

Public Instance Methods

acceptAlert() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 137
def acceptAlert
  execute :acceptAlert
end
addCookie(cookie) click to toggle source

cookies

# File lib/selenium/webdriver/remote/bridge.rb, line 348
def addCookie(cookie)
  execute :addCookie, {}, :cookie => cookie
end
browser() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 71
def browser
  @browser ||= (
    name = @capabilities.browser_name
    name ? name.gsub(" ", "_").to_sym : 'unknown'
  )
end
clearElement(element) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 422
def clearElement(element)
  execute :clearElement, :id => element
end
clearLocalStorage() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 276
def clearLocalStorage
  execute :clearLocalStorage
end
clearSessionStorage() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 300
def clearSessionStorage
  execute :clearSessionStorage
end
click() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 372
def click
  execute :click, {}, :button => 0
end
clickElement(element) click to toggle source

actions

# File lib/selenium/webdriver/remote/bridge.rb, line 368
def clickElement(element)
  execute :clickElement, :id => element
end
close() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 205
def close
  execute :close
end
contextClick() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 380
def contextClick
  execute :click, {}, :button => 2
end
create_session(desired_capabilities) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 98
def create_session(desired_capabilities)
  resp = raw_execute :newSession, {}, :desiredCapabilities => desired_capabilities
  @session_id = resp['sessionId'] or raise Error::WebDriverError, 'no sessionId in returned payload'

  Capabilities.json_create resp['value']
end
deleteAllCookies() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 360
def deleteAllCookies
  execute :deleteAllCookies
end
deleteCookie(name) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 352
def deleteCookie(name)
  execute :deleteCookie, :name => name
end
dismissAlert() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 141
def dismissAlert
  execute :dismissAlert
end
doubleClick() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 376
def doubleClick
  execute :doubleClick
end
dragElement(element, right_by, down_by) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 431
def dragElement(element, right_by, down_by)
  execute :dragElement, {:id => element}, :x => right_by, :y => down_by
end
driver_extensions() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 78
def driver_extensions
  [
    DriverExtensions::HasInputDevices,
    DriverExtensions::UploadsFiles,
    DriverExtensions::TakesScreenshot,
    DriverExtensions::HasSessionId,
    DriverExtensions::Rotatable,
    DriverExtensions::HasTouchScreen,
    DriverExtensions::HasRemoteStatus
  ]
end
elementEquals(element, other) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 559
def elementEquals(element, other)
  if element.ref == other.ref
    true
  else
    execute :elementEquals, :id => element.ref, :other => other.ref
  end
end
executeAsyncScript(script, *args) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 337
def executeAsyncScript(script, *args)
  assert_javascript_enabled

  result = execute :executeAsyncScript, {}, :script => script, :args => args
  unwrap_script_result result
end
executeScript(script, *args) click to toggle source

javascript execution

# File lib/selenium/webdriver/remote/bridge.rb, line 330
def executeScript(script, *args)
  assert_javascript_enabled

  result = execute :executeScript, {}, :script => script, :args => args
  unwrap_script_result result
end
find_element_by(how, what, parent = nil) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 576
def find_element_by(how, what, parent = nil)
  if parent
    id = execute :findChildElement, {:id => parent}, {:using => how, :value => what}
  else
    id = execute :findElement, {}, {:using => how, :value => what}
  end

  Element.new self, element_id_from(id)
end
find_elements_by(how, what, parent = nil) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 586
def find_elements_by(how, what, parent = nil)
  if parent
    ids = execute :findChildElements, {:id => parent}, {:using => how, :value => what}
  else
    ids = execute :findElements, {}, {:using => how, :value => what}
  end

  ids.map { |id| Element.new self, element_id_from(id) }
end
get(url) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 109
def get(url)
  execute :get, {}, :url => url
end
getActiveElement() click to toggle source

finding elements

# File lib/selenium/webdriver/remote/bridge.rb, line 571
def getActiveElement
  Element.new self, element_id_from(execute(:getActiveElement))
end
Also aliased as: switchToActiveElement
getAlert() click to toggle source

alerts

# File lib/selenium/webdriver/remote/bridge.rb, line 133
def getAlert
  execute :getAlert
end
getAlertText() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 149
def getAlertText
  execute :getAlertText
end
getAllCookies() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 356
def getAllCookies
  execute :getCookies
end
getAvailableLogTypes() click to toggle source

logs

# File lib/selenium/webdriver/remote/bridge.rb, line 493
def getAvailableLogTypes
  types = execute :getAvailableLogTypes
  Array(types).map { |e| e.to_sym }
end
getCapabilities() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 113
def getCapabilities
  Capabilities.json_create execute(:getCapabilities)
end
getCurrentUrl() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 165
def getCurrentUrl
  execute :getCurrentUrl
end
getCurrentWindowHandle() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 221
def getCurrentWindowHandle
  execute :getCurrentWindowHandle
end
getElementAttribute(element, name) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 514
def getElementAttribute(element, name)
  execute :getElementAttribute, :id => element, :name => name
end
getElementLocation(element) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 526
def getElementLocation(element)
  data = execute :getElementLocation, :id => element

  Point.new data['x'], data['y']
end
getElementLocationOnceScrolledIntoView(element) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 532
def getElementLocationOnceScrolledIntoView(element)
  data = execute :getElementLocationOnceScrolledIntoView, :id => element

  Point.new data['x'], data['y']
end
getElementSize(element) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 538
def getElementSize(element)
  data = execute :getElementSize, :id => element

  Dimension.new data['width'], data['height']
end
getElementTagName(element) click to toggle source

element properties

# File lib/selenium/webdriver/remote/bridge.rb, line 510
def getElementTagName(element)
  execute :getElementTagName, :id => element
end
getElementText(element) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 522
def getElementText(element)
  execute :getElementText, :id => element
end
getElementValue(element) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 518
def getElementValue(element)
  execute :getElementValue, :id => element
end
getElementValueOfCssProperty(element, prop) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 555
def getElementValueOfCssProperty(element, prop)
  execute :getElementValueOfCssProperty, :id => element, :property_name => prop
end
getLocalStorageItem(key) click to toggle source

HTML 5

# File lib/selenium/webdriver/remote/bridge.rb, line 260
def getLocalStorageItem(key)
  execute :getLocalStorageItem, :key => key
end
getLocalStorageKeys() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 268
def getLocalStorageKeys
  execute :getLocalStorageKeys
end
getLocalStorageSize() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 280
def getLocalStorageSize
  execute :getLocalStorageSize
end
getLocation() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 308
def getLocation
  obj = execute(:getLocation) || {} # android returns null
  Location.new obj['latitude'], obj['longitude'], obj['altitude']
end
getLog(type) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 498
def getLog(type)
  data = execute :getLog, {}, :type => type.to_s

  Array(data).map do |l|
    LogEntry.new l.fetch('level'), l.fetch('timestamp'), l.fetch('message')
  end
end
getPageSource() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 173
def getPageSource
  execute :getPageSource
end
getScreenOrientation() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 485
def getScreenOrientation
  execute :getScreenOrientation
end
getScreenshot() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 252
def getScreenshot
  execute :screenshot
end
getSessionStorageItem(key) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 284
def getSessionStorageItem(key)
  execute :getSessionStorageItem, :key => key
end
getSessionStorageKeys() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 292
def getSessionStorageKeys
  execute :getSessionStorageKeys
end
getSessionStorageSize() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 304
def getSessionStorageSize
  execute :getSessionStorageSize
end
getTitle() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 169
def getTitle
  execute :getTitle
end
getVisible() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 177
def getVisible
  execute :getVisible
end
getWindowHandles() click to toggle source

window handling

# File lib/selenium/webdriver/remote/bridge.rb, line 217
def getWindowHandles
  execute :getWindowHandles
end
getWindowPosition(handle = :current) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 246
def getWindowPosition(handle = :current)
  data = execute :getWindowPosition, :window_handle => handle

  Point.new data['x'], data['y']
end
getWindowSize(handle = :current) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 235
def getWindowSize(handle = :current)
  data = execute :getWindowSize, :window_handle => handle

  Dimension.new data['width'], data['height']
end
goBack() click to toggle source

navigation

# File lib/selenium/webdriver/remote/bridge.rb, line 157
def goBack
  execute :goBack
end
goForward() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 161
def goForward
  execute :goForward
end
isBrowserOnline() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 318
def isBrowserOnline
  execute :isBrowserOnline
end
isElementDisplayed(element) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 552
def isElementDisplayed(element)
  execute :isElementDisplayed, :id => element
end
isElementEnabled(element) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 544
def isElementEnabled(element)
  execute :isElementEnabled, :id => element
end
isElementSelected(element) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 548
def isElementSelected(element)
  execute :isElementSelected, :id => element
end
maximizeWindow(handle = :current) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 231
def maximizeWindow(handle = :current)
  execute :maximizeWindow, :window_handle => handle
end
mouseDown() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 384
def mouseDown
  execute :mouseDown
end
mouseMoveTo(element, x = nil, y = nil) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 392
def mouseMoveTo(element, x = nil, y = nil)
  params = { :element => element }

  if x && y
    params.merge! :xoffset => x, :yoffset => y
  end

  execute :mouseMoveTo, {}, params
end
mouseUp() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 388
def mouseUp
  execute :mouseUp
end
quit() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 199
def quit
  execute :quit
  http.close
rescue *QUIT_ERRORS
end
refresh() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 209
def refresh
  execute :refresh
end
removeLocalStorageItem(key) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 264
def removeLocalStorageItem(key)
  execute :removeLocalStorageItem, :key => key
end
removeSessionStorageItem(key) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 288
def removeSessionStorageItem(key)
  execute :removeSessionStorageItem, :key => key
end
sendKeysToActiveElement(key) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 402
def sendKeysToActiveElement(key)
  execute :sendKeysToActiveElement, {}, :value => key
end
sendKeysToElement(element, keys) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 406
def sendKeysToElement(element, keys)
  if @file_detector && local_file = @file_detector.call(keys)
    keys = upload(local_file)
  end

  execute :sendKeysToElement, {:id => element}, {:value => Array(keys)}
end
session_id() click to toggle source

Returns the current session ID.

# File lib/selenium/webdriver/remote/bridge.rb, line 94
def session_id
  @session_id || raise(Error::WebDriverError, "no current session exists")
end
setAlertValue(keys) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 145
def setAlertValue(keys)
  execute :setAlertValue, {}, :text => keys.to_s
end
setBrowserOnline(bool) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 322
def setBrowserOnline(bool)
  execute :setBrowserOnline, {}, :state => bool
end
setImplicitWaitTimeout(milliseconds) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 117
def setImplicitWaitTimeout(milliseconds)
  execute :implicitlyWait, {}, :ms => milliseconds
end
setLocalStorageItem(key, value) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 272
def setLocalStorageItem(key, value)
  execute :setLocalStorageItem, {}, :key => key, :value => value
end
setLocation(lat, lon, alt) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 313
def setLocation(lat, lon, alt)
  loc = {:latitude => lat, :longitude => lon, :altitude => alt}
  execute :setLocation, {}, :location => loc
end
setScreenOrientation(orientation) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 481
def setScreenOrientation(orientation)
  execute :setScreenOrientation, {}, :orientation => orientation
end
setScriptTimeout(milliseconds) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 121
def setScriptTimeout(milliseconds)
  execute :setScriptTimeout, {}, :ms => milliseconds
end
setSessionStorageItem(key, value) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 296
def setSessionStorageItem(key, value)
  execute :setSessionStorageItem, {}, :key => key, :value => value
end
setTimeout(type, milliseconds) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 125
def setTimeout(type, milliseconds)
  execute :setTimeout, {}, :type => type, :ms => milliseconds
end
setVisible(bool) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 181
def setVisible(bool)
  execute :setVisible, {}, bool
end
setWindowPosition(x, y, handle = :current) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 241
def setWindowPosition(x, y, handle = :current)
  execute :setWindowPosition, {:window_handle => handle},
                               :x => x, :y => y
end
setWindowSize(width, height, handle = :current) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 225
def setWindowSize(width, height, handle = :current)
  execute :setWindowSize, {:window_handle => handle},
                           :width  => width,
                           :height => height
end
status() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 105
def status
  execute :status
end
submitElement(element) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 427
def submitElement(element)
  execute :submitElement, :id => element
end
switchToActiveElement()
Alias for: getActiveElement
switchToDefaultContent() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 193
def switchToDefaultContent
  execute :switchToFrame, {}, :id => nil
end
switchToFrame(id) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 189
def switchToFrame(id)
  execute :switchToFrame, {}, :id => id
end
switchToWindow(name) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 185
def switchToWindow(name)
  execute :switchToWindow, {}, :name => name
end
touchDoubleTap(element) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 439
def touchDoubleTap(element)
  execute :touchDoubleTap, {}, :element => element
end
touchDown(x, y) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 447
def touchDown(x, y)
  execute :touchDown, {}, :x => x, :y => y
end
touchElementFlick(element, right_by, down_by, speed) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 473
def touchElementFlick(element, right_by, down_by, speed)
  execute :touchFlick, {}, :element => element,
                           :xoffset => right_by,
                           :yoffset => down_by,
                           :speed   => speed

end
touchFlick(xspeed, yspeed) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 469
def touchFlick(xspeed, yspeed)
  execute :touchFlick, {}, :xspeed => xspeed, :yspeed => yspeed
end
touchLongPress(element) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 443
def touchLongPress(element)
  execute :touchLongPress, {}, :element => element
end
touchMove(x, y) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 455
def touchMove(x, y)
  execute :touchMove, {}, :x => x, :y => y
end
touchScroll(element, x, y) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 459
def touchScroll(element, x, y)
  if element
    execute :touchScroll, {}, :element => element,
                              :xoffset => x,
                              :yoffset => y
  else
    execute :touchScroll, {}, :xoffset => x, :yoffset => y
  end
end
touchSingleTap(element) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 435
def touchSingleTap(element)
  execute :touchSingleTap, {}, :element => element
end
touchUp(x, y) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 451
def touchUp(x, y)
  execute :touchUp, {}, :x => x, :y => y
end
upload(local_file) click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 414
def upload(local_file)
  unless File.file?(local_file)
    raise Error::WebDriverError, "you may only upload files: #{local_file.inspect}"
  end

  execute :uploadFile, {}, :file => Zipper.zip_file(local_file)
end

Private Instance Methods

assert_javascript_enabled() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 598
def assert_javascript_enabled
  unless capabilities.javascript_enabled?
    raise Error::UnsupportedOperationError, "underlying webdriver instance does not support javascript"
  end
end
escaper() click to toggle source
# File lib/selenium/webdriver/remote/bridge.rb, line 637
def escaper
  @escaper ||= defined?(URI::Parser) ? URI::Parser.new : URI
end
execute(*args) click to toggle source

executes a command on the remote server.

Returns the 'value' of the returned payload

# File lib/selenium/webdriver/remote/bridge.rb, line 611
def execute(*args)
  raw_execute(*args)['value']
end
raw_execute(command, opts = {}, command_hash = nil) click to toggle source

executes a command on the remote server.

@return [WebDriver::Remote::Response]

# File lib/selenium/webdriver/remote/bridge.rb, line 621
def raw_execute(command, opts = {}, command_hash = nil)
  verb, path = COMMANDS[command] || raise(ArgumentError, "unknown command: #{command.inspect}")
  path       = path.dup

  path[':session_id'] = @session_id if path.include?(":session_id")

  begin
    opts.each { |key, value| path[key.inspect] = escaper.escape(value.to_s) }
  rescue IndexError
    raise ArgumentError, "#{opts.inspect} invalid for #{command.inspect}"
  end

  puts "-> #{verb.to_s.upcase} #{path}" if $DEBUG
  http.call verb, path, command_hash
end