class Selenium::WebDriver::Remote::W3C::Bridge

Low level bridge to the remote server implementing JSON wire protocol (W3C dialect), through which the rest of the API works. @api private

www.w3.org/TR/2015/WD-webdriver-20150918/#list-of-endpoints @api private

Constants

COMMANDS
ESCAPE_CSS_REGEXP
QUIT_ERRORS
UNICODE_CODE_POINT

Public Class Methods

new(capabilities, session_id, **opts) click to toggle source
Calls superclass method Selenium::WebDriver::Remote::Bridge.new
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 34
def initialize(capabilities, session_id, **opts)
  @capabilities = capabilities
  @session_id = session_id
  super(opts)
end

Public Instance Methods

accept_alert() click to toggle source

alerts

# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 78
def accept_alert
  execute :accept_alert
end
action(async = false) click to toggle source

actions

# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 345
def action(async = false)
  W3CActionBuilder.new self,
                       Interactions.pointer(:mouse, name: 'mouse'),
                       Interactions.key('keyboard'),
                       async
end
Also aliased as: actions
actions(async = false)
Alias for: action
active_element() click to toggle source

finding elements

# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 534
def active_element
  Element.new self, element_id_from(execute(:get_active_element))
end
Also aliased as: switch_to_active_element
alert=(keys) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 86
def alert=(keys)
  execute :send_alert_text, {}, {value: keys.split(//), text: keys}
end
alert_text() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 90
def alert_text
  execute :get_alert_text
end
clear_element(element) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 397
def clear_element(element)
  execute :element_clear, id: element
end
clear_local_storage() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 251
def clear_local_storage
  execute_script('localStorage.clear()')
end
clear_session_storage() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 275
def clear_session_storage
  execute_script('sessionStorage.clear()')
end
click_element(element) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 369
def click_element(element)
  execute :element_click, id: element
end
close() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 159
def close
  execute :close_window
end
commands(command) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 44
def commands(command)
  case command
  when :status
    Remote::OSS::Bridge::COMMANDS[command]
  else
    COMMANDS[command]
  end
end
cookies() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 333
def cookies
  execute :get_all_cookies
end
delete_all_cookies() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 337
def delete_all_cookies
  execute :delete_all_cookies
end
dialect() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 40
def dialect
  :w3c
end
dismiss_alert() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 82
def dismiss_alert
  execute :dismiss_alert
end
drag_element(element, right_by, down_by) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 408
def drag_element(element, right_by, down_by)
  execute :drag_element, {id: element}, {x: right_by, y: down_by}
end
element_attribute(element, name) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 473
def element_attribute(element, name)
  WebDriver.logger.info "Using script for :getAttribute of #{name}"
  execute_atom :getAttribute, element, name
end
element_displayed?(element) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 521
def element_displayed?(element)
  WebDriver.logger.info 'Using script for :isDisplayed'
  execute_atom :isDisplayed, element
end
element_enabled?(element) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 513
def element_enabled?(element)
  execute :is_element_enabled, id: element
end
element_location(element) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 490
def element_location(element)
  data = execute :get_element_rect, id: element

  Point.new data['x'], data['y']
end
element_location_once_scrolled_into_view(element) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 502
def element_location_once_scrolled_into_view(element)
  send_keys_to_element(element, [''])
  element_location(element)
end
element_property(element, name) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 478
def element_property(element, name)
  execute :get_element_property, id: element.ref, name: name
end
element_rect(element) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 496
def element_rect(element)
  data = execute :get_element_rect, id: element

  Rectangle.new data['x'], data['y'], data['width'], data['height']
end
element_selected?(element) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 517
def element_selected?(element)
  execute :is_element_selected, id: element
end
element_size(element) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 507
def element_size(element)
  data = execute :get_element_rect, id: element

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

element properties

# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 469
def element_tag_name(element)
  execute :get_element_tag_name, id: element
end
element_text(element) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 486
def element_text(element)
  execute :get_element_text, id: element
end
element_value(element) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 482
def element_value(element)
  element_property element, 'value'
end
element_value_of_css_property(element, prop) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 526
def element_value_of_css_property(element, prop)
  execute :get_element_css_value, id: element, property_name: prop
end
execute_async_script(script, *args) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 308
def execute_async_script(script, *args)
  result = execute :execute_async_script, {}, {script: script, args: args}
  unwrap_script_result result
end
execute_script(script, *args) click to toggle source

javascript execution

# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 303
def execute_script(script, *args)
  result = execute :execute_script, {}, {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/w3c/bridge.rb, line 540
def find_element_by(how, what, parent = nil)
  how, what = convert_locators(how, what)

  id = if parent
         execute :find_child_element, {id: parent}, {using: how, value: what}
       else
         execute :find_element, {}, {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/w3c/bridge.rb, line 551
def find_elements_by(how, what, parent = nil)
  how, what = convert_locators(how, what)

  ids = if parent
          execute :find_child_elements, {id: parent}, {using: how, value: what}
        else
          execute :find_elements, {}, {using: how, value: what}
        end

  ids.map { |id| Element.new self, element_id_from(id) }
end
full_screen_window() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 203
def full_screen_window
  execute :fullscreen_window
end
get(url) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 57
def get(url)
  execute :get, {}, {url: url}
end
go_back() click to toggle source

navigation

# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 98
def go_back
  execute :back
end
go_forward() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 102
def go_forward
  execute :forward
end
implicit_wait_timeout=(milliseconds) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 61
def implicit_wait_timeout=(milliseconds)
  timeout('implicit', milliseconds)
end
keyboard() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 357
def keyboard
  raise Error::UnsupportedOperationError, '#keyboard is no longer supported, use #action instead'
end
local_storage_item(key, value = nil) click to toggle source

HTML 5

# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 235
def local_storage_item(key, value = nil)
  if value
    execute_script("localStorage.setItem('#{key}', '#{value}')")
  else
    execute_script("return localStorage.getItem('#{key}')")
  end
end
local_storage_keys() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 247
def local_storage_keys
  execute_script('return Object.keys(localStorage)')
end
local_storage_size() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 255
def local_storage_size
  execute_script('return localStorage.length')
end
location() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 283
def location
  raise Error::UnsupportedOperationError, 'The W3C standard does not currently support getting location'
end
manage() click to toggle source

cookies

# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 317
def manage
  @manage ||= WebDriver::W3CManager.new(self)
end
maximize_window(handle = :current) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 197
def maximize_window(handle = :current)
  raise Error::UnsupportedOperationError, 'Switch to desired window before changing its size' unless handle == :current

  execute :maximize_window
end
minimize_window() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 193
def minimize_window
  execute :minimize_window
end
mouse() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 353
def mouse
  raise Error::UnsupportedOperationError, '#mouse is no longer supported, use #action instead'
end
network_connection() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 291
def network_connection
  raise Error::UnsupportedOperationError, 'The W3C standard does not currently support getting network connection'
end
network_connection=(_type) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 295
def network_connection=(_type)
  raise Error::UnsupportedOperationError, 'The W3C standard does not currently support setting network connection'
end
new_window(type) click to toggle source

Create a new top-level browsing context w3c.github.io/webdriver/#new-window @param type [String] Supports two values: 'tab' and 'window'.

Use 'tab' if you'd like the new window to share an OS-level window
with the current browsing context.
Use 'window' otherwise

@return [Hash] Containing 'handle' with the value of the window handle

and 'type' with the value of the created window type
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 130
def new_window(type)
  execute :new_window, {}, {type: type}
end
page_source() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 114
def page_source
  execute_script('var source = document.documentElement.outerHTML;' \
                    'if (!source) { source = new XMLSerializer().serializeToString(document); }' \
                    'return source;')
end
quit() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 153
def quit
  execute :delete_session
  http.close
rescue *QUIT_ERRORS
end
refresh() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 163
def refresh
  execute :refresh
end
release_actions() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 365
def release_actions
  execute :release_actions
end
remove_local_storage_item(key) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 243
def remove_local_storage_item(key)
  execute_script("localStorage.removeItem('#{key}')")
end
remove_session_storage_item(key) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 267
def remove_session_storage_item(key)
  execute_script("sessionStorage.removeItem('#{key}')")
end
reposition_window(x, y) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 207
def reposition_window(x, y)
  set_window_rect(x: x, y: y)
end
resize_window(width, height, handle = :current) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 179
def resize_window(width, height, handle = :current)
  raise Error::WebDriverError, 'Switch to desired window before changing its size' unless handle == :current

  set_window_rect(width: width, height: height)
end
screen_orientation() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 461
def screen_orientation
  execute :get_screen_orientation
end
screen_orientation=(orientation) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 457
def screen_orientation=(orientation)
  execute :set_screen_orientation, {}, {orientation: orientation}
end
screenshot() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 227
def screenshot
  execute :take_screenshot
end
script_timeout=(milliseconds) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 65
def script_timeout=(milliseconds)
  timeout('script', milliseconds)
end
send_actions(data) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 361
def send_actions(data)
  execute :actions, {}, {actions: data}
end
send_keys_to_element(element, keys) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 373
def send_keys_to_element(element, keys)
  # TODO: rework file detectors before Selenium 4.0
  if @file_detector
    local_files = keys.first.split("\n").map { |key| @file_detector.call(Array(key)) }.compact
    if local_files.any?
      keys = local_files.map { |local_file| upload(local_file) }
      keys = Array(keys.join("\n"))
    end
  end

  # Keep .split(//) for backward compatibility for now
  text = keys.join('')
  execute :element_send_keys, {id: element}, {value: text.split(//), text: text}
end
session_storage_item(key, value = nil) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 259
def session_storage_item(key, value = nil)
  if value
    execute_script("sessionStorage.setItem('#{key}', '#{value}')")
  else
    execute_script("return sessionStorage.getItem('#{key}')")
  end
end
session_storage_keys() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 271
def session_storage_keys
  execute_script('return Object.keys(sessionStorage)')
end
session_storage_size() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 279
def session_storage_size
  execute_script('return sessionStorage.length')
end
set_location(_lat, _lon, _alt) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 287
def set_location(_lat, _lon, _alt)
  raise Error::UnsupportedOperationError, 'The W3C standard does not currently support setting location'
end
set_window_rect(x: nil, y: nil, width: nil, height: nil) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 216
def set_window_rect(x: nil, y: nil, width: nil, height: nil)
  params = {x: x, y: y, width: width, height: height}
  params.update(params) { |_k, v| Integer(v) unless v.nil? }
  execute :set_window_rect, {}, params
end
status() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 53
def status
  execute :status
end
submit_element(element) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 401
def submit_element(element)
  form = find_element_by('xpath', "./ancestor-or-self::form", element)
  execute_script("var e = arguments[0].ownerDocument.createEvent('Event');" \
                    "e.initEvent('submit', true, true);" \
                    'if (arguments[0].dispatchEvent(e)) { arguments[0].submit() }', form.as_json)
end
switch_to_active_element()
Alias for: active_element
switch_to_default_content() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 147
def switch_to_default_content
  switch_to_frame nil
end
switch_to_frame(id) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 138
def switch_to_frame(id)
  id = find_element_by('id', id) if id.is_a? String
  execute :switch_to_frame, {}, {id: id}
end
switch_to_parent_frame() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 143
def switch_to_parent_frame
  execute :switch_to_parent_frame
end
switch_to_window(name) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 134
def switch_to_window(name)
  execute :switch_to_window, {}, {handle: name}
end
timeout(type, milliseconds) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 69
def timeout(type, milliseconds)
  type = 'pageLoad' if type == 'page load'
  execute :set_timeout, {}, {type => milliseconds}
end
title() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 110
def title
  execute :get_title
end
touch_double_tap(element) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 416
def touch_double_tap(element)
  execute :touch_double_tap, {}, {element: element}
end
touch_down(x, y) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 424
def touch_down(x, y)
  execute :touch_down, {}, {x: x, y: y}
end
touch_element_flick(element, right_by, down_by, speed) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 450
def touch_element_flick(element, right_by, down_by, speed)
  execute :touch_flick, {}, {element: element,
                             xoffset: right_by,
                             yoffset: down_by,
                             speed: speed}
end
touch_flick(xspeed, yspeed) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 446
def touch_flick(xspeed, yspeed)
  execute :touch_flick, {}, {xspeed: xspeed, yspeed: yspeed}
end
touch_long_press(element) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 420
def touch_long_press(element)
  execute :touch_long_press, {}, {element: element}
end
touch_move(x, y) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 432
def touch_move(x, y)
  execute :touch_move, {}, {x: x, y: y}
end
touch_scroll(element, x, y) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 436
def touch_scroll(element, x, y)
  if element
    execute :touch_scroll, {}, {element: element,
                                xoffset: x,
                                yoffset: y}
  else
    execute :touch_scroll, {}, {xoffset: x, yoffset: y}
  end
end
touch_single_tap(element) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 412
def touch_single_tap(element)
  execute :touch_single_tap, {}, {element: element}
end
touch_up(x, y) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 428
def touch_up(x, y)
  execute :touch_up, {}, {x: x, y: y}
end
upload(local_file) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 388
def upload(local_file)
  unless File.file?(local_file)
    WebDriver.logger.debug("File detector only works with files. #{local_file.inspect} isn`t a file!")
    raise Error::WebDriverError, "You are trying to work with something that isn't a file."
  end

  execute :upload_file, {}, {file: Zipper.zip_file(local_file)}
end
url() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 106
def url
  execute :get_current_url
end
window_handle() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 175
def window_handle
  execute :get_window_handle
end
window_handles() click to toggle source

window handling

# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 171
def window_handles
  execute :get_window_handles
end
window_position() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 211
def window_position
  data = execute :get_window_rect
  Point.new data['x'], data['y']
end
window_rect() click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 222
def window_rect
  data = execute :get_window_rect
  Rectangle.new data['x'], data['y'], data['width'], data['height']
end
window_size(handle = :current) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 185
def window_size(handle = :current)
  raise Error::UnsupportedOperationError, 'Switch to desired window before getting its size' unless handle == :current

  data = execute :get_window_rect

  Dimension.new data['width'], data['height']
end

Private Instance Methods

convert_locators(how, what) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 569
def convert_locators(how, what)
  case how
  when 'class name'
    how = 'css selector'
    what = ".#{escape_css(what)}"
  when 'id'
    how = 'css selector'
    what = "##{escape_css(what)}"
  when 'name'
    how = 'css selector'
    what = "*[name='#{escape_css(what)}']"
  when 'tag name'
    how = 'css selector'
  end
  [how, what]
end
escape_css(string) click to toggle source

Escapes invalid characters in CSS selector. @see mathiasbynens.be/notes/css-escapes

# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 591
def escape_css(string)
  string = string.gsub(ESCAPE_CSS_REGEXP) { |match| "\\#{match}" }
  if !string.empty? && string[0] =~ /[[:digit:]]/
    string = "\\#{UNICODE_CODE_POINT + Integer(string[0])} #{string[1..-1]}"
  end

  string
end
execute(*) click to toggle source
# File lib/selenium/webdriver/remote/w3c/bridge.rb, line 565
def execute(*)
  super['value']
end