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

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

Direct Known Subclasses

Chrome::Bridge, Firefox::Bridge

Constant Summary collapse

QUIT_ERRORS =
[IOError]

Constants included from Find

Find::FINDERS

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

Methods included from Find

#find_element, #find_elements

Constructor Details

#initialize(opts = {}) ⇒ Bridge

Initializes the bridge with the given server URL.

Parameters:

  • url (String)

    url for the remote server

  • http_client (Class)

    an HTTP client class that implements the same interface as DefaultHttpClient

  • desired_capabilities (Capabilities)

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



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

def initialize(opts = {})
  opts                 = default_options.merge(opts)
  http_client_class    = opts.delete(:http_client)
  desired_capabilities = opts.delete(:desired_capabilities)
  url                  = opts.delete(:url)

  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 = URI.parse(url)
  uri.path += "/" unless uri.path =~ /\/$/

  @http         = http_client_class.new uri
  @capabilities = create_session(desired_capabilities)
end

Instance Attribute Details

#capabilitiesObject (readonly)

Returns the value of attribute capabilities.



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

def capabilities
  @capabilities
end

#contextObject

Returns the value of attribute context.



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

def context
  @context
end

#httpObject

Returns the value of attribute http.



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

def http
  @http
end

Class Method Details

.command(name, verb, url) ⇒ Object

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

Parameters:

  • name (Symbol)

    name of the resulting method

  • 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.

  • verb (Symbol)

    the appropriate http verb, such as :get, :post, or :delete



29
30
31
# File 'lib/selenium/webdriver/remote/bridge.rb', line 29

def self.command(name, verb, url)
  COMMANDS[name] = [verb, url.freeze]
end

Instance Method Details

#addCookie(cookie) ⇒ Object



184
185
186
# File 'lib/selenium/webdriver/remote/bridge.rb', line 184

def addCookie(cookie)
  execute :addCookie, {}, :cookie => cookie
end

#browserObject



69
70
71
# File 'lib/selenium/webdriver/remote/bridge.rb', line 69

def browser
  @browser ||= @capabilities.browser_name.gsub(" ", "_").to_sym
end

#clearElement(element) ⇒ Object



305
306
307
# File 'lib/selenium/webdriver/remote/bridge.rb', line 305

def clearElement(element)
  execute :clearElement, :id => element
end

#clickElement(element) ⇒ Object

Element functions



269
270
271
# File 'lib/selenium/webdriver/remote/bridge.rb', line 269

def clickElement(element)
  execute :clickElement, :id => element
end

#closeObject



151
152
153
# File 'lib/selenium/webdriver/remote/bridge.rb', line 151

def close
  execute :close
end

#create_session(desired_capabilities) ⇒ Object



85
86
87
88
89
90
# File 'lib/selenium/webdriver/remote/bridge.rb', line 85

def create_session(desired_capabilities)
  resp = raw_execute :newSession, {}, :desiredCapabilities => desired_capabilities
  @session_id = resp['sessionId'] || raise(Error::WebDriverError, 'no sessionId in returned payload')

  Capabilities.json_create resp['value']
end

#deleteAllCookiesObject



196
197
198
# File 'lib/selenium/webdriver/remote/bridge.rb', line 196

def deleteAllCookies
  execute :deleteAllCookies
end

#deleteCookie(name) ⇒ Object



188
189
190
# File 'lib/selenium/webdriver/remote/bridge.rb', line 188

def deleteCookie(name)
  execute :deleteCookieNamed, :name => name
end

#dragElement(element, rigth_by, down_by) ⇒ Object



346
347
348
# File 'lib/selenium/webdriver/remote/bridge.rb', line 346

def dragElement(element, rigth_by, down_by)
  execute :dragElement, {:id => element}, :x => rigth_by, :y => down_by
end

#driver_extensionsObject



73
74
75
# File 'lib/selenium/webdriver/remote/bridge.rb', line 73

def driver_extensions
  []
end

