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

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

Constant Summary collapse

CP_ACP =
0
CP_OEMCP =
1
CP_MACCP =
2
CP_THREAD_ACP =
3
CP_SYMBOL =
42
CP_UTF7 =
65000
CP_UTF8 =
65001

Instance Method Summary collapse

Instance Method Details

#create_element(&blk) ⇒ Object



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

def create_element(&blk)
  element_ptr_ref = FFI::MemoryPointer.new :pointer
  yield element_ptr_ref
  Element.new(self, element_ptr_ref.get_pointer(0))
ensure
  element_ptr_ref.free
end

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

Yields:

  • (elements_ptr_ref)


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

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)


61
62
63
64
65
66
# File 'lib/selenium/webdriver/ie/util.rb', line 61

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

  extract_string_from wrapper
end

#extract_elements_from(elements_ptr_ref) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/selenium/webdriver/ie/util.rb', line 91

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
ensure
  elements_ptr_ref.free
  length_ptr.free
end

#extract_string_from(string_ptr_ref) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/selenium/webdriver/ie/util.rb', line 68

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?
  string_ptr_ref.free
end

#string_array_from(raw_strings) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/selenium/webdriver/ie/util.rb', line 18

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)
  raw_strings.free
end

#wstring_ptr(str) ⇒ Object



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

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/selenium/webdriver/ie/util.rb', line 134

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)
ensure
  buf.free if buf
end