Inheritance: Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid
        /// <summary>
        /// Executes JavaScript in the context of the currently selected frame or window.
        /// </summary>
        /// <param name="script">The JavaScript code to execute.</param>
        /// <param name="args">The arguments to the script.</param>
        /// <returns>The value returned by the script.</returns>
        /// <remarks>
        /// <para>
        /// The <see cref="ExecuteScript"/>method executes JavaScript in the context of
        /// the currently selected frame or window. This means that "document" will refer
        /// to the current document. If the script has a return value, then the following
        /// steps will be taken:
        /// </para>
        /// <para>
        /// <list type="bullet">
        /// <item><description>For an HTML element, this method returns a <see cref="IWebElement"/></description></item>
        /// <item><description>For a number, a <see cref="System.Int64"/> is returned</description></item>
        /// <item><description>For a boolean, a <see cref="System.Boolean"/> is returned</description></item>
        /// <item><description>For all other cases a <see cref="System.String"/> is returned.</description></item>
        /// <item><description>For an array,we check the first element, and attempt to return a
        /// <see cref="List{T}"/> of that type, following the rules above. Nested lists are not
        /// supported.</description></item>
        /// <item><description>If the value is null or there is no return value,
        /// <see langword="null"/> is returned.</description></item>
        /// </list>
        /// </para>
        /// <para>
        /// Arguments must be a number (which will be converted to a <see cref="System.Int64"/>),
        /// a <see cref="System.Boolean"/>, a <see cref="System.String"/> or a <see cref="IWebElement"/>.
        /// An exception will be thrown if the arguments do not meet these criteria.
        /// The arguments will be made available to the JavaScript via the "arguments" magic
        /// variable, as if the function were called via "Function.apply"
        /// </para>
        /// </remarks>
        public object ExecuteScript(string script, params object[] args)
        {
            object toReturn = null;
            SafeScriptArgsHandle scriptArgsHandle = new SafeScriptArgsHandle();
            WebDriverResult      result           = NativeDriverLibrary.Instance.NewScriptArgs(ref scriptArgsHandle, args.Length);

            ResultHandler.VerifyResultCode(result, "Unable to create new script arguments array");

            try
            {
                PopulateArguments(scriptArgsHandle, args);

                script = "(function() { return function(){" + script + "};})();";

                SafeScriptResultHandle scriptResultHandle = new SafeScriptResultHandle();
                result = NativeDriverLibrary.Instance.ExecuteScript(handle, script, scriptArgsHandle, ref scriptResultHandle);

                ResultHandler.VerifyResultCode(result, "Cannot execute script");

                // Note that ExtractReturnValue frees the memory for the script result.
                toReturn = ExtractReturnValue(scriptResultHandle);
            }
            finally
            {
                scriptArgsHandle.Dispose();
            }

            return(toReturn);
        }
