private static WebDriverResult PopulateArguments(SafeScriptArgsHandle scriptArgs, object[] args) { WebDriverResult result = WebDriverResult.Success; foreach (object arg in args) { string stringArg = arg as string; InternetExplorerWebElement webElementArg = arg as InternetExplorerWebElement; if (stringArg != null) { result = NativeDriverLibrary.Instance.AddStringScriptArg(scriptArgs, stringArg); } else if (arg is bool) { bool param = (bool)arg; result = NativeDriverLibrary.Instance.AddBooleanScriptArg(scriptArgs, !param ? 0 : 1); } else if (webElementArg != null) { result = webElementArg.AddToScriptArgs(scriptArgs); } else if (arg is int || arg is short || arg is long) { int param; bool parseSucceeded = int.TryParse(arg.ToString(), out param); if (!parseSucceeded) { throw new ArgumentException("Parameter is not recognized as an int: " + arg); } result = NativeDriverLibrary.Instance.AddNumberScriptArg(scriptArgs, param); } else if (arg is float || arg is double) { double param; bool parseSucceeded = double.TryParse(arg.ToString(), out param); if (!parseSucceeded) { throw new ArgumentException("Parameter is not of recognized as a double: " + arg); } result = NativeDriverLibrary.Instance.AddDoubleScriptArg(scriptArgs, param); } else { throw new ArgumentException("Parameter is not of recognized type: " + arg); } ResultHandler.VerifyResultCode(result, "Unable to add argument: " + arg); } return(result); }
private object ExtractReturnValue(SafeScriptResultHandle scriptResult) { WebDriverResult result; int type; result = NativeDriverLibrary.Instance.GetScriptResultType(handle, scriptResult, out type); ResultHandler.VerifyResultCode(result, "Cannot determine result type"); object toReturn = null; try { switch (type) { case 1: SafeStringWrapperHandle stringHandle = new SafeStringWrapperHandle(); result = NativeDriverLibrary.Instance.GetStringScriptResult(scriptResult, ref stringHandle); ResultHandler.VerifyResultCode(result, "Cannot extract string result"); using (StringWrapper wrapper = new StringWrapper(stringHandle)) { toReturn = wrapper.Value; } break; case 2: long longVal; result = NativeDriverLibrary.Instance.GetNumberScriptResult(scriptResult, out longVal); ResultHandler.VerifyResultCode(result, "Cannot extract number result"); toReturn = longVal; break; case 3: int boolVal; result = NativeDriverLibrary.Instance.GetBooleanScriptResult(scriptResult, out boolVal); ResultHandler.VerifyResultCode(result, "Cannot extract boolean result"); toReturn = boolVal == 1 ? true : false; break; case 4: SafeInternetExplorerWebElementHandle element; result = NativeDriverLibrary.Instance.GetElementScriptResult(scriptResult, handle, out element); ResultHandler.VerifyResultCode(result, "Cannot extract element result"); toReturn = new InternetExplorerWebElement(this, element); break; case 5: toReturn = null; break; case 6: SafeStringWrapperHandle messageHandle = new SafeStringWrapperHandle(); result = NativeDriverLibrary.Instance.GetStringScriptResult(scriptResult, ref messageHandle); ResultHandler.VerifyResultCode(result, "Cannot extract string result"); string message = string.Empty; using (StringWrapper wrapper = new StringWrapper(messageHandle)) { message = wrapper.Value; } throw new WebDriverException(message); case 7: double doubleVal; result = NativeDriverLibrary.Instance.GetDoubleScriptResult(scriptResult, out doubleVal); ResultHandler.VerifyResultCode(result, "Cannot extract number result"); toReturn = doubleVal; break; case 8: bool allArrayItemsAreElements = true; int arrayLength = 0; result = NativeDriverLibrary.Instance.GetArrayLengthScriptResult(handle, scriptResult, out arrayLength); ResultHandler.VerifyResultCode(result, "Cannot extract array length."); List<object> list = new List<object>(); for (int i = 0; i < arrayLength; i++) { // Get reference to object SafeScriptResultHandle currItemHandle = new SafeScriptResultHandle(); WebDriverResult getItemResult = NativeDriverLibrary.Instance.GetArrayItemFromScriptResult(handle, scriptResult, i, out currItemHandle); if (getItemResult != WebDriverResult.Success) { // Note about memory management: Usually memory for this item // will be released during the recursive call to // ExtractReturnValue. It is freed explicitly here since a // recursive call will not happen. currItemHandle.Dispose(); throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, "Cannot extract element from collection at index: {0} ({1})", i, result)); } object arrayItem = ExtractReturnValue(currItemHandle); if (allArrayItemsAreElements && !(arrayItem is IWebElement)) { allArrayItemsAreElements = false; } // Call ExtractReturnValue with the fetched item (recursive) list.Add(arrayItem); } if (allArrayItemsAreElements) { List<IWebElement> elementList = new List<IWebElement>(); foreach (object item in list) { elementList.Add((IWebElement)item); } toReturn = elementList.AsReadOnly(); } else { toReturn = list.AsReadOnly(); } break; default: throw new WebDriverException("Cannot determine result type"); } } finally { scriptResult.Dispose(); } return toReturn; }
private object ExtractReturnValue(SafeScriptResultHandle scriptResult) { WebDriverResult result; int type; result = NativeDriverLibrary.Instance.GetScriptResultType(handle, scriptResult, out type); ResultHandler.VerifyResultCode(result, "Cannot determine result type"); object toReturn = null; try { switch (type) { case 1: SafeStringWrapperHandle stringHandle = new SafeStringWrapperHandle(); result = NativeDriverLibrary.Instance.GetStringScriptResult(scriptResult, ref stringHandle); ResultHandler.VerifyResultCode(result, "Cannot extract string result"); using (StringWrapper wrapper = new StringWrapper(stringHandle)) { toReturn = wrapper.Value; } break; case 2: long longVal; result = NativeDriverLibrary.Instance.GetNumberScriptResult(scriptResult, out longVal); ResultHandler.VerifyResultCode(result, "Cannot extract number result"); toReturn = longVal; break; case 3: int boolVal; result = NativeDriverLibrary.Instance.GetBooleanScriptResult(scriptResult, out boolVal); ResultHandler.VerifyResultCode(result, "Cannot extract boolean result"); toReturn = boolVal == 1 ? true : false; break; case 4: SafeInternetExplorerWebElementHandle element; result = NativeDriverLibrary.Instance.GetElementScriptResult(scriptResult, handle, out element); ResultHandler.VerifyResultCode(result, "Cannot extract element result"); toReturn = new InternetExplorerWebElement(this, element); break; case 5: toReturn = null; break; case 6: SafeStringWrapperHandle messageHandle = new SafeStringWrapperHandle(); result = NativeDriverLibrary.Instance.GetStringScriptResult(scriptResult, ref messageHandle); ResultHandler.VerifyResultCode(result, "Cannot extract string result"); string message = string.Empty; using (StringWrapper wrapper = new StringWrapper(messageHandle)) { message = wrapper.Value; } throw new WebDriverException(message); case 7: double doubleVal; result = NativeDriverLibrary.Instance.GetDoubleScriptResult(scriptResult, out doubleVal); ResultHandler.VerifyResultCode(result, "Cannot extract number result"); toReturn = doubleVal; break; case 8: bool allArrayItemsAreElements = true; int arrayLength = 0; result = NativeDriverLibrary.Instance.GetArrayLengthScriptResult(handle, scriptResult, out arrayLength); ResultHandler.VerifyResultCode(result, "Cannot extract array length."); List <object> list = new List <object>(); for (int i = 0; i < arrayLength; i++) { // Get reference to object SafeScriptResultHandle currItemHandle = new SafeScriptResultHandle(); WebDriverResult getItemResult = NativeDriverLibrary.Instance.GetArrayItemFromScriptResult(handle, scriptResult, i, out currItemHandle); if (getItemResult != WebDriverResult.Success) { // Note about memory management: Usually memory for this item // will be released during the recursive call to // ExtractReturnValue. It is freed explicitly here since a // recursive call will not happen. currItemHandle.Dispose(); throw new WebDriverException(string.Format(CultureInfo.InvariantCulture, "Cannot extract element from collection at index: {0} ({1})", i, result)); } object arrayItem = ExtractReturnValue(currItemHandle); if (allArrayItemsAreElements && !(arrayItem is IWebElement)) { allArrayItemsAreElements = false; } // Call ExtractReturnValue with the fetched item (recursive) list.Add(arrayItem); } if (allArrayItemsAreElements) { List <IWebElement> elementList = new List <IWebElement>(); foreach (object item in list) { elementList.Add((IWebElement)item); } toReturn = elementList.AsReadOnly(); } else { toReturn = list.AsReadOnly(); } break; default: throw new WebDriverException("Cannot determine result type"); } } finally { scriptResult.Dispose(); } return(toReturn); }