#elementEquals(element, other) ⇒ Object



350
351
352
# File 'lib/selenium/webdriver/remote/bridge.rb', line 350

def elementEquals(element, other)
  execute :elementEquals, :id => element.ref, :other => other.ref
end

#executeScript(script, *args) ⇒ Object



175
176
177
178
179
180
181
182
# File 'lib/selenium/webdriver/remote/bridge.rb', line 175

def executeScript(script, *args)
  unless capabilities.javascript_enabled?
    raise Error::UnsupportedOperationError, "underlying webdriver instance does not support javascript"
  end

  result = execute :executeScript, {}, :script => script, :args => args
  unwrap_script_result result
end

#findElementByClassName(parent, class_name) ⇒ Object



200
201
202
# File 'lib/selenium/webdriver/remote/bridge.rb', line 200

def findElementByClassName(parent, class_name)
  find_element_by 'class name', class_name, parent
end

#findElementByCssSelector(parent, selector) ⇒ Object



208
209
210
# File 'lib/selenium/webdriver/remote/bridge.rb', line 208

def findElementByCssSelector(parent, selector)
  find_element_by 'css selector', selector, parent
end

#findElementById(parent, id) ⇒ Object



216
217
218
# File 'lib/selenium/webdriver/remote/bridge.rb', line 216

def findElementById(parent, id)
  find_element_by 'id', id, parent
end

#findElementByLinkText(parent, link_text) ⇒ Object



224
225
226
# File 'lib/selenium/webdriver/remote/bridge.rb', line 224

def findElementByLinkText(parent, link_text)
  find_element_by 'link text', link_text, parent
end

#findElementByName(parent, name) ⇒ Object



240
241
242
# File 'lib/selenium/webdriver/remote/bridge.rb', line 240

def findElementByName(parent, name)
  find_element_by 'name', name, parent
end

#findElementByPartialLinkText(parent, link_text) ⇒ Object



232
233
234
# File 'lib/selenium/webdriver/remote/bridge.rb', line 232

def findElementByPartialLinkText(parent, link_text)
  find_element_by 'partial link text', link_text, parent
end

#findElementByTagName(parent, tag_name) ⇒ Object



248
249
250
# File 'lib/selenium/webdriver/remote/bridge.rb', line 248

def findElementByTagName(parent, tag_name)
  find_element_by 'tag name', tag_name, parent
end

#findElementByXpath(parent, xpath) ⇒ Object



256
257
258
# File 'lib/selenium/webdriver/remote/bridge.rb', line 256

def findElementByXpath(parent, xpath)
  find_element_by 'xpath', xpath, parent
end

#findElementsByClassName(parent, class_name) ⇒ Object



204
205
206
# File 'lib/selenium/webdriver/remote/bridge.rb', line 204

def findElementsByClassName(parent, class_name)
  find_elements_by 'class name', class_name, parent
end

#findElementsByCssSelector(parent, selector) ⇒ Object



212
213
214
# File 'lib/selenium/webdriver/remote/bridge.rb', line 212

def findElementsByCssSelector(parent, selector)
  find_elements_by 'css selector', selector, parent
end

#findElementsById(parent, id) ⇒ Object



220
221
222
# File 'lib/selenium/webdriver/remote/bridge.rb', line 220

def findElementsById(parent, id)
  find_elements_by 'id', id, parent
end

#findElementsByLinkText(parent, link_text) ⇒ Object



228
229
230
# File 'lib/selenium/webdriver/remote/bridge.rb', line 228

def findElementsByLinkText(parent, link_text)
  find_elements_by 'link text', link_text, parent
end

#findElementsByName(parent, name) ⇒ Object



244
245
246
# File 'lib/selenium/webdriver/remote/bridge.rb', line 244

def findElementsByName(parent, name)
  find_elements_by 'name', name, parent
end

#findElementsByPartialLinkText(parent, link_text) ⇒ Object



236
237
238
# File 'lib/selenium/webdriver/remote/bridge.rb', line 236

