Class: Selenium::WebDriver::IE::Bridge

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/selenium/webdriver/ie/bridge.rb

Constant Summary

Constants included from Util

Util::CP_UTF8

Instance Method Summary collapse

Methods included from Util

#create_element, #create_element_collection, #create_string, #extract_elements_from, #extract_string_from, #string_array_from, #wstring_ptr, #wstring_to_bytestring

Constructor Details

#initializeBridge

Returns a new instance of Bridge.



12
13
14
15
16
17
18
19
20
# File 'lib/selenium/webdriver/ie/bridge.rb', line 12

def initialize
  ptr_ref = FFI::MemoryPointer.new :pointer

  check_error_code Lib.wdNewDriverInstance(ptr_ref),
                   "could not create driver instance"

  @driver_pointer = ptr_ref.get_pointer(0)
  @speed          = :fast
end

Instance Method Details

#addCookie(opts) ⇒ Object

Configuration



170
171
172
173
174
175
176
177
# File 'lib/selenium/webdriver/ie/bridge.rb', line 170

def addCookie(opts)
  cookie_string  = "#{opts[:name]}=#{opts[:value]}; "
  cookie_string << "path=#{opts[:path]}; "                  if opts[:path] && !opts[:path].empty?
  cookie_string << "domain=#{opts[:domain][/^(.+?):/, 1]};" if opts[:domain] && !opts[:domain].empty?

  check_error_code Lib.wdAddCookie(@driver_pointer, wstring_ptr(cookie_string)),
                   "unable to add cookie"
end

#browserObject



22
23
24
# File 'lib/selenium/webdriver/ie/bridge.rb', line 22

def browser
  :internet_explorer
end

#clearElement(element_pointer) ⇒ Object



376
377
378
# File 'lib/selenium/webdriver/ie/bridge.rb', line 376

def clearElement(element_pointer)
  check_error_code Lib.wdeClear(element_pointer), "unable to clear element"
end

#clickElement(element_pointer) ⇒ Object

Element functions



338
339
340
# File 'lib/selenium/webdriver/ie/bridge.rb', line 338

def clickElement(element_pointer)
  check_error_code Lib.wdeClick(element_pointer), "unable to click element"
end

#closeObject



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

def close
  check_error_code Lib.wdClose(@driver_pointer), "unable to close driver"
end

#deleteAllCookiesObject

Raises:

  • (NotImplementedError)


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

def deleteAllCookies
  raise NotImplementedError, "missing from IE driver"
end

#deleteCookie(name) ⇒ Object

Raises:

  • (NotImplementedError)


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

def deleteCookie(name)
  raise NotImplementedError, "missing from IE driver"
end

#dragElement(element_pointer, right_by, down_by) ⇒ Object



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/selenium/webdriver/ie/bridge.rb', line 440

def dragElement(element_pointer, right_by, down_by)
  # TODO: check return values?
  hwnd                = FFI::MemoryPointer.new :pointer
  x, y, width, height = Array.new(4) { FFI::MemoryPointer.new :long }

  check_error_code Lib.wdeGetDetailsOnceScrolledOnToScreen(element_pointer, hwnd, x, y, width, height),
                   "unable to determine location once scrolled on to screen"

  Lib.wdeMouseDownAt(hwnd.get_pointer(0), x.get_long(0), y.get_long(0))

  destination_x = x.get_long(0) + right_by
  destination_y = y.get_long(0) + down_by
  duration      = 500 # TODO: parent.manage().getSpeed().getTimeOut();

  Lib.wdeMouseMoveTo(hwnd.get_pointer(0), duration, x.get_long(0), y.get_long(0), destination_x, destination_y)
  Lib.wdeMouseUpAt(hwnd.get_pointer(0), destination_x, destination_y)
end

#driver_extensionsObject



26
27
28
# File 'lib/selenium/webdriver/ie/bridge.rb', line 26

def driver_extensions
  []
end

#elementEquals(element, other) ⇒ Object



436
437
438
# File 'lib/selenium/webdriver/ie/bridge.rb', line 436

def elementEquals(element, other)
  executeScript "return arguments[0] === arguments[1]", element, other
end

#executeScript(script, *args) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/selenium/webdriver/ie/bridge.rb', line 142

def executeScript(script, *args)
  script_args_ref = FFI::MemoryPointer.new :pointer
  result          = Lib.wdNewScriptArgs(script_args_ref, args.size)

  check_error_code result, "unable to create new script arguments array"

  args_pointer = script_args_ref.get_pointer(0)
  populate_arguments(result, args_pointer, args)

  script            = "(function() { return function(){" + script + "};})();"
  script_result_ref = FFI::MemoryPointer.new :pointer

  check_error_code Lib.wdExecuteScript(@driver_pointer, wstring_ptr(script), args_pointer, script_result_ref),
                   "Cannot execute script"

  extract_return_value script_result_ref
ensure
  Lib.wdFreeScriptArgs(args_pointer) if args_pointer
end

#finalize(element_pointer) ⇒ Object



476
477
478
479
# File 'lib/selenium/webdriver/ie/bridge.rb', line 476

def finalize(element_pointer)
  check_error_code Lib.wdeFreeElement(element_pointer),
                   "unable to finalize #{element_pointer} for #{self}"
end

#findElementByClassName(parent, class_name) ⇒ Object

Finders



219
220
221
222
223
224
# File 'lib/selenium/webdriver/ie/bridge.rb', line 219

def findElementByClassName(parent, class_name)
  create_element do |raw_element|
    check_error_code Lib.wdFindElementByClassName(@driver_pointer, parent, wstring_ptr(class_name), raw_element),
                     "unable to find element by class name using #{class_name.inspect}"
  end
end

#findElementByCssSelector(parent, selector) ⇒ Object



233
234
235
236
237
238
# File 'lib/selenium/webdriver/ie/bridge.rb', line 233

def findElementByCssSelector(parent, selector)
  create_element do |raw_element|
    check_error_code Lib.wdFindElementByCss(@driver_pointer, parent, wstring_ptr(selector), raw_element),
                     "unable to find element by css selector using #{selector.inspect}"
  end
end

#findElementById(parent, id) ⇒ Object



247
248
249
250
251
252
# File 'lib/selenium/webdriver/ie/bridge.rb', line 247

def findElementById(parent, id)
  create_element do |raw_element|
    check_error_code Lib.wdFindElementById(@driver_pointer, parent, wstring_ptr(id), raw_element),
                     "unable to find element by id using #{id.inspect}"
  end
end

#findElementByLinkText(parent, link_text) ⇒ Object



261
262
263
264
265
266
# File 'lib/selenium/webdriver/ie/bridge.rb', line 261

def findElementByLinkText(parent, link_text)
  create_element do |raw_element|
    check_error_code Lib.wdFindElementByLinkText(@driver_pointer, parent, wstring_ptr(link_text), raw_element),
                     "unable to find element by link text using #{link_text.inspect}"
  end
end

#findElementByName(parent, name) ⇒ Object



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

def findElementByName(parent, name)
  create_element do |raw_element|
    check_error_code Lib.wdFindElementByName(@driver_pointer, parent, wstring_ptr(name), raw_element),
                     "unable to find element by name using #{name.inspect}"
  end
end

#findElementByPartialLinkText(parent, link_text) ⇒ Object



275
276
277
278
279
280
# File 'lib/selenium/webdriver/ie/bridge.rb', line 275

def findElementByPartialLinkText(parent, link_text)
  create_element do |raw_element|
    check_error_code Lib.wdFindElementByPartialLinkText(@driver_pointer, parent, wstring_ptr(link_text), raw_element),
                     "unable to find element by partial link text using #{link_text.inspect}"
  end
end

#findElementByTagName(parent, tag_name) ⇒ Object



303
304
305
306
307
308
# File 'lib/selenium/webdriver/ie/bridge.rb', line 303

def findElementByTagName(parent, tag_name)
  create_element do |raw_element|
    check_error_code Lib.wdFindElementByTagName(@driver_pointer, parent, wstring_ptr(tag_name), raw_element),
                     "unable to find element by tag name using #{tag_name.inspect}"
  end
end

#findElementByXpath(parent, xpath) ⇒ Object



317
318
319
320
321
322
323
# File 'lib/selenium/webdriver/ie/bridge.rb', line 317

def findElementByXpath(parent, xpath)
  create_element do |raw_element|
    check_error_code Lib.wdFindElementByXPath(@driver_pointer, parent, wstring_ptr(xpath), raw_element),
                     "unable to find element by xpath using #{xpath.inspect}"
    # TODO: Additional error handling
  end
end

#findElementsByClassName(parent, class_name) ⇒ Object



226
227
228
229
230
231
# File 'lib/selenium/webdriver/ie/bridge.rb', line 226

def findElementsByClassName(parent, class_name)
  create_element_collection do |raw_elements|
    check_error_code Lib.wdFindElementsByClassName(@driver_pointer, parent, wstring_ptr(class_name), raw_elements),
                     "unable to find elements by class name using #{class_name.inspect}"
  end
end

#findElementsByCssSelector(parent, selector) ⇒ Object



240
241
242
243
244
245
# File 'lib/selenium/webdriver/ie/bridge.rb', line 240

def findElementsByCssSelector(parent, selector)
  create_element_collection do |raw_elements|
    check_error_code Lib.wdFindElementsByCss(@driver_pointer, parent, wstring_ptr(selector), raw_elements),
                     "unable to find elements by css selector using #{selector.inspect}"
  end
end

#findElementsById(parent, id) ⇒ Object



254
255
256
257
258
259
# File 'lib/selenium/webdriver/ie/bridge.rb', line 254

def findElementsById(parent, id)
  create_element_collection do |raw_elements|
    check_error_code Lib.wdFindElementsById(@driver_pointer, parent, wstring_ptr(id), raw_elements),
                     "unable to find elements by id using #{id.inspect}"
  end
end

#findElementsByLinkText(parent, link_text) ⇒ Object



268
269
270
271
272
273
# File 'lib/selenium/webdriver/ie/bridge.rb', line 268

def findElementsByLinkText(parent, link_text)
  create_element_collection do |raw_elements|
    check_error_code Lib.wdFindElementsByLinkText(@driver_pointer, parent, wstring_ptr(link_text), raw_elements),
                     "unable to find elements by link text using #{link_text.inspect}"
  end
end

#findElementsByName(parent, name) ⇒ Object



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

def findElementsByName(parent, name)
  create_element_collection do |raw_elements|
    check_error_code Lib.wdFindElementsByName(@driver_pointer, parent, wstring_ptr(name), raw_elements),
                     "unable to find elements by name using #{name.inspect}"
  end
end

#findElementsByPartialLinkText(parent, link_text) ⇒ Object



282
283
284
285
286
287
# File 'lib/selenium/webdriver/ie/bridge.rb', line 282

def findElementsByPartialLinkText(parent, link_text)
  create_element_collection do |raw_elements|
    check_error_code Lib.wdFindElementsByPartialLinkText(@driver_pointer, parent, wstring_ptr(link_text), raw_elements),
                     "unable to find elements by partial link text using #{link_text.inspect}"
  end
end

#findElementsByTagName(parent, tag_name) ⇒ Object



310
311
312
313
314
315
# File 'lib/selenium/webdriver/ie/bridge.rb', line 310

def findElementsByTagName(parent, tag_name)
  create_element_collection do |raw_elements|
    check_error_code Lib.wdFindElementsByTagName(@driver_pointer, parent, wstring_ptr(tag_name), raw_elements),
                     "unable to find elements by tag name using #{tag_name.inspect}"
  end
end

#findElementsByXpath(parent, xpath) ⇒ Object



325
326
327
328
329
330
331
# File 'lib/selenium/webdriver/ie/bridge.rb', line 325

def findElementsByXpath(parent, xpath)
  create_element_collection do |raw_elements|
    check_error_code Lib.wdFindElementsByXPath(@driver_pointer, parent, wstring_ptr(xpath), raw_elements),
                     "unable to find elements by xpath using #{xpath.inspect}"
    # TODO: Additional error handling
  end
end

#get(url) ⇒ Object



30
31
32
33
# File 'lib/selenium/webdriver/ie/bridge.rb', line 30

def get(url)
  check_error_code Lib.wdGet(@driver_pointer, wstring_ptr(url)),
                   "Cannot get url #{url.inspect}"
end

#getAllCookiesObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/selenium/webdriver/ie/bridge.rb', line 179

def getAllCookies
  str = create_string do |wrapper|
    check_error_code Lib.wdGetCookies(@driver_pointer, wrapper), "unable to get cookies"
  end

  str.split("; ").map do |cookie_string|
    parts = cookie_string.split("=")
    next unless parts.size == 2

    {
      'name'    => parts[0],
      'value'   => parts[1],
      'domain'  => getCurrentUrl,
      'path'    => "",
      'expires' => nil,
      'secure'  => false
    }
  end.compact
end

#getBrowserVisibleObject



66
67
68
69
70
71
# File 'lib/selenium/webdriver/ie/bridge.rb', line 66

def getBrowserVisible
  int_ptr = FFI::MemoryPointer.new :int
  check_error_code Lib.wdGetVisible(@driver_pointer, int_ptr), "unable to determine if browser is visible"

  int_ptr.get_int(0) == 1
end

#getCurrentUrlObject



35
36
37
38
39
40
# File 'lib/selenium/webdriver/ie/bridge.rb', line 35

def getCurrentUrl
  create_string do |wrapper|
    check_error_code Lib.wdGetCurrentUrl(@driver_pointer, wrapper),
                     "unable to get current URL"
  end
end

#getCurrentWindowHandleObject



135
136
137
138
139
140
# File 'lib/selenium/webdriver/ie/bridge.rb', line 135

def getCurrentWindowHandle
  create_string do |string_pointer|
    check_error_code Lib.wdGetCurrentWindowHandle(@driver_pointer, string_pointer),
                     "unable to obtain current window handle"
  end
end

#getElementAttribute(element_pointer, name) ⇒ Object



349
350
351
352
353
354
# File 'lib/selenium/webdriver/ie/bridge.rb', line 349

def getElementAttribute(element_pointer, name)
  create_string do |string_pointer|
    check_error_code Lib.wdeGetAttribute(@driver_pointer, element_pointer, wstring_ptr(name), string_pointer),
                     "unable to get attribute #{name.inspect}"
  end
end

#getElementLocation(element_pointer) ⇒ Object



458
459
460
461
462
463
464
465
# File 'lib/selenium/webdriver/ie/bridge.rb', line 458

def getElementLocation(element_pointer)
  x = FFI::MemoryPointer.new :long
  y = FFI::MemoryPointer.new :long

  check_error_code Lib.wdeGetLocation(element_pointer, x, y), "unable to get location of element"

  Point.new x.get_int(0), y.get_int(0)
end

#getElementSize(element_pointer) ⇒ Object



467
468
469
470
471
472
473
474
# File 'lib/selenium/webdriver/ie/bridge.rb', line 467

def getElementSize(element_pointer)
  width  = FFI::MemoryPointer.new :long
  height = FFI::MemoryPointer.new :long

  check_error_code Lib.wdeGetSize(element_pointer, width, height), "unable to get size of element"

  Dimension.new width.get_int(0), height.get_int(0)
end

#getElementTagName(element_pointer) ⇒ Object



342
343
344
345
346
347
# File 'lib/selenium/webdriver/ie/bridge.rb', line 342

def getElementTagName(element_pointer)
  create_string do |string_pointer|
    check_error_code Lib.wdeGetTagName(element_pointer, string_pointer),
                     "unable to get tag name"
  end
end

#getElementText(element_pointer) ⇒ Object



361
362
363
364
365
366
367
368
# File 'lib/selenium/webdriver/ie/bridge.rb', line 361

def getElementText(element_pointer)
  val = create_string do |string_pointer|
    check_error_code Lib.wdeGetText(element_pointer, string_pointer),
                     "unable to get text"
  end

  val.gsub("\r\n", "\n") if val
end

#getElementValue(element_pointer) ⇒ Object



356
357
358
359
# File 'lib/selenium/webdriver/ie/bridge.rb', line 356

def getElementValue(element_pointer)
  val = getElementAttribute(element_pointer, 'value')
  val.gsub("\r\n", "\n") if val
end

#getElementValueOfCssProperty(element_pointer, prop) ⇒ Object



425
426
427
428
429
430
# File 'lib/selenium/webdriver/ie/bridge.rb', line 425

def getElementValueOfCssProperty(element_pointer, prop)
  create_string do |string_pointer|
    check_error_code Lib.wdeGetValueOfCssProperty(element_pointer, wstring_ptr(prop), string_pointer),
                     "unable to get value of css property: #{prop.inspect}"
  end
end

#getPageSourceObject



59
60
61
62
63
64
# File 'lib/selenium/webdriver/ie/bridge.rb', line 59

def getPageSource
  create_string do |wrapper|
    check_error_code Lib.wdGetPageSource(@driver_pointer, wrapper),
                     "unable to get page source"
  end
end

#getSpeedObject



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

def getSpeed
  @speed
end

#getTitleObject



52
53
54
55
56
57
# File 'lib/selenium/webdriver/ie/bridge.rb', line 52

def getTitle
  create_string do |wrapper|
    check_error_code Lib.wdGetTitle(@driver_pointer, wrapper),
                     "unable to get title"
  end
end

#getWindowHandlesObject



127
128
129
130
131
132
133
# File 'lib/selenium/webdriver/ie/bridge.rb', line 127

def getWindowHandles
  raw_handles = FFI::MemoryPointer.new :pointer
  check_error_code Lib.wdGetAllWindowHandles(@driver_pointer, raw_handles),
                   "unable to obtain all window handles"

  string_array_from(raw_handles).uniq
end

#goBackObject



42
43
44
45
# File 'lib/selenium/webdriver/ie/bridge.rb', line 42

def goBack
  check_error_code Lib.wdGoBack(@driver_pointer),
                   "Cannot navigate back"
end

#goForwardObject



47
48
49
50
# File 'lib/selenium/webdriver/ie/bridge.rb', line 47

def goForward
  check_error_code Lib.wdGoForward(@driver_pointer),
                   "Cannot navigate back"
end

#hoverOverElement(element_pointer) ⇒ Object

Raises:

  • (NotImplementedError)


432
433
434
# File 'lib/selenium/webdriver/ie/bridge.rb', line 432

def hoverOverElement(element_pointer)
  raise NotImplementedError
end

#isElementDisplayed(element_pointer) ⇒ Object



396
397
398
399
400
401
# File 'lib/selenium/webdriver/ie/bridge.rb', line 396

def isElementDisplayed(element_pointer)
  int_ptr = FFI::MemoryPointer.new :int
  check_error_code Lib.wdeIsDisplayed(element_pointer, int_ptr), "unable to check visibilty"

  int_ptr.get_int(0) == 1;
end

#isElementEnabled(element_pointer) ⇒ Object



380
381
382
383
384
385
386
# File 'lib/selenium/webdriver/ie/bridge.rb', line 380

def isElementEnabled(element_pointer)
  int_ptr = FFI::MemoryPointer.new(:int)
  check_error_code Lib.wdeIsEnabled(element_pointer, int_ptr),
                   "unable to get enabled state"

  int_ptr.get_int(0) == 1
end

#isElementSelected(element_pointer) ⇒ Object



388
389
390
391
392
393
394
# File 'lib/selenium/webdriver/ie/bridge.rb', line 388

def isElementSelected(element_pointer)
  int_ptr = FFI::MemoryPointer.new(:int)
  check_error_code Lib.wdeIsSelected(element_pointer, int_ptr),
                   "unable to get selected state"

  int_ptr.get_int(0) == 1
end

#quitObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/selenium/webdriver/ie/bridge.rb', line 104

def quit
  getWindowHandles.each do |handle|
    begin
      switchToWindow handle
      close
    rescue Error::NoSuchWindowError
    end
  end
ensure
  ptr = @driver_pointer
  @driver_pointer = nil

  check_error_code Lib.wdFreeDriver(ptr), "unable to free driver #{self}"
end

#refreshObject



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

def refresh
  check_error_code Lib.wdRefresh(@driver_pointer), "unable to refresh current page"
end

#sendKeysToElement(element_pointer, string) ⇒ Object



370
371
372
373
374
# File 'lib/selenium/webdriver/ie/bridge.rb', line 370

def sendKeysToElement(element_pointer, string)
  check_error_code Lib.wdeSendKeys(element_pointer, wstring_ptr(string)),
                   "unable to send keys to #{self}"
  waitForLoadToComplete
end

#setBrowserVisible(bool) ⇒ Object



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

def setBrowserVisible(bool)
  check_error_code Lib.wdSetVisible(@driver_pointer, bool ? 1 : 0),
                   "unable to change the visibility of the browser"
end

#setElementSelected(element_pointer) ⇒ Object



421
422
423
# File 'lib/selenium/webdriver/ie/bridge.rb', line 421

def setElementSelected(element_pointer)
  check_error_code Lib.wdeSetSelected(element_pointer), "unable to select element"
end

#setImplicitWaitTimeout(milliseconds) ⇒ Object



78
79
80
81
# File 'lib/selenium/webdriver/ie/bridge.rb', line 78

def setImplicitWaitTimeout(milliseconds)
  check_error_code Lib.wdSetImplicitWaitTimeout(@driver_pointer, milliseconds),
                   "unable to set implicit wait timeout"
end

#setSpeed(speed) ⇒ Object



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

def setSpeed(speed)
  @speed = speed
end

#submitElement(element_pointer) ⇒ Object



403
404
405
# File 'lib/selenium/webdriver/ie/bridge.rb', line 403

def submitElement(element_pointer)
  check_error_code Lib.wdeSubmit(element_pointer), "unable to submit element"
end

#switchToActiveElementObject



93
94
95
96
97
98
# File 'lib/selenium/webdriver/ie/bridge.rb', line 93

def switchToActiveElement
  create_element do |ptr|
    check_error_code Lib.wdSwitchToActiveElement(@driver_pointer, ptr),
                     "unable to switch to active element"
  end
end

#switchToDefaultContentObject



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

def switchToDefaultContent
  switchToFrame ""
end

#switchToFrame(id) ⇒ Object



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

def switchToFrame(id)
  check_error_code Lib.wdSwitchToFrame(@driver_pointer, wstring_ptr(id)),
                   "unable to locate frame #{id.inspect}"
end

#switchToWindow(id) ⇒ Object



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

def switchToWindow(id)
  check_error_code Lib.wdSwitchToWindow(@driver_pointer, wstring_ptr(id)),
                   "unable to locate window #{id.inspect}"
end

#toggleElement(element_pointer) ⇒ Object



407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/selenium/webdriver/ie/bridge.rb', line 407

def toggleElement(element_pointer)
  int_ptr = FFI::MemoryPointer.new :int
  result = Lib.wdeToggle(element_pointer, int_ptr)

  if result == 9
    raise WebDriver::UnsupportedOperationError,
      "You may not toggle this element: #{get_element_tag_name(element_pointer)}"
  end

  check_error_code result, "unable to toggle element"

  int_ptr.get_int(0) == 1
end

#waitForLoadToCompleteObject



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

def waitForLoadToComplete
  Lib.wdWaitForLoadToComplete(@driver_pointer)
end