lib/selenium/webdriver/ie/bridge.rb in selenium-webdriver-0.0.28 vs lib/selenium/webdriver/ie/bridge.rb in selenium-webdriver-0.0.29
- old
+ new
@@ -66,12 +66,10 @@
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
- ensure
- int_ptr.free
end
def setBrowserVisible(bool)
check_error_code Lib.wdSetVisible(@driver_pointer, bool ? 1 : 0),
"unable to change the visibility of the browser"
@@ -109,16 +107,15 @@
switchToWindow handle
close
rescue Error::NoSuchWindowError
end
end
-
- # hack
- ObjectSpace.each_object(WebDriver::Element) { |e| finalize e.ref if e.bridge == self }
ensure
- Lib.wdFreeDriver(@driver_pointer)
+ ptr = @driver_pointer
@driver_pointer = nil
+
+ check_error_code Lib.wdFreeDriver(ptr), "unable to free driver #{self}"
end
def close
check_error_code Lib.wdClose(@driver_pointer), "unable to close driver"
end
@@ -131,11 +128,10 @@
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
- # TODO: who calls raw_handles.free if exception is raised?
end
def getCurrentWindowHandle
create_string do |string_pointer|
check_error_code Lib.wdGetCurrentWindowHandle(@driver_pointer, string_pointer),
@@ -158,12 +154,10 @@
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
- script_args_ref.free
- script_result_ref.free if script_result_ref
Lib.wdFreeScriptArgs(args_pointer) if args_pointer
end
def waitForLoadToComplete
Lib.wdWaitForLoadToComplete(@driver_pointer)
@@ -221,27 +215,37 @@
#
# Finders
#
def findElementByClassName(parent, class_name)
- # TODO: argument checks
-
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
def findElementsByClassName(parent, class_name)
- # TODO: argument checks
-
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
+ 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
+
+ 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
+
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
@@ -377,31 +381,25 @@
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
- ensure
- int_ptr.free
end
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
- ensure
- int_ptr.free
end
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;
- ensure
- int_ptr.free
end
def submitElement(element_pointer)
check_error_code Lib.wdeSubmit(element_pointer), "unable to submit element"
end
@@ -416,12 +414,10 @@
end
check_error_code result, "unable to toggle element"
int_ptr.get_int(0) == 1
- ensure
- int_ptr.free
end
def setElementSelected(element_pointer)
check_error_code Lib.wdeSetSelected(element_pointer), "unable to select element"
end
@@ -455,36 +451,28 @@
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)
- ensure
- [hwnd, x, y, width, height].each { |pointer| pointer.free }
end
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)
- ensure
- x.free
- y.free
end
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)
- ensure
- width.free
- height.free
end
def finalize(element_pointer)
check_error_code Lib.wdeFreeElement(element_pointer),
"unable to finalize #{element_pointer} for #{self}"
@@ -516,14 +504,13 @@
result
end
def extract_return_value(pointer_ref)
result, returned = nil
- pointers_to_free = []
script_result = pointer_ref.get_pointer(0)
- pointers_to_free << type_pointer = FFI::MemoryPointer.new(:int)
+ type_pointer = FFI::MemoryPointer.new(:int)
result = Lib.wdGetScriptResultType(@driver_pointer, script_result, type_pointer)
check_error_code result, "Cannot determine result type"
result_type = type_pointer.get_int(0)
@@ -531,42 +518,43 @@
when 1 # String
create_string do |wrapper|
check_error_code Lib.wdGetStringScriptResult(script_result, wrapper), "Cannot extract string result"
end
when 2 # Long
- pointers_to_free << long_pointer = FFI::MemoryPointer.new(:long)
+ long_pointer = FFI::MemoryPointer.new(:long)
check_error_code Lib.wdGetNumberScriptResult(script_result, long_pointer),
"Cannot extract number result"
long_pointer.get_long(0)
when 3 # Boolean
- pointers_to_free << int_pointer = FFI::MemoryPointer.new(:int)
+ int_pointer = FFI::MemoryPointer.new(:int)
check_error_code Lib.wdGetBooleanScriptResult(script_result, int_pointer),
"Cannot extract boolean result"
int_pointer.get_int(0) == 1
when 4 # WebElement
- pointers_to_free << element_pointer = FFI::MemoryPointer.new(:pointer)
+ element_pointer = FFI::MemoryPointer.new(:pointer)
check_error_code Lib.wdGetElementScriptResult(script_result, @driver_pointer, element_pointer),
"Cannot extract element result"
- Element.new(self, element_pointer.get_pointer(0))
+ auto_ptr = FFI::AutoPointer.new(element_pointer.get_pointer(0), Lib.method(:finalize_element))
+ Element.new(self, auto_ptr)
when 5 # Nothing
nil
when 6 # Exception
message = create_string do |string_pointer|
check_error_code Lib.wdGetStringScriptResult(script_result, string_pointer), "Cannot extract string result"
end
raise Error::WebDriverError, message
when 7 # Double
- pointers_to_free << double_pointer = FFI::MemoryPointer.new(:double)
+ double_pointer = FFI::MemoryPointer.new(:double)
check_error_code Lib.wdGetDoubleScriptResult(script_result, double_pointer), "Cannot extract double result"
double_pointer.get_double(0)
when 8 # Array
- pointers_to_free << array_length_pointer = FFI::MemoryPointer.new(:int)
+ array_length_pointer = FFI::MemoryPointer.new(:int)
check_error_code Lib.wdGetArrayLengthScriptResult(@driver_pointer, script_result, array_length_pointer),
"Cannot extract array length"
arr = []
array_length_pointer.get_int(0).times do |idx|
@@ -586,10 +574,9 @@
else
raise Error::WebDriverError, "Cannot determine result type (#{result_type.inspect})"
end
ensure
Lib.wdFreeScriptResult(script_result) if script_result
- pointers_to_free.each { |p| p.free }
end
def check_error_code(code, message)
return unless ex = WebDriver::Error.for_code(code)
\ No newline at end of file