示例#2
0
        /// <summary>
        /// Add to the script args.
        /// </summary>
        /// <param name="scriptArgs">Arguments to be added.</param>
        /// <returns>A Driver result from adding it.</returns>
        internal WebDriverResult AddToScriptArgs(SafeScriptArgsHandle scriptArgs)
        {
            WebDriverResult result = NativeDriverLibrary.Instance.AddElementScriptArg(scriptArgs, elementHandle);

            ResultHandler.VerifyResultCode(result, "adding to script arguments");
            return(result);
        }
        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);
        }
        /// <summary>
        /// Executes JavaScript in the context of the currently selected frame or window.
        /// </summary>
        /// <param name="script">The JavaScript code to execute.</param>
        /// <param name="args">The arguments to the script.</param>
        /// <returns>The value returned by the script.</returns>
        /// <remarks>
        /// <para>
        /// The <see cref="ExecuteScript"/>method executes JavaScript in the context of 
        /// the currently selected frame or window. This means that "document" will refer 
        /// to the current document. If the script has a return value, then the following 
        /// steps will be taken:
        /// </para>
        /// <para>
        /// <list type="bullet">
        /// <item><description>For an HTML element, this method returns a <see cref="IWebElement"/></description></item>
        /// <item><description>For a number, a <see cref="System.Int64"/> is returned</description></item>
        /// <item><description>For a boolean, a <see cref="System.Boolean"/> is returned</description></item>
        /// <item><description>For all other cases a <see cref="System.String"/> is returned.</description></item>
        /// <item><description>For an array,we check the first element, and attempt to return a 
        /// <see cref="List{T}"/> of that type, following the rules above. Nested lists are not
        /// supported.</description></item>
        /// <item><description>If the value is null or there is no return value,
        /// <see langword="null"/> is returned.</description></item>
        /// </list>
        /// </para>
        /// <para>
        /// Arguments must be a number (which will be converted to a <see cref="System.Int64"/>),
        /// a <see cref="System.Boolean"/>, a <see cref="System.String"/> or a <see cref="IWebElement"/>.
        /// An exception will be thrown if the arguments do not meet these criteria. 
        /// The arguments will be made available to the JavaScript via the "arguments" magic 
        /// variable, as if the function were called via "Function.apply" 
        /// </para>
        /// </remarks>
        public object ExecuteScript(string script, params object[] args)
        {
            object toReturn = null;
            SafeScriptArgsHandle scriptArgsHandle = new SafeScriptArgsHandle();
            WebDriverResult result = NativeDriverLibrary.Instance.NewScriptArgs(ref scriptArgsHandle, args.Length);
            ResultHandler.VerifyResultCode(result, "Unable to create new script arguments array");

            try
            {
                PopulateArguments(scriptArgsHandle, args);

                script = "(function() { return function(){" + script + "};})();";

                SafeScriptResultHandle scriptResultHandle = new SafeScriptResultHandle();
                result = NativeDriverLibrary.Instance.ExecuteScript(handle, script, scriptArgsHandle, ref scriptResultHandle);

                ResultHandler.VerifyResultCode(result, "Cannot execute script");

                // Note that ExtractReturnValue frees the memory for the script result.
                toReturn = ExtractReturnValue(scriptResultHandle);
            }
            finally
            {
                scriptArgsHandle.Dispose();
            }

            return toReturn;
        }
示例#5
0
 internal static extern WebDriverResult wdAddStringScriptArg(SafeScriptArgsHandle scriptArgs, string arg);
示例#6
0
 internal static extern WebDriverResult wdNewScriptArgs(ref SafeScriptArgsHandle scriptArgs, int maxLength);
示例#7
0
 internal static extern WebDriverResult wdAddNumberScriptArg(SafeScriptArgsHandle scriptArgs, long param);
示例#8
0
 internal static extern WebDriverResult wdAddElementScriptArg(SafeScriptArgsHandle scriptArgs, SafeInternetExplorerWebElementHandle handle);
示例#9
0
 internal static extern WebDriverResult wdAddDoubleScriptArg(SafeScriptArgsHandle scriptArgs, double param);
示例#10
0
 internal static extern WebDriverResult wdExecuteScript(SafeInternetExplorerDriverHandle driver, string script, SafeScriptArgsHandle scriptArgs, ref SafeScriptResultHandle scriptRes);
示例#11
0
 /// <summary>
 /// Executes arbitrary JavaScript on the page.
 /// </summary>
 /// <param name="driverHandle">A handle to the instance of the <see cref="InternetExplorerDriver"/> class.</param>
 /// <param name="script">The script to run.</param>
 /// <param name="scriptArgsHandle">A handle to the instance of the script arguments.</param>
 /// <param name="scriptResultHandle">A handle to the result of the script.</param>
 /// <returns>A <see cref="WebDriverResult"/> value indicating success or failure.</returns>
 internal WebDriverResult ExecuteScript(SafeInternetExplorerDriverHandle driverHandle, string script, SafeScriptArgsHandle scriptArgsHandle, ref SafeScriptResultHandle scriptResultHandle)
 {
     IntPtr functionPointer = NativeMethods.GetProcAddress(nativeLibraryHandle, ExecuteScriptFunctionName);
     ExecuteScriptFunction executeFunction = Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(ExecuteScriptFunction)) as ExecuteScriptFunction;
     WebDriverResult result = executeFunction(driverHandle, script, scriptArgsHandle, ref scriptResultHandle);
     return result;
 }
示例#12
0
 /// <summary>
 /// Adds a string argument to the set of script arguments.
 /// </summary>
 /// <param name="scriptArgsHandle">A handle to the instance of the script arguments.</param>
 /// <param name="argument">The argument to add.</param>
 /// <returns>A <see cref="WebDriverResult"/> value indicating success or failure.</returns>
 internal WebDriverResult AddStringScriptArg(SafeScriptArgsHandle scriptArgsHandle, string argument)
 {
     IntPtr functionPointer = NativeMethods.GetProcAddress(nativeLibraryHandle, AddStringScriptArgFunctionName);
     StringParameterScriptArgsFunction addScriptArgFunction = Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(StringParameterScriptArgsFunction)) as StringParameterScriptArgsFunction;
     WebDriverResult result = addScriptArgFunction(scriptArgsHandle, argument);
     return result;
 }
示例#13
0
 /// <summary>
 /// Adds an element argument to the set of script arguments.
 /// </summary>
 /// <param name="scriptArgsHandle">A handle to the instance of the script arguments.</param>
 /// <param name="argument">The argument to add.</param>
 /// <returns>A <see cref="WebDriverResult"/> value indicating success or failure.</returns>
 internal WebDriverResult AddElementScriptArg(SafeScriptArgsHandle scriptArgsHandle, SafeInternetExplorerWebElementHandle argument)
 {
     IntPtr functionPointer = NativeMethods.GetProcAddress(nativeLibraryHandle, AddElementScriptArgFunctionName);
     ElementParameterScriptArgsFunction addScriptArgFunction = Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(ElementParameterScriptArgsFunction)) as ElementParameterScriptArgsFunction;
     WebDriverResult result = addScriptArgFunction(scriptArgsHandle, argument);
     return result;
 }
示例#14
0
 /// <summary>
 /// Creates a new set of script arguments.
 /// </summary>
 /// <param name="scriptArgsHandle">A handle to the instance of the script arguments.</param>
 /// <param name="maxLength">The maximum number of arguments to allocate.</param>
 /// <returns>A <see cref="WebDriverResult"/> value indicating success or failure.</returns>
 internal WebDriverResult NewScriptArgs(ref SafeScriptArgsHandle scriptArgsHandle, int maxLength)
 {
     IntPtr functionPointer = NativeMethods.GetProcAddress(nativeLibraryHandle, NewScriptArgsFunctionName);
     ScriptArgsReturningFunction newScriptArgsFunction = Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(ScriptArgsReturningFunction)) as ScriptArgsReturningFunction;
     WebDriverResult result = newScriptArgsFunction(ref scriptArgsHandle, maxLength);
     return result;
 }
示例#15
0
        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;
        }
示例#16
0
 /// <summary>
 /// Add to the script args.
 /// </summary>
 /// <param name="scriptArgs">Arguments to be added.</param>
 /// <returns>A Driver result from adding it.</returns>
 internal WebDriverResult AddToScriptArgs(SafeScriptArgsHandle scriptArgs)
 {
     WebDriverResult result = NativeDriverLibrary.Instance.AddElementScriptArg(scriptArgs, elementHandle);
     ResultHandler.VerifyResultCode(result, "adding to script arguments");
     return result;
 }
示例#17
0
 internal static extern WebDriverResult wdAddBooleanScriptArg(SafeScriptArgsHandle scriptArgs, int boolean);