class Selenium::WebDriver::Remote::Bridge
Low level bridge to the remote server, through which the rest of the API works.
@api private
Licensed to the Software Freedom Conservancy (SFC) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The SFC licenses this file to you under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Constants
- COMMANDS
- QUIT_ERRORS
Attributes
Public Class Methods
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 46 def self.command(name, verb, url) COMMANDS[name] = [verb, url.freeze] end
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 61 def initialize(opts = {}) opts = opts.dup http_client = opts.delete(:http_client) { Http::Default.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) @file_detector = nil end
Public Instance Methods
alerts
# File lib/selenium/webdriver/remote/bridge.rb, line 156 def acceptAlert execute :acceptAlert end
cookies
# File lib/selenium/webdriver/remote/bridge.rb, line 367 def addCookie(cookie) execute :addCookie, {}, :cookie => cookie end
# File lib/selenium/webdriver/remote/bridge.rb, line 91 def browser @browser ||= ( name = @capabilities.browser_name name ? name.gsub(" ", "_").to_sym : 'unknown' ) end
# File lib/selenium/webdriver/remote/bridge.rb, line 441 def clearElement(element) execute :clearElement, :id => element end
# File lib/selenium/webdriver/remote/bridge.rb, line 295 def clearLocalStorage execute :clearLocalStorage end
# File lib/selenium/webdriver/remote/bridge.rb, line 319 def clearSessionStorage execute :clearSessionStorage end
# File lib/selenium/webdriver/remote/bridge.rb, line 391 def click execute :click, {}, :button => 0 end
actions
# File lib/selenium/webdriver/remote/bridge.rb, line 387 def clickElement(element) execute :clickElement, :id => element end
# File lib/selenium/webdriver/remote/bridge.rb, line 224 def close execute :close end
# File lib/selenium/webdriver/remote/bridge.rb, line 399 def contextClick execute :click, {}, :button => 2 end
# File lib/selenium/webdriver/remote/bridge.rb, line 121 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
# File lib/selenium/webdriver/remote/bridge.rb, line 379 def deleteAllCookies execute :deleteAllCookies end
# File lib/selenium/webdriver/remote/bridge.rb, line 371 def deleteCookie(name) execute :deleteCookie, :name => name end
# File lib/selenium/webdriver/remote/bridge.rb, line 160 def dismissAlert execute :dismissAlert end
# File lib/selenium/webdriver/remote/bridge.rb, line 395 def doubleClick execute :doubleClick end
# File lib/selenium/webdriver/remote/bridge.rb, line 449 def dragElement(element, right_by, down_by) execute :dragElement, {:id => element}, :x => right_by, :y => down_by end
# File lib/selenium/webdriver/remote/bridge.rb, line 98 def driver_extensions [ DriverExtensions::HasInputDevices, DriverExtensions::UploadsFiles, DriverExtensions::TakesScreenshot, DriverExtensions::HasSessionId, DriverExtensions::Rotatable, DriverExtensions::HasTouchScreen, DriverExtensions::HasLocation, DriverExtensions::HasNetworkConnection, DriverExtensions::HasRemoteStatus, DriverExtensions::HasWebStorage ] end
# File lib/selenium/webdriver/remote/bridge.rb, line 356 def executeAsyncScript(script, *args) assert_javascript_enabled result = execute :executeAsyncScript, {}, :script => script, :args => args unwrap_script_result result end
javascript execution
# File lib/selenium/webdriver/remote/bridge.rb, line 349 def executeScript(script, *args) assert_javascript_enabled result = execute :executeScript, {}, :script => script, :args => args unwrap_script_result result end
# File lib/selenium/webdriver/remote/bridge.rb, line 591 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
# File lib/selenium/webdriver/remote/bridge.rb, line 601 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
# File lib/selenium/webdriver/remote/bridge.rb, line 132 def get(url) execute :get, {}, :url => url end
finding elements
# File lib/selenium/webdriver/remote/bridge.rb, line 586 def getActiveElement Element.new self, element_id_from(execute(:getActiveElement)) end
# File lib/selenium/webdriver/remote/bridge.rb, line 168 def getAlertText execute :getAlertText end
# File lib/selenium/webdriver/remote/bridge.rb, line 375 def getAllCookies execute :getCookies end
logs
# File lib/selenium/webdriver/remote/bridge.rb, line 511 def getAvailableLogTypes types = execute :getAvailableLogTypes Array(types).map { |e| e.to_sym } end
# File lib/selenium/webdriver/remote/bridge.rb, line 136 def getCapabilities Capabilities.json_create execute(:getCapabilities) end
# File lib/selenium/webdriver/remote/bridge.rb, line 188 def getCurrentUrl execute :getCurrentUrl end
# File lib/selenium/webdriver/remote/bridge.rb, line 240 def getCurrentWindowHandle execute :getCurrentWindowHandle end
# File lib/selenium/webdriver/remote/bridge.rb, line 536 def getElementAttribute(element, name) execute :getElementAttribute, :id => element, :name => name end
# File lib/selenium/webdriver/remote/bridge.rb, line 548 def getElementLocation(element) data = execute :getElementLocation, :id => element Point.new data['x'], data['y'] end
# File lib/selenium/webdriver/remote/bridge.rb, line 554 def getElementLocationOnceScrolledIntoView(element) data = execute :getElementLocationOnceScrolledIntoView, :id => element Point.new data['x'], data['y'] end
# File lib/selenium/webdriver/remote/bridge.rb, line 560 def getElementSize(element) data = execute :getElementSize, :id => element Dimension.new data['width'], data['height'] end
element properties
# File lib/selenium/webdriver/remote/bridge.rb, line 532 def getElementTagName(element) execute :getElementTagName, :id => element end
# File lib/selenium/webdriver/remote/bridge.rb, line 544 def getElementText(element) execute :getElementText, :id => element end
# File lib/selenium/webdriver/remote/bridge.rb, line 540 def getElementValue(element) execute :getElementValue, :id => element end
# File lib/selenium/webdriver/remote/bridge.rb, line 578 def getElementValueOfCssProperty(element, prop) execute :getElementValueOfCssProperty, :id => element, :property_name => prop end
HTML 5
# File lib/selenium/webdriver/remote/bridge.rb, line 279 def getLocalStorageItem(key) execute :getLocalStorageItem, :key => key end
# File lib/selenium/webdriver/remote/bridge.rb, line 287 def getLocalStorageKeys execute :getLocalStorageKeys end
# File lib/selenium/webdriver/remote/bridge.rb, line 299 def getLocalStorageSize execute :getLocalStorageSize end
# File lib/selenium/webdriver/remote/bridge.rb, line 327 def getLocation obj = execute(:getLocation) || {} # android returns null Location.new obj['latitude'], obj['longitude'], obj['altitude'] end
# File lib/selenium/webdriver/remote/bridge.rb, line 516 def getLog(type) data = execute :getLog, {}, :type => type.to_s Array(data).map do |l| begin LogEntry.new l.fetch('level', 'UNKNOWN'), l.fetch('timestamp'), l.fetch('message') rescue KeyError next end end end
# File lib/selenium/webdriver/remote/bridge.rb, line 337 def getNetworkConnection execute :getNetworkConnection end
# File lib/selenium/webdriver/remote/bridge.rb, line 196 def getPageSource execute :getPageSource end
# File lib/selenium/webdriver/remote/bridge.rb, line 503 def getScreenOrientation execute :getScreenOrientation end
# File lib/selenium/webdriver/remote/bridge.rb, line 271 def getScreenshot execute :screenshot end
# File lib/selenium/webdriver/remote/bridge.rb, line 303 def getSessionStorageItem(key) execute :getSessionStorageItem, :key => key end
# File lib/selenium/webdriver/remote/bridge.rb, line 311 def getSessionStorageKeys execute :getSessionStorageKeys end
# File lib/selenium/webdriver/remote/bridge.rb, line 323 def getSessionStorageSize execute :getSessionStorageSize end
# File lib/selenium/webdriver/remote/bridge.rb, line 192 def getTitle execute :getTitle end
window handling
# File lib/selenium/webdriver/remote/bridge.rb, line 236 def getWindowHandles execute :getWindowHandles end
# File lib/selenium/webdriver/remote/bridge.rb, line 265 def getWindowPosition(handle = :current) data = execute :getWindowPosition, :window_handle => handle Point.new data['x'], data['y'] end
# File lib/selenium/webdriver/remote/bridge.rb, line 254 def getWindowSize(handle = :current) data = execute :getWindowSize, :window_handle => handle Dimension.new data['width'], data['height'] end
navigation
# File lib/selenium/webdriver/remote/bridge.rb, line 180 def goBack execute :goBack end
# File lib/selenium/webdriver/remote/bridge.rb, line 184 def goForward execute :goForward end
# File lib/selenium/webdriver/remote/bridge.rb, line 574 def isElementDisplayed(element) execute :isElementDisplayed, :id => element end
# File lib/selenium/webdriver/remote/bridge.rb, line 566 def isElementEnabled(element) execute :isElementEnabled, :id => element end
# File lib/selenium/webdriver/remote/bridge.rb, line 570 def isElementSelected(element) execute :isElementSelected, :id => element end
# File lib/selenium/webdriver/remote/bridge.rb, line 250 def maximizeWindow(handle = :current) execute :maximizeWindow, :window_handle => handle end
# File lib/selenium/webdriver/remote/bridge.rb, line 403 def mouseDown execute :mouseDown end
# File lib/selenium/webdriver/remote/bridge.rb, line 411 def mouseMoveTo(element, x = nil, y = nil) params = { :element => element } if x && y params.merge! :xoffset => x, :yoffset => y end execute :mouseMoveTo, {}, params end
# File lib/selenium/webdriver/remote/bridge.rb, line 407 def mouseUp execute :mouseUp end
# File lib/selenium/webdriver/remote/bridge.rb, line 218 def quit execute :quit http.close rescue *QUIT_ERRORS end
# File lib/selenium/webdriver/remote/bridge.rb, line 228 def refresh execute :refresh end
# File lib/selenium/webdriver/remote/bridge.rb, line 283 def removeLocalStorageItem(key) execute :removeLocalStorageItem, :key => key end
# File lib/selenium/webdriver/remote/bridge.rb, line 307 def removeSessionStorageItem(key) execute :removeSessionStorageItem, :key => key end
# File lib/selenium/webdriver/remote/bridge.rb, line 421 def sendKeysToActiveElement(key) execute :sendKeysToActiveElement, {}, :value => key end
# File lib/selenium/webdriver/remote/bridge.rb, line 425 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
Returns the current session ID.
# File lib/selenium/webdriver/remote/bridge.rb, line 117 def session_id @session_id || raise(Error::WebDriverError, "no current session exists") end
# File lib/selenium/webdriver/remote/bridge.rb, line 164 def setAlertValue(keys) execute :setAlertValue, {}, :text => keys.to_s end
# File lib/selenium/webdriver/remote/bridge.rb, line 172 def setAuthentication(credentials) execute :setAuthentication, {}, credentials end
# File lib/selenium/webdriver/remote/bridge.rb, line 140 def setImplicitWaitTimeout(milliseconds) execute :implicitlyWait, {}, :ms => milliseconds end
# File lib/selenium/webdriver/remote/bridge.rb, line 291 def setLocalStorageItem(key, value) execute :setLocalStorageItem, {}, :key => key, :value => value end
# File lib/selenium/webdriver/remote/bridge.rb, line 332 def setLocation(lat, lon, alt) loc = {:latitude => lat, :longitude => lon, :altitude => alt} execute :setLocation, {}, :location => loc end
# File lib/selenium/webdriver/remote/bridge.rb, line 341 def setNetworkConnection(type) execute :setNetworkConnection, {}, :parameters => {:type => type} end
# File lib/selenium/webdriver/remote/bridge.rb, line 499 def setScreenOrientation(orientation) execute :setScreenOrientation, {}, :orientation => orientation end
# File lib/selenium/webdriver/remote/bridge.rb, line 144 def setScriptTimeout(milliseconds) execute :setScriptTimeout, {}, :ms => milliseconds end
# File lib/selenium/webdriver/remote/bridge.rb, line 315 def setSessionStorageItem(key, value) execute :setSessionStorageItem, {}, :key => key, :value => value end
# File lib/selenium/webdriver/remote/bridge.rb, line 148 def setTimeout(type, milliseconds) execute :setTimeout, {}, :type => type, :ms => milliseconds end
# File lib/selenium/webdriver/remote/bridge.rb, line 260 def setWindowPosition(x, y, handle = :current) execute :setWindowPosition, {:window_handle => handle}, :x => x, :y => y end
# File lib/selenium/webdriver/remote/bridge.rb, line 244 def setWindowSize(width, height, handle = :current) execute :setWindowSize, {:window_handle => handle}, :width => width, :height => height end
# File lib/selenium/webdriver/remote/bridge.rb, line 128 def status execute :status end
# File lib/selenium/webdriver/remote/bridge.rb, line 445 def submitElement(element) execute :submitElement, :id => element end
# File lib/selenium/webdriver/remote/bridge.rb, line 212 def switchToDefaultContent execute :switchToFrame, {}, :id => nil end
# File lib/selenium/webdriver/remote/bridge.rb, line 204 def switchToFrame(id) execute :switchToFrame, {}, :id => id end
# File lib/selenium/webdriver/remote/bridge.rb, line 208 def switchToParentFrame execute :switchToParentFrame end
# File lib/selenium/webdriver/remote/bridge.rb, line 200 def switchToWindow(name) execute :switchToWindow, {}, :name => name end
# File lib/selenium/webdriver/remote/bridge.rb, line 457 def touchDoubleTap(element) execute :touchDoubleTap, {}, :element => element end
# File lib/selenium/webdriver/remote/bridge.rb, line 465 def touchDown(x, y) execute :touchDown, {}, :x => x, :y => y end
# File lib/selenium/webdriver/remote/bridge.rb, line 491 def touchElementFlick(element, right_by, down_by, speed) execute :touchFlick, {}, :element => element, :xoffset => right_by, :yoffset => down_by, :speed => speed end
# File lib/selenium/webdriver/remote/bridge.rb, line 487 def touchFlick(xspeed, yspeed) execute :touchFlick, {}, :xspeed => xspeed, :yspeed => yspeed end
# File lib/selenium/webdriver/remote/bridge.rb, line 461 def touchLongPress(element) execute :touchLongPress, {}, :element => element end
# File lib/selenium/webdriver/remote/bridge.rb, line 473 def touchMove(x, y) execute :touchMove, {}, :x => x, :y => y end
# File lib/selenium/webdriver/remote/bridge.rb, line 477 def touchScroll(element, x, y) if element execute :touchScroll, {}, :element => element, :xoffset => x, :yoffset => y else execute :touchScroll, {}, :xoffset => x, :yoffset => y end end
# File lib/selenium/webdriver/remote/bridge.rb, line 453 def touchSingleTap(element) execute :touchSingleTap, {}, :element => element end
# File lib/selenium/webdriver/remote/bridge.rb, line 469 def touchUp(x, y) execute :touchUp, {}, :x => x, :y => y end
# File lib/selenium/webdriver/remote/bridge.rb, line 433 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
# File lib/selenium/webdriver/remote/bridge.rb, line 613 def assert_javascript_enabled return if capabilities.javascript_enabled? raise Error::UnsupportedOperationError, "underlying webdriver instance does not support javascript" end
# File lib/selenium/webdriver/remote/bridge.rb, line 651 def escaper @escaper ||= defined?(URI::Parser) ? URI::Parser.new : URI end
executes a command on the remote server.
Returns the 'value' of the returned payload
# File lib/selenium/webdriver/remote/bridge.rb, line 625 def execute(*args) raw_execute(*args)['value'] end
executes a command on the remote server.
@return [WebDriver::Remote::Response]
# File lib/selenium/webdriver/remote/bridge.rb, line 635 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