def findElementsByPartialLinkText(parent, link_text)
  find_elements_by 'partial link text', link_text, parent
end

#findElementsByTagName(parent, tag_name) ⇒ Object



252
253
254
# File 'lib/selenium/webdriver/remote/bridge.rb', line 252

def findElementsByTagName(parent, tag_name)
  find_elements_by 'tag name', tag_name, parent
end

#findElementsByXpath(parent, xpath) ⇒ Object



260
261
262
# File 'lib/selenium/webdriver/remote/bridge.rb', line 260

def findElementsByXpath(parent, xpath)
  find_elements_by 'xpath', xpath, parent
end

#get(url) ⇒ Object



92
93
94
# File 'lib/selenium/webdriver/remote/bridge.rb', line 92

def get(url)
  execute :get, {}, :url => url
end

#getActiveElementObject Also known as: switchToActiveElement



337
338
339
# File 'lib/selenium/webdriver/remote/bridge.rb', line 337

def getActiveElement
  Element.new self, element_id_from(execute(:getActiveElement))
end

#getAllCookiesObject



192
193
194
# File 'lib/selenium/webdriver/remote/bridge.rb', line 192

def getAllCookies
  execute :getAllCookies
end

#getCapabilitiesObject



96
97
98
# File 'lib/selenium/webdriver/remote/bridge.rb', line 96

def getCapabilities
  Capabilities.json_create execute(:getCapabilities)
end

#getCurrentUrlObject



112
113
114
# File 'lib/selenium/webdriver/remote/bridge.rb', line 112

def getCurrentUrl
  execute :getCurrentUrl
end

#getCurrentWindowHandleObject



163
164
165
# File 'lib/selenium/webdriver/remote/bridge.rb', line 163

def getCurrentWindowHandle
  execute :getCurrentWindowHandle
end

#getElementAttribute(element, name) ⇒ Object



277
278
279
# File 'lib/selenium/webdriver/remote/bridge.rb', line 277

def getElementAttribute(element, name)
  execute :getElementAttribute, :id => element, :name => name
end

#getElementLocation(element) ⇒ Object



289
290
291
292
293
# File 'lib/selenium/webdriver/remote/bridge.rb', line 289

def getElementLocation(element)
  data = execute :getElementLocation, :id => element

  Point.new data['x'], data['y']
end

#getElementSize(element) ⇒ Object



295
296
297
298
299
# File 'lib/selenium/webdriver/remote/bridge.rb', line 295

def getElementSize(element)
  data = execute :getElementSize, :id => element

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

#getElementTagName(element) ⇒ Object



273
274
275
# File 'lib/selenium/webdriver/remote/bridge.rb', line 273

def getElementTagName(element)
  execute :getElementTagName, :id => element
end

#getElementText(element) ⇒ Object



285
286
287
# File 'lib/selenium/webdriver/remote/bridge.rb', line 285

def getElementText(element)
  execute :getElementText, :id => element
end

#getElementValue(element) ⇒ Object



281
282
283
# File 'lib/selenium/webdriver/remote/bridge.rb', line 281

def getElementValue(element)
  execute :getElementValue, :id => element
end

#getElementValueOfCssProperty(element, prop) ⇒ Object



333
334
335
# File 'lib/selenium/webdriver/remote/bridge.rb', line 333

def getElementValueOfCssProperty(element, prop)
  execute :getElementValueOfCssProperty, :id => element, :property_name => prop
end

#getPageSourceObject



120
121
122
# File 'lib/selenium/webdriver/remote/bridge.rb', line 120

def getPageSource
  execute :getPageSource
end

#getSpeedObject



171
172
173
# File 'lib/selenium/webdriver/remote/bridge.rb', line 171

def getSpeed
  execute :getSpeed
end

#getTitleObject



116
117
118
# File 'lib/selenium/webdriver/remote/bridge.rb', line 116

def getTitle
  execute :getTitle
end

#getVisibleObject



124
125
126
# File 'lib/selenium/webdriver/remote/bridge.rb', line 124

def getVisible
  execute :getVisible
end

#getWindowHandlesObject



159
160
161
# File 'lib/selenium/webdriver/remote/bridge.rb', line 159

def getWindowHandles
  execute :getWindowHandles
end

#goBackObject



104
105
106
# File 'lib/selenium/webdriver/remote/bridge.rb', line 104

def goBack
  execute :goBack
end

#goForwardObject



108
109
110
# File 'lib/selenium/webdriver/remote/bridge.rb', line 108

def goForward
  execute :goForward
end

#hoverOverElement(element) ⇒ Object



342
343
344
# File 'lib/selenium/webdriver/remote/bridge.rb', line 342

def hoverOverElement(element)
  execute :hoverOverElement, :id => element
end

#isElementDisplayed(element) ⇒ Object



317
318
319
# File 'lib/selenium/webdriver/remote/bridge.rb', line 317

def isElementDisplayed(element)
  execute :isElementDisplayed, :id => element
end

#isElementEnabled(element) ⇒ Object



309
310
311
# File 'lib/selenium/webdriver/remote/bridge.rb', line 309

def isElementEnabled(element)
  execute :isElementEnabled, :id => element
end

#isElementSelected(element) ⇒ Object



313
314
315
# File 'lib/selenium/webdriver/remote/bridge.rb', line 313

def isElementSelected(element)
  execute :isElementSelected, :id => element
end

#quitObject



146
147
148
149
# File 'lib/selenium/webdriver/remote/bridge.rb', line 146

def quit
  execute :quit
rescue *QUIT_ERRORS
end

#refreshObject



155
156
157
# File 'lib/selenium/webdriver/remote/bridge.rb', line 155

def refresh
  execute :refresh
end

#sendKeysToElement(element, string) ⇒ Object



301
302
303
# File 'lib/selenium/webdriver/remote/bridge.rb', line 301

def sendKeysToElement(element, string)
  execute :sendKeysToElement, {:id => element}, {:value => string.split(//u)}
end

#session_idObject

Returns the current session ID.



81
82
83
# File 'lib/selenium/webdriver/remote/bridge.rb', line 81

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

#setElementSelected(element) ⇒ Object



329
330
331
# File 'lib/selenium/webdriver/remote/bridge.rb', line 329

def setElementSelected(element)
  execute :setElementSelected, :id => element
end

#setImplicitWaitTimeout(milliseconds) ⇒ Object



100
101
102
# File 'lib/selenium/webdriver/remote/bridge.rb', line 100

def setImplicitWaitTimeout(milliseconds)
  execute :setImplicitWaitTimeout, {}, :ms => milliseconds
end

#setSpeed(value) ⇒ Object



167
168
169
# File 'lib/selenium/webdriver/remote/bridge.rb', line 167

def setSpeed(value)
  execute :setSpeed, {}, :speed => value
end

#setVisible(bool) ⇒ Object



128
129
130
# File 'lib/selenium/webdriver/remote/bridge.rb', line 128

def setVisible(bool)
  execute :setVisible, {}, bool
end

#submitElement(element) ⇒ Object



321
322
323
# File 'lib/selenium/webdriver/remote/bridge.rb', line 321

def submitElement(element)
  execute :submitElement, :id => element
end

#switchToDefaultContentObject



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

def switchToDefaultContent
  execute :switchToFrame, {}, :id => nil
end

#switchToFrame(id) ⇒ Object



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

def switchToFrame(id)
  execute :switchToFrame, {}, :id => id
end

#switchToWindow(name) ⇒ Object



132
133
134
# File 'lib/selenium/webdriver/remote/bridge.rb', line 132

def switchToWindow(name)
  execute :switchToWindow, {}, :name => name
end

#toggleElement(element) ⇒ Object



325
326
327
# File 'lib/selenium/webdriver/remote/bridge.rb', line 325

def toggleElement(element)
  execute :toggleElement, :id => element
end