Module: Selenium::WebDriver::IE::Util

Included in:
Bridge
Defined in:
lib/selenium/webdriver/ie/util.rb

Constant Summary collapse

CP_UTF8 =
65001

Instance Method Summary collapse

Instance Method Details

#create_element {|element_ptr_ref| ... } ⇒ Object

Yields:

  • (element_ptr_ref)


39
40
41
42
43
44
45
# File 'lib/selenium/webdriver/ie/util.rb', line 39

def create_element(&blk)
  element_ptr_ref = FFI::MemoryPointer.new :pointer
  yield element_ptr_ref

  auto_ptr = FFI::AutoPointer.new(element_ptr_ref.get_pointer(0), Lib.method(:finalize_element))
  Element.new(self, auto_ptr)
end

#create_element_collection {|elements_ptr_ref| ... } ⇒ Object

Yields:

  • (elements_ptr_ref)


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

def create_element_collection(&blk)
  elements_ptr_ref = FFI::MemoryPointer.new :pointer
  yield elements_ptr_ref

  extract_elements_from elements_ptr_ref
end

#create_string {|wrapper| ... } ⇒ Object

Yields:

  • (wrapper)


54
55
56
57
58
59
# File 'lib/selenium/webdriver/ie/util.rb', line 54

def create_string(&blk)
  wrapper = FFI::MemoryPointer.new :pointer
  yield wrapper

  extract_string_from wrapper
end

#extract_elements_from(elements_ptr_ref) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/selenium/webdriver/ie/util.rb', line 83

def extract_elements_from(elements_ptr_ref)
  elements_ptr = elements_ptr_ref.get_pointer(0)
  length_ptr   = FFI::MemoryPointer.new :int

  check_error_code Lib.wdcGetElementCollectionLength(elements_ptr, length_ptr),
                   "Cannot extract elements from collection"

  arr = []

  length_ptr.get_int(0).times do |idx|
    arr << create_element do |ptr|
      result = Lib.wdcGetElementAtIndex(elements_ptr, idx, ptr)

      if e = WebDriver::Error.for_code(result)
        Lib.wdFreeElementCollection(elements_ptr, 1)
        raise e, "unable to create element from collection at index #{idx} (#{result})"
      end
    end
  end

  Lib.wdFreeElementCollection(elements_ptr, 0)

  arr
end

#extract_string_from(string_ptr_ref) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/selenium/webdriver/ie/util.rb', line 61

def extract_string_from(string_ptr_ref)
  string_ptr = string_ptr_ref.get_pointer(0)
  return if string_ptr.null? # getElementAttribute()

  length_ptr = FFI::MemoryPointer.new :int

  if Lib.wdStringLength(string_ptr, length_ptr) != 0
    raise Error::WebDriverError, "cannot determine length of string"
  end

  length     = length_ptr.get_int(0)
  raw_string = wstring_ptr(" "*length)

  if Lib.wdCopyString(string_ptr, length, raw_string) != 0
    raise Error::WebDriverError, "cannot copy string from native data to Ruby string"
  end

  wstring_to_bytestring raw_string
ensure
  Lib.wdFreeString(string_ptr) unless string_ptr.null?
end

#string_array_from(raw_strings) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/selenium/webdriver/ie/util.rb', line 12

def string_array_from(raw_strings)
  strings_ptr = raw_strings.get_pointer(0)
  length_ptr  = FFI::MemoryPointer.new :int
  result      = Lib.wdcGetStringCollectionLength(strings_ptr, length_ptr)

  if result != 0
    raise Error::WebDriverError, "cannot extract strings from collection, error code: #{result.inspect}"
  end

  arr    = []

  length_ptr.get_int(0).times do |idx|
    str_ptr_ref = FFI::MemoryPointer.new :pointer
    result = Lib.wdcGetStringAtIndex(strings_ptr, idx, str_ptr_ref)

    if result != 0
      raise Error::WebDriverError, "unable to get string at index: #{idx}, error: #{result}"
    end

    arr << extract_string_from(str_ptr_ref)
  end

  arr
ensure
  Lib.wdFreeStringCollection(strings_ptr)
end

#wstring_ptr(str) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/selenium/webdriver/ie/util.rb', line 108

def wstring_ptr(str)
  str  = str.to_s
  size = Kernel32.MultiByteToWideChar(CP_UTF8, 0, str, -1, nil, 0)

  unless size > 0
    raise Error::WebDriverError, "unable to convert #{str.inspect} to wchar ptr"
  end

  buf = FFI::MemoryPointer.new :pointer, size
  Kernel32.MultiByteToWideChar(CP_UTF8, 0, str, -1, buf, size)

  buf
end

#wstring_to_bytestring(wstring) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/selenium/webdriver/ie/util.rb', line 122

def wstring_to_bytestring(wstring)
  size = Kernel32.WideCharToMultiByte(CP_UTF8, 0, wstring, -1, nil, 0, nil, nil)

  unless size > 0
    raise Error::WebDriverError, "could not convert wstring pointer to bytestring"
  end

  buf = FFI::MemoryPointer.new :pointer, size
  Kernel32.WideCharToMultiByte(CP_UTF8, 0, wstring, -1, buf, size, nil, nil )

  buf.get_bytes(0, size - 1)
end