private object ExecuteScriptInternal(string script, bool async, params object[] args) { if (!this.Capabilities.IsJavaScriptEnabled) { throw new NotSupportedException("You must be using an underlying instance of WebDriver that supports executing javascript"); } // Escape the quote marks // script = script.Replace("\"", "\\\""); object[] convertedArgs = ConvertArgumentsToJavaScriptObjects(args); Dictionary <string, object> parameters = new Dictionary <string, object>(); parameters.Add("script", script); if (convertedArgs != null && convertedArgs.Length > 0) { parameters.Add("args", convertedArgs); } else { parameters.Add("args", new object[] { }); } DriverCommand command = async ? DriverCommand.ExecuteAsyncScript : DriverCommand.ExecuteScript; Response commandResponse = this.Execute(command, parameters); return(this.ParseJavaScriptReturnValue(commandResponse.Value)); }
/// <summary> /// Initializes a new instance of the Command class for a Session /// </summary> /// <param name="sessionId">Session ID the driver is using</param> /// <param name="name">Name of the command</param> /// <param name="parameters">Parameters for that command</param> public Command(SessionId sessionId, DriverCommand name, Dictionary<string, object> parameters) { commandSessionId = sessionId; if (parameters != null) { commandParameters = parameters; } commandName = name; }
/// <summary> /// Gets the <see cref="CommandInfo"/> for a <see cref="DriverCommand"/>. /// </summary> /// <param name="commandName">The <see cref="DriverCommand"/> for which to get the information.</param> /// <returns>The <see cref="CommandInfo"/> for the specified command.</returns> public CommandInfo GetCommandInfo(DriverCommand commandName) { CommandInfo toReturn = null; if (commandDictionary.ContainsKey(commandName)) { toReturn = commandDictionary[commandName]; } return toReturn; }
/// <summary> /// Initializes a new instance of the Command class for a Session /// </summary> /// <param name="sessionId">Session ID the driver is using</param> /// <param name="name">Name of the command</param> /// <param name="parameters">Parameters for that command</param> public Command(SessionId sessionId, DriverCommand name, Dictionary <string, object> parameters) { this.commandSessionId = sessionId; if (parameters != null) { this.commandParameters = parameters; } this.commandName = name; }
/// <summary> /// Gets the <see cref="CommandInfo"/> for a <see cref="DriverCommand"/>. /// </summary> /// <param name="commandName">The <see cref="DriverCommand"/> for which to get the information.</param> /// <returns>The <see cref="CommandInfo"/> for the specified command.</returns> public CommandInfo GetCommandInfo(DriverCommand commandName) { CommandInfo toReturn = null; if (this.commandDictionary.ContainsKey(commandName)) { toReturn = this.commandDictionary[commandName]; } return(toReturn); }
/// <summary> /// Executes a command with this driver . /// </summary> /// <param name="driverCommandToExecute">A <see cref="DriverCommand"/> value representing the command to execute.</param> /// <param name="parameters">A <see cref="Dictionary{K, V}"/> containing the names and values of the parameters of the command.</param> /// <returns>A <see cref="Response"/> containing information about the success or failure of the command and any data returned by the command.</returns> protected virtual Response Execute(DriverCommand driverCommandToExecute, Dictionary <string, object> parameters) { Command commandToExecute = new Command(this.sessionId, driverCommandToExecute, parameters); Response commandResponse = new Response(); try { commandResponse = this.executor.Execute(commandToExecute); } catch (System.Net.WebException e) { commandResponse.Status = WebDriverResult.UnhandledError; commandResponse.Value = e; } if (commandResponse.Status != WebDriverResult.Success) { UnpackAndThrowOnError(commandResponse); } return(commandResponse); }
/// <summary> /// Executes a command with this driver . /// </summary> /// <param name="driverCommandToExecute">A <see cref="DriverCommand"/> value representing the command to execute.</param> /// <param name="parameters">A <see cref="Dictionary{K, V}"/> containing the names and values of the parameters of the command.</param> /// <returns>A <see cref="Response"/> containing information about the success or failure of the command and any data returned by the command.</returns> protected virtual Response Execute(DriverCommand driverCommandToExecute, Dictionary<string, object> parameters) { Command commandToExecute = new Command(this.sessionId, driverCommandToExecute, parameters); Response commandResponse = new Response(); try { commandResponse = this.executor.Execute(commandToExecute); } catch (System.Net.WebException e) { commandResponse.Status = WebDriverResult.UnhandledError; commandResponse.Value = e; } if (commandResponse.Status != WebDriverResult.Success) { UnpackAndThrowOnError(commandResponse); } return commandResponse; }
/// <summary> /// Executes commands with the driver /// </summary> /// <param name="driverCommandToExecute">Command that needs executing</param> /// <param name="parameters">Parameters needed for the command</param> /// <returns>WebDriver Response</returns> internal Response InternalExecute(DriverCommand driverCommandToExecute, Dictionary<string, object> parameters) { return this.Execute(driverCommandToExecute, parameters); }
/// <summary> /// Initializes a new instance of the Command class using a command name and a JSON-encoded string for the parameters. /// </summary> /// <param name="name">Name of the command</param> /// <param name="jsonParameters">Parameters for the command as a JSON-encoded string.</param> public Command(DriverCommand name, string jsonParameters) : this(null, name, ConvertParametersFromJson(jsonParameters)) { }
/// <summary> /// Executes a command on this element using the specified parameters. /// </summary> /// <param name="commandToExecute">The <see cref="DriverCommand"/> to execute against this element.</param> /// <param name="parameters">A <see cref="Dictionary{K, V}"/> containing names and values of the parameters for the command.</param> /// <returns>The <see cref="Response"/> object containing the result of the command execution.</returns> protected Response Execute(DriverCommand commandToExecute, Dictionary <string, object> parameters) { return(this.driver.InternalExecute(commandToExecute, parameters)); }
/// <summary> /// Executes a passed command using the current ChromeCommandExecutor /// </summary> /// <param name="driverCommand">command to execute</param> /// <param name="parameters">parameters of command being executed</param> /// <returns>response to the command (a Response wrapping a null value if none)</returns> public ChromeResponse Execute(DriverCommand driverCommand, params object[] parameters) { ChromeCommand command = new ChromeCommand( new SessionId( "[No sessionId]"), new Context("[No context]"), driverCommand, parameters); ChromeResponse commandResponse = null; try { commandResponse = executor.Execute(command); } catch (NotSupportedException nse) { string message = nse.Message.ToLowerInvariant(); if (message.Contains("cannot toggle a") || message.Contains("cannot unselect a single element select")) { throw new NotImplementedException(); } throw; } catch (ArgumentException) { // Exceptions may leave the extension hung, or in an // inconsistent state, so we restart Chrome StopClient(); StartClient(); } catch (FatalChromeException) { // Exceptions may leave the extension hung, or in an // inconsistent state, so we restart Chrome StopClient(); StartClient(); } return commandResponse; }
/// <summary> /// Executes commands with the driver /// </summary> /// <param name="driverCommandToExecute">Command that needs executing</param> /// <param name="parameters">Parameters needed for the command</param> /// <returns>WebDriver Response</returns> internal Response InternalExecute(DriverCommand driverCommandToExecute, Dictionary <string, object> parameters) { return(this.Execute(driverCommandToExecute, parameters)); }
/// <summary> /// Execute commands on the browser /// </summary> /// <param name="command">Command to be executed</param> /// <param name="arg">Any commands that the command requires</param> /// <returns>A response from Chrome</returns> public ChromeResponse Execute(DriverCommand command, params object[] arg) { return instance.Execute(command, arg); }
/// <summary> /// Executes a command with this driver . /// </summary> /// <param name="driverCommandToExecute">A <see cref="DriverCommand"/> value representing the command to execute.</param> /// <param name="parameters">A <see cref="Dictionary{K, V}"/> containing the names and values of the parameters of the command.</param> /// <returns>A <see cref="Response"/> containing information about the success or failure of the command and any data returned by the command.</returns> protected override Response Execute(DriverCommand driverCommandToExecute, Dictionary<string, object> parameters) { Response commandResponse = null; try { commandResponse = base.Execute(driverCommandToExecute, parameters); } catch (ArgumentException) { // Exceptions may leave the extension hung, or in an // inconsistent state, so we restart Chrome StopClient(); StartClient(); } catch (FatalChromeException) { // Exceptions may leave the extension hung, or in an inconsistent state, // so we restart Chrome. There is also a legitimate success condition // where navigating to about:blank does not cause the extension to // write a response back to the driver. StopClient(); StartClient(); } return commandResponse; }
/// <summary> /// Executes a command on this element using the specified parameters. /// </summary> /// <param name="commandToExecute">The <see cref="DriverCommand"/> to execute against this element.</param> /// <param name="parameters">A <see cref="Dictionary{K, V}"/> containing names and values of the parameters for the command.</param> /// <returns>The <see cref="Response"/> object containing the result of the command execution.</returns> protected Response Execute(DriverCommand commandToExecute, Dictionary<string, object> parameters) { return this.driver.InternalExecute(commandToExecute, parameters); }
/// <summary> /// Executes commands with the driver /// </summary> /// <param name="driverCommandToExecute">Command that needs executing</param> /// <param name="parameters">Parameters needed for the command</param> /// <returns>WebDriver Response</returns> internal Response Execute(DriverCommand driverCommandToExecute, object[] parameters) { Command commandToExecute = new Command(sessionId, new Context("foo"), driverCommandToExecute, parameters); Response commandResponse = new Response(); try { commandResponse = executor.Execute(commandToExecute); AmendElementValueIfNecessary(commandResponse); } catch (System.Net.WebException e) { commandResponse.IsError = true; commandResponse.Value = e; } if (commandResponse.IsError) { UnpackAndThrowOnError(commandResponse.Value); } return commandResponse; }