protected RemoteWebElement CreateRemoteWebElementFromResponse(Response response) { var elementDictionary = response.Value as Dictionary<string, object>; if (elementDictionary == null) { return null; } return new RemoteWebElement((RemoteWebDriver)this.WrappedDriver, (string)elementDictionary["ELEMENT"]); }
private Response WaitForResponse(TimeSpan timeout) { bool timedOut = false; Response response = null; DateTime endTime = DateTime.Now.Add(timeout); while (response == null && !timedOut) { if (this.connectionClosed) { response = new Response(); break; } else { response = this.GetQueuedResponse(); if (response != null) { if (response.Value is string) { // First, collapse all \r\n pairs to \n, then replace all \n with // System.Environment.NewLine. This ensures the consistency of // the values. response.Value = ((string)response.Value).Replace("\r\n", "\n").Replace("\n", System.Environment.NewLine); } break; } } Thread.Sleep(250); if (DateTime.Now > endTime) { timedOut = true; } } if (timedOut) { Dictionary<string, object> valueDictionary = new Dictionary<string, object>(); valueDictionary["message"] = "Timed out waiting for response from WebSocket server"; response = new Response(); response.Status = WebDriverResult.Timeout; response.Value = valueDictionary; } return response; }
internal Response InternalExecute(string driverCommandToExecute, Dictionary<string, object> parameters) { //return this.Execute(driverCommandToExecute, parameters); Response resp = new Response(); if ("getElementTagName" == driverCommandToExecute) { resp.Value = this.TagNameResponse; } if ("getElementText" == driverCommandToExecute) { resp.Value = this.TextResponse; } if ("isElementEnabled" == driverCommandToExecute) { resp.Value = this.EnabledResponse; } if ("isElementSelected" == driverCommandToExecute) { resp.Value = this.SelectedResponse; } if ("isElementDisplayed" == driverCommandToExecute) { resp.Value = this.DisplayedResponse; } // if ("" == driverCommandToExecute) { // // } // if ("" == driverCommandToExecute) { // // } // if ("" == driverCommandToExecute) { // // } // if ("" == driverCommandToExecute) { // // } // if ("" == driverCommandToExecute) { // // } return resp; }
private Response WaitForResponse(TimeSpan timeout) { bool timedOut = false; Response response = null; DateTime endTime = DateTime.Now.Add(timeout); while (response == null && !timedOut) { if (this.connectionClosed) { response = new Response(); break; } else { response = this.GetQueuedResponse(); if (response != null) { break; } } Thread.Sleep(250); if (DateTime.Now > endTime) { timedOut = true; } } if (timedOut) { Dictionary<string, object> valueDictionary = new Dictionary<string, object>(); valueDictionary["message"] = "Timed out waiting for response from WebSocket server"; response = new Response(); response.Status = WebDriverResult.Timeout; response.Value = valueDictionary; } return response; }
/// <summary> /// Find the element in the response /// </summary> /// <param name="response">Response from the browser</param> /// <returns>Element from the page</returns> internal IWebElement GetElementFromResponse(Response response) { if (response == null) { throw new NoSuchElementException(); } RemoteWebElement element = null; Dictionary<string, object> elementDictionary = response.Value as Dictionary<string, object>; if (elementDictionary != null) { // TODO: Remove this "if" logic once the spec is properly updated // and remote-end implementations comply. string id = string.Empty; if (elementDictionary.ContainsKey("element-6066-11e4-a52e-4f735466cecf")) { id = (string)elementDictionary["element-6066-11e4-a52e-4f735466cecf"]; } else if (elementDictionary.ContainsKey("ELEMENT")) { id = (string)elementDictionary["ELEMENT"]; } element = this.CreateElement(id); } return element; }
private static void UnpackAndThrowOnError(Response errorResponse) { // Check the status code of the error, and only handle if not success. if (errorResponse.Status != WebDriverResult.Success) { Dictionary<string, object> errorAsDictionary = errorResponse.Value as Dictionary<string, object>; if (errorAsDictionary != null) { ErrorResponse errorResponseObject = new ErrorResponse(errorAsDictionary); string errorMessage = errorResponseObject.Message; switch (errorResponse.Status) { case WebDriverResult.NoSuchElement: throw new NoSuchElementException(errorMessage); case WebDriverResult.NoSuchFrame: throw new NoSuchFrameException(errorMessage); case WebDriverResult.UnknownCommand: throw new NotImplementedException(errorMessage); case WebDriverResult.ObsoleteElement: throw new StaleElementReferenceException(errorMessage); case WebDriverResult.ElementNotDisplayed: throw new ElementNotVisibleException(errorMessage); case WebDriverResult.InvalidElementState: case WebDriverResult.ElementNotSelectable: throw new InvalidElementStateException(errorMessage); case WebDriverResult.UnhandledError: throw new InvalidOperationException(errorMessage); case WebDriverResult.NoSuchDocument: throw new NoSuchElementException(errorMessage); case WebDriverResult.Timeout: throw new WebDriverTimeoutException(errorMessage); case WebDriverResult.NoSuchWindow: throw new NoSuchWindowException(errorMessage); case WebDriverResult.InvalidCookieDomain: case WebDriverResult.UnableToSetCookie: throw new WebDriverException(errorMessage); case WebDriverResult.AsyncScriptTimeout: throw new WebDriverTimeoutException(errorMessage); case WebDriverResult.UnexpectedAlertOpen: // TODO(JimEvans): Handle the case where the unexpected alert setting // has been set to "ignore", so there is still a valid alert to be // handled. string alertText = string.Empty; if (errorAsDictionary.ContainsKey("alert")) { Dictionary<string, object> alertDescription = errorAsDictionary["alert"] as Dictionary<string, object>; if (alertDescription != null && alertDescription.ContainsKey("text")) { alertText = alertDescription["text"].ToString(); } } throw new UnhandledAlertException(errorMessage, alertText); case WebDriverResult.NoAlertPresent: throw new NoAlertPresentException(errorMessage); case WebDriverResult.InvalidSelector: throw new InvalidSelectorException(errorMessage); default: throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "{0} ({1})", errorMessage, errorResponse.Status)); } } else { throw new WebDriverException("Unexpected error. " + errorResponse.Value.ToString()); } } }
/// <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(string 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> /// Find the element in the response /// </summary> /// <param name="response">Reponse from the browser</param> /// <returns>Element from the page</returns> internal IWebElement GetElementFromResponse(Response response) { IWebElement element = null; ReadOnlyCollection<IWebElement> elements = GetElementsFromResponse(response); if (elements.Count > 0) { element = elements[0]; } return element; }
/// <summary> /// Finds the elements that are in the response /// </summary> /// <param name="response">Response from the browser</param> /// <returns>Collection of elements</returns> internal ReadOnlyCollection<IWebElement> GetElementsFromResponse(Response response) { List<IWebElement> toReturn = new List<IWebElement>(); object[] elements = response.Value as object[]; foreach (object elementObject in elements) { Dictionary<string, object> elementDictionary = elementObject as Dictionary<string, object>; if (elementDictionary != null) { string id = (string)elementDictionary["ELEMENT"]; RemoteWebElement element = this.CreateElement(id); toReturn.Add(element); } } return toReturn.AsReadOnly(); }
/// <summary> /// Find the element in the response /// </summary> /// <param name="response">Response from the browser</param> /// <returns>Element from the page</returns> internal IWebElement GetElementFromResponse(Response response) { if (response == null) { throw new NoSuchElementException(); } RemoteWebElement element = null; Dictionary<string, object> elementDictionary = response.Value as Dictionary<string, object>; if (elementDictionary != null) { string id = (string)elementDictionary["ELEMENT"]; element = this.CreateElement(id); } return element; }
private static void UnpackAndThrowOnError(Response errorResponse) { // Check the status code of the error, and only handle if not success. if (errorResponse.Status != WebDriverResult.Success) { Dictionary<string, object> errorAsDictionary = errorResponse.Value as Dictionary<string, object>; if (errorAsDictionary != null) { ErrorResponse errorResponseObject = new ErrorResponse(errorAsDictionary); string errorMessage = errorResponseObject.Message; switch (errorResponse.Status) { case WebDriverResult.NoSuchElement: throw new NoSuchElementException(errorMessage); case WebDriverResult.NoSuchFrame: throw new NoSuchFrameException(errorMessage); case WebDriverResult.UnknownCommand: throw new NotImplementedException(errorMessage); case WebDriverResult.ObsoleteElement: throw new StaleElementReferenceException(errorMessage); case WebDriverResult.ElementNotDisplayed: throw new ElementNotVisibleException(errorMessage); case WebDriverResult.InvalidElementState: case WebDriverResult.ElementNotSelectable: throw new InvalidElementStateException(errorMessage); case WebDriverResult.UnhandledError: throw new InvalidOperationException(errorMessage); case WebDriverResult.NoSuchDocument: throw new NoSuchElementException(errorMessage); case WebDriverResult.Timeout: throw new TimeoutException("The driver reported that the command timed out. There may " + "be several reasons for this. Check that the destination " + "site is in IE's 'Trusted Sites' (accessed from Tools->" + "Internet Options in the 'Security' tab) If it is a " + "trusted site, then the request may have taken more than " + "a minute to finish."); case WebDriverResult.NoSuchWindow: throw new NoSuchWindowException(errorMessage); case WebDriverResult.InvalidCookieDomain: case WebDriverResult.UnableToSetCookie: throw new WebDriverException(errorMessage); case WebDriverResult.AsyncScriptTimeout: throw new TimeoutException(errorMessage); case WebDriverResult.NoAlertPresent: throw new NoAlertPresentException(errorMessage); case WebDriverResult.InvalidSelector: throw new InvalidSelectorException(errorMessage); default: throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "{0} ({1})", errorMessage, errorResponse.Status)); } } else { throw new WebDriverException("Unexpected error. " + errorResponse.Value.ToString()); } } }
/// <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; }
/// <summary> /// Finds the elements that are in the response /// </summary> /// <param name="response">Response from the browser</param> /// <returns>Collection of elements</returns> internal ReadOnlyCollection<IWebElement> GetElementsFromResponse(Response response) { List<IWebElement> toReturn = new List<IWebElement>(); object[] elements = response.Value as object[]; foreach (object elementObject in elements) { Dictionary<string, object> elementDictionary = elementObject as Dictionary<string, object>; if (elementDictionary != null) { // TODO: Remove this "if" logic once the spec is properly updated // and remote-end implementations comply. string id = string.Empty; if (elementDictionary.ContainsKey("element-6066-11e4-a52e-4f735466cecf")) { id = (string)elementDictionary["element-6066-11e4-a52e-4f735466cecf"]; } else if (elementDictionary.ContainsKey("ELEMENT")) { id = (string)elementDictionary["ELEMENT"]; } RemoteWebElement element = this.CreateElement(id); toReturn.Add(element); } } return toReturn.AsReadOnly(); }
/// <summary> /// Returns a new <see cref="Response"/> from a JSON-encoded string. /// </summary> /// <param name="value">The JSON string to deserialize into a <see cref="Response"/>.</param> /// <returns>A <see cref="Response"/> object described by the JSON string.</returns> public static Response FromJson(string value) { Dictionary<string, object> deserializedResponse = JsonConvert.DeserializeObject<Dictionary<string, object>>(value, new ResponseValueJsonConverter()); Response response = new Response(deserializedResponse); return response; }
/// <summary> /// Gets the elements from the response coming back from Chrome /// </summary> /// <param name="response">The Chrome Response</param> /// <returns>A readonlycollection of WebElement</returns> public ReadOnlyCollection<IWebElement> GetElementsFrom(Response response) { List<IWebElement> elements = new List<IWebElement>(); object[] result = response.Value as object[]; if (result != null) { foreach (object element in result) { elements.Add(new ChromeWebElement(this, (string)element)); } } return new ReadOnlyCollection<IWebElement>(elements); }
/// <summary> /// Gets the element from the response coming back from Chrome /// </summary> /// <param name="response">The Chrome Response</param> /// <returns>A Web Element if the Item is found</returns> /// <exception cref="NoSuchElementException">Thrown if the item isn't found</exception> public IWebElement GetElementFrom(Response response) { if (response != null) { object result = response.Value; object[] elements = (object[])result; return new ChromeWebElement(this, (string)elements[0]); } throw new NoSuchElementException(); }
private void AmendElementValueIfNecessary(Response commandResponse) { if (!(commandResponse.Value is RemoteWebElement)) { return; } // Ensure that the parent is set properly RemoteWebElement existingElement = (RemoteWebElement)commandResponse.Value; existingElement.Parent = this; if (!Capabilities.IsJavaScriptEnabled) { return; } if (!(commandResponse.Value is RenderedRemoteWebElement)) { return; // Good, nothing to do} } RenderedRemoteWebElement replacement = new RenderedRemoteWebElement(); replacement.Id = existingElement.Id; replacement.Parent = this; commandResponse.Value = replacement; }
/// <summary> /// Finds the elements that are in the response /// </summary> /// <param name="response">Response from the browser</param> /// <returns>Collection of elements</returns> internal ReadOnlyCollection<IWebElement> GetElementsFromResponse(Response response) { List<IWebElement> toReturn = new List<IWebElement>(); object[] urls = (object[])response.Value; foreach (object url in urls) { // We cheat here, because we know that the URL for an element ends with its ID. // This is lazy and bad. We should, instead, go to each of the URLs in turn. string[] parts = url.ToString().Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries); RemoteWebElement element = CreateRemoteWebElement(); element.Id = parts[parts.Length - 1]; toReturn.Add(element); } return new ReadOnlyCollection<IWebElement>(toReturn); }
private RemoteWebElement CreateElementFromResponse(Response response) { var elementDictionary = response.Value as Dictionary<string, object>; if (elementDictionary == null) { return null; } return this.CreateElement((string)elementDictionary["ELEMENT"]); }
/// <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> public object ExecuteScript(string script, object[] args) { if (!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); Command command; if (convertedArgs != null && convertedArgs.Length > 0) { command = new Command(sessionId, new Context("foo"), DriverCommand.ExecuteScript, new object[] { script, convertedArgs }); } else { command = new Command(sessionId, new Context("foo"), DriverCommand.ExecuteScript, new object[] { script, new object[] { } }); } Response commandResponse = new Response(); try { commandResponse = executor.Execute(command); } catch (System.Net.WebException e) { commandResponse.IsError = true; commandResponse.Value = e; } if (commandResponse.IsError) { UnpackAndThrowOnError(commandResponse.Value); } return ParseJavaScriptReturnValue(commandResponse.Value); }