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

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 (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



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

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

  http_client.server_url = uri

  @http         = http_client
  @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



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

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

#browserObject



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

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

#clearElement(element) ⇒ Object



307
308
309
# File 'lib/selenium/webdriver/remote/bridge.rb', line 307

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

#clickElement(element) ⇒ Object

Element functions



271
272
273
# File 'lib/selenium/webdriver/remote/bridge.rb', line 271

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

#closeObject



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

def close
  execute :close
end

#create_session(desired_capabilities) ⇒ Object



88
89
90
91
92
93
# File 'lib/selenium/webdriver/remote/bridge.rb', line 88

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



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

def deleteAllCookies
  execute :deleteAllCookies
end

#deleteCookie(name) ⇒ Object



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

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

#dragElement(element, rigth_by, down_by) ⇒ Object



348
349
350
# File 'lib/selenium/webdriver/remote/bridge.rb', line 348

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

#driver_extensionsObject



76
77
78
# File 'lib/selenium/webdriver/remote/bridge.rb', line 76

def driver_extensions
  []
end

#elementEquals(element, other) ⇒ Object



352
353
354
# File 'lib/selenium/webdriver/remote/bridge.rb', line 352

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

#executeScript(script, *args) ⇒ Object



178
179
180
181
182
183
184
185
# File 'lib/selenium/webdriver/remote/bridge.rb', line 178

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



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

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

#findElementByCssSelector(parent, selector) ⇒ Object



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

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

#findElementById(parent, id) ⇒ Object



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

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

#findElementByLinkText(parent, link_text) ⇒ Object



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

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

#findElementByName(parent, name) ⇒ Object



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

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

#findElementByPartialLinkText(parent, link_text) ⇒ Object



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

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

#findElementByTagName(parent, tag_name) ⇒ Object



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

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

#findElementByXpath(parent, xpath) ⇒ Object



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

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

#findElementsByClassName(parent, class_name) ⇒ Object



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

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

#findElementsByCssSelector(parent, selector) ⇒ Object



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

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

#findElementsById(parent, id) ⇒ Object



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

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

#findElementsByLinkText(parent, link_text) ⇒ Object



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

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

#findElementsByName(parent, name) ⇒ Object



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

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

#findElementsByPartialLinkText(parent, link_text) ⇒ Object



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

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

#findElementsByTagName(parent, tag_name) ⇒ Object



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

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

#findElementsByXpath(parent, xpath) ⇒ Object



263
264
265
# File 'lib/selenium/webdriver/remote/bridge.rb', line 263

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

#get(url) ⇒ Object



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

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

#getActiveElementObject Also known as: switchToActiveElement



339
340
341
# File 'lib/selenium/webdriver/remote/bridge.rb', line 339

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

#getAllCookiesObject



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

def getAllCookies
  execute :getAllCookies
end

#getCapabilitiesObject



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

def getCapabilities
  Capabilities.json_create execute(:getCapabilities)
end

#getCurrentUrlObject



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

def getCurrentUrl
  execute :getCurrentUrl
end

#getCurrentWindowHandleObject



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

def getCurrentWindowHandle
  execute :getCurrentWindowHandle
end

#getElementAttribute(element, name) ⇒ Object



279
280
281
# File 'lib/selenium/webdriver/remote/bridge.rb', line 279

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

#getElementLocation(element) ⇒ Object



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

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

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

#getElementSize(element) ⇒ Object



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

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

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

#getElementTagName(element) ⇒ Object



275
276
277
# File 'lib/selenium/webdriver/remote/bridge.rb', line 275

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

#getElementText(element) ⇒ Object



287
288
289
# File 'lib/selenium/webdriver/remote/bridge.rb', line 287

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

#getElementValue(element) ⇒ Object



283
284
285
# File 'lib/selenium/webdriver/remote/bridge.rb', line 283

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

#getElementValueOfCssProperty(element, prop) ⇒ Object



335
336
337
# File 'lib/selenium/webdriver/remote/bridge.rb', line 335

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

#getPageSourceObject



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

def getPageSource
  execute :getPageSource
end

#getSpeedObject



174
175
176
# File 'lib/selenium/webdriver/remote/bridge.rb', line 174

def getSpeed
  execute :getSpeed
end

#getTitleObject



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

def getTitle
  execute :getTitle
end

#getVisibleObject



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

def getVisible
  execute :getVisible
end

#getWindowHandlesObject



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

def getWindowHandles
  execute :getWindowHandles
end

#goBackObject



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

def goBack
  execute :goBack
end

#goForwardObject



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

def goForward
  execute :goForward
end

#hoverOverElement(element) ⇒ Object



344
345
346
# File 'lib/selenium/webdriver/remote/bridge.rb', line 344

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

#isElementDisplayed(element) ⇒ Object



319
320
321
# File 'lib/selenium/webdriver/remote/bridge.rb', line 319

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

#isElementEnabled(element) ⇒ Object



311
312
313
# File 'lib/selenium/webdriver/remote/bridge.rb', line 311

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

#isElementSelected(element) ⇒ Object



315
316
317
# File 'lib/selenium/webdriver/remote/bridge.rb', line 315

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

#quitObject



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

def quit
  execute :quit
rescue *QUIT_ERRORS
end

#refreshObject



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

def refresh
  execute :refresh
end

#sendKeysToElement(element, string) ⇒ Object



303
304
305
# File 'lib/selenium/webdriver/remote/bridge.rb', line 303

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

#session_idObject

Returns the current session ID.



84
85
86
# File 'lib/selenium/webdriver/remote/bridge.rb', line 84

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

#setElementSelected(element) ⇒ Object



331
332
333
# File 'lib/selenium/webdriver/remote/bridge.rb', line 331

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

#setImplicitWaitTimeout(milliseconds) ⇒ Object



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

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

#setSpeed(value) ⇒ Object



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

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

#setVisible(bool) ⇒ Object



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

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

#submitElement(element) ⇒ Object



323
324
325
# File 'lib/selenium/webdriver/remote/bridge.rb', line 323

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

#switchToDefaultContentObject



143
144
145
# File 'lib/selenium/webdriver/remote/bridge.rb', line 143

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

#switchToFrame(id) ⇒ Object



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

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

#switchToWindow(name) ⇒ Object



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

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

#toggleElement(element) ⇒ Object



327
328
329
# File 'lib/selenium/webdriver/remote/bridge.rb', line 327

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