/// <summary> /// Executes the command. /// </summary> /// <param name="environment">The <see cref="CommandEnvironment"/> to use in executing the command.</param> /// <param name="parameters">The <see cref="Dictionary{string, object}"/> containing the command parameters.</param> /// <returns>The JSON serialized string representing the command response.</returns> public override Response Execute(CommandEnvironment environment, Dictionary <string, object> parameters) { object mechanism; if (!parameters.TryGetValue("using", out mechanism)) { return(Response.CreateMissingParametersResponse("using")); } object criteria; if (!parameters.TryGetValue("value", out criteria)) { return(Response.CreateMissingParametersResponse("value")); } Response response; DateTime timeout = DateTime.Now.AddMilliseconds(environment.ImplicitWaitTimeout); do { string result = this.EvaluateAtom(environment, WebDriverAtoms.FindElements, mechanism, criteria, null, environment.CreateFrameObject()); response = Response.FromJson(result); if (response.Status == WebDriverStatusCode.Success) { object[] foundElements = response.Value as object[]; if (foundElements != null && foundElements.Length > 0) { // Return early for success return(response); } } else if (response.Status != WebDriverStatusCode.NoSuchElement) { if (mechanism.ToString().ToLowerInvariant() != "xpath" && response.Status == WebDriverStatusCode.InvalidSelector) { response.Status = WebDriverStatusCode.NoSuchElement; } // Also return early for response of not NoSuchElement. return(response); } }while (DateTime.Now < timeout); // We should theoretically only reach here if the timeout has // expired, and no elements have been found. This is still a // success condition. Since the JSON result hasn't actually been // modified, we can simply return it. return(response); }
/// <summary> /// Executes the command. /// </summary> /// <param name="environment">The <see cref="CommandEnvironment"/> to use in executing the command.</param> /// <param name="parameters">The <see cref="Dictionary{string, object}"/> containing the command parameters.</param> /// <returns>The JSON serialized string representing the command response.</returns> public override Response Execute(CommandEnvironment environment, Dictionary <string, object> parameters) { object element; if (!parameters.TryGetValue("ID", out element)) { return(Response.CreateMissingParametersResponse("ID")); } // TODO: Fix this to properly evaluate the position in the view port // before blindly scrolling. This is a hack, to be rectified in the // a future revision. string tagNameScript = "arguments[0].scrollIntoView(false);"; string scrollResult = this.EvaluateAtom(environment, WebDriverAtoms.ExecuteScript, tagNameScript, new object[] { element }, environment.CreateFrameObject()); string result = this.EvaluateAtom(environment, WebDriverAtoms.GetTopLeftCoordinates, element, environment.CreateFrameObject()); return(Response.FromJson(result)); }
/// <summary> /// Executes the command. /// </summary> /// <param name="environment">The <see cref="CommandEnvironment"/> to use in executing the command.</param> /// <param name="parameters">The <see cref="Dictionary{string, object}"/> containing the command parameters.</param> /// <returns>The JSON serialized string representing the command response.</returns> public override Response Execute(CommandEnvironment environment, Dictionary <string, object> parameters) { WebBrowserNavigationMonitor monitor = new WebBrowserNavigationMonitor(environment); monitor.MonitorNavigation(() => { string refreshScript = "window.location.reload(true);"; this.EvaluateAtom(environment, WebDriverAtoms.ExecuteScript, refreshScript, new object[] { }, environment.CreateFrameObject()); }); if (monitor.IsNavigationTimedOut) { return(Response.CreateErrorResponse(WebDriverStatusCode.Timeout, "Timed out loading page")); } environment.FocusedFrame = string.Empty; return(Response.CreateSuccessResponse()); }
/// <summary> /// Executes the command. /// </summary> /// <param name="environment">The <see cref="CommandEnvironment"/> to use in executing the command.</param> /// <param name="parameters">The <see cref="Dictionary{string, object}"/> containing the command parameters.</param> /// <returns>The JSON serialized string representing the command response.</returns> public override Response Execute(CommandEnvironment environment, Dictionary <string, object> parameters) { object button; if (!parameters.TryGetValue("button", out button)) { button = 0; } string result = this.EvaluateAtom(environment, WebDriverAtoms.MouseClick, button, environment.MouseState, environment.CreateFrameObject()); Response atomResponse = Response.FromJson(result); if (atomResponse.Status == WebDriverStatusCode.Success) { Dictionary <string, object> mouseState = atomResponse.Value as Dictionary <string, object>; if (mouseState != null) { environment.MouseState = mouseState; } return(Response.CreateSuccessResponse()); } return(atomResponse); }
/// <summary> /// Executes the command. /// </summary> /// <param name="environment">The <see cref="CommandEnvironment"/> to use in executing the command.</param> /// <param name="parameters">The <see cref="Dictionary{string, object}"/> containing the command parameters.</param> /// <returns>The JSON serialized string representing the command response.</returns> public override Response Execute(CommandEnvironment environment, Dictionary <string, object> parameters) { object frameIdentifier; if (!parameters.TryGetValue("id", out frameIdentifier)) { return(Response.CreateMissingParametersResponse("id")); } Response response; string result = string.Empty; if (frameIdentifier == null) { environment.FocusedFrame = string.Empty; Dictionary <string, object> responseValue = new Dictionary <string, object>(); responseValue[CommandEnvironment.WindowObjectKey] = null; return(Response.CreateSuccessResponse(responseValue)); } else { Dictionary <string, object> frameElement = frameIdentifier as Dictionary <string, object>; if (frameElement == null) { string frameAtom = WebDriverAtoms.FrameByIndex; string frameIdAsString = frameIdentifier as string; if (frameIdAsString != null) { frameAtom = WebDriverAtoms.FrameByIdOrName; } result = this.EvaluateAtom(environment, frameAtom, frameIdentifier, environment.CreateFrameObject()); } else { result = this.EvaluateAtom(environment, WebDriverAtoms.GetFrameWindow, frameElement); } response = Response.FromJson(result); Dictionary <string, object> valueAsDictionary = response.Value as Dictionary <string, object>; if (valueAsDictionary == null) { string errorMessage = string.Format("No frame found for criteria {0}", frameIdentifier.ToString()); response = Response.CreateErrorResponse(WebDriverStatusCode.NoSuchFrame, errorMessage); } else { if (response.Status == WebDriverStatusCode.Success) { environment.FocusedFrame = valueAsDictionary[CommandEnvironment.WindowObjectKey].ToString(); } } } return(response); }
public override Response Execute(CommandEnvironment environment, Dictionary <string, object> parameters) { object script; if (!parameters.TryGetValue("script", out script)) { return(Response.CreateMissingParametersResponse("script")); } object args; if (!parameters.TryGetValue("args", out args)) { return(Response.CreateMissingParametersResponse("args")); } this.navigationStarted = false; environment.Browser.Navigating += this.BrowserNavigatingEventHandler; object[] argsArray = new object[] { script, args, environment.AsyncScriptTimeout }; string argumentString = CreateArgumentString(argsArray); string callback = "function(result){window.top.__wd_fn_result = result;}"; argumentString = string.Format(CultureInfo.InvariantCulture, "{0}, {1}, {2}", argumentString, callback, JsonConvert.SerializeObject(environment.CreateFrameObject())); string atom = "window.top.__wd_fn_result = '';(" + WebDriverAtoms.ExecuteAsyncScript + ")(" + argumentString + ");"; string result = string.Empty; environment.Browser.Dispatcher.BeginInvoke(() => { try { environment.Browser.InvokeScript("eval", atom); } catch (Exception ex) { result = string.Format(CultureInfo.InvariantCulture, "{{ \"status\": {2}, \"value\": {{ \"message\": \"Unexpected exception ({0}) - '{1}'\" }} }}", ex.GetType().ToString(), ex.Message, WebDriverStatusCode.UnhandledError); } }); result = this.WaitForAsyncScriptResult(environment); environment.Browser.Navigating -= this.BrowserNavigatingEventHandler; if (this.navigationStarted) { return(Response.CreateErrorResponse(WebDriverStatusCode.UnhandledError, "Page load detected during asynchronous script execution")); } return(Response.FromJson(result)); }
/// <summary> /// Executes the command. /// </summary> /// <param name="environment">The <see cref="CommandEnvironment"/> to use in executing the command.</param> /// <param name="parameters">The <see cref="Dictionary{string, object}"/> containing the command parameters.</param> /// <returns>The JSON serialized string representing the command response.</returns> public override Response Execute(CommandEnvironment environment, Dictionary <string, object> parameters) { string pageSource = string.Empty; if (string.IsNullOrEmpty(environment.FocusedFrame)) { ManualResetEvent synchronizer = new ManualResetEvent(false); /*Deployment.Current.Dispatcher.BeginInvoke(() => * { * pageSource = environment.Browser.SaveToString(); * synchronizer.Set(); * }); * * synchronizer.WaitOne();*/ } else { string script = "return document.documentElement.outerHTML;"; string result = this.EvaluateAtom(environment, WebDriverAtoms.ExecuteScript, script, new object[] { }, environment.CreateFrameObject()); return(Response.FromJson(result)); } return(Response.CreateSuccessResponse(pageSource)); }
/// <summary> /// Executes the command. /// </summary> /// <param name="environment">The <see cref="CommandEnvironment"/> to use in executing the command.</param> /// <param name="parameters">The <see cref="Dictionary{string, object}"/> containing the command parameters.</param> /// <returns>The JSON serialized string representing the command response.</returns> public override Response Execute(CommandEnvironment environment, Dictionary <string, object> parameters) { object script; if (!parameters.TryGetValue("script", out script)) { return(Response.CreateMissingParametersResponse("script")); } object args; if (!parameters.TryGetValue("args", out args)) { return(Response.CreateMissingParametersResponse("args")); } // TODO for some reason the GetAttribute method is invoked through this ... // if (driver.IsSpecificationCompliant) Execute(DriverCommand.ExecuteScript, dictionary); var getAtomMethod = typeof(RemoteWebElement).GetMethod("GetAtom", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic); var atom = getAtomMethod.Invoke(null, new object[] { "getAttribute.js" }); if (atom.Equals(script.ToString())) { var keyValuePairs = JArray.Parse(args.ToString()); var elementId = keyValuePairs[0].Last().Last().ToString(); var attributeName = keyValuePairs[1].ToString(); return(CommandHandlerFactory.Instance.GetHandler(DriverCommand.GetElementAttribute).Execute(environment, new Dictionary <string, object> { { "NAME", attributeName }, { "ID", elementId } })); } atom = getAtomMethod.Invoke(null, new object[] { "isDisplayed.js" }); if (atom.Equals(script.ToString())) { var keyValuePairs = JArray.Parse(args.ToString()); var elementId = keyValuePairs[0].Last().Last().ToString(); return(CommandHandlerFactory.Instance.GetHandler(DriverCommand.IsElementDisplayed).Execute(environment, new Dictionary <string, object> { { "ID", elementId } })); } if (script.ToString() == "var rect = arguments[0].getBoundingClientRect(); return {'x': rect.left, 'y': rect.top};") { var keyValuePairs = JArray.Parse(args.ToString()); var elementId = keyValuePairs[0].Last().Last().ToString(); return(CommandHandlerFactory.Instance.GetHandler(DriverCommand.GetElementRect).Execute(environment, new Dictionary <string, object> { { "ID", elementId } })); } if (script.ToString() == "return window.name") { var keyValuePairs = JArray.Parse(args.ToString()); /*var elementId = keyValuePairs[0].Last().Last().ToString(); * return CommandHandlerFactory.Instance.GetHandler(DriverCommand.GetElementRect).Execute(environment, new Dictionary<string, object> * { * { "ID", elementId } * });*/ return(Response.CreateErrorResponse(WebDriverStatusCode.UnexpectedJavaScriptError, "Cannot get window.name")); } string result = this.EvaluateAtom(environment, WebDriverAtoms.ExecuteScript, script, args, environment.CreateFrameObject()); return(Response.FromJson(result)); }
/// <summary> /// Executes the command. /// </summary> /// <param name="environment">The <see cref="CommandEnvironment"/> to use in executing the command.</param> /// <param name="parameters">The <see cref="Dictionary{string, object}"/> containing the command parameters.</param> /// <returns>The JSON serialized string representing the command response.</returns> public override Response Execute(CommandEnvironment environment, Dictionary <string, object> parameters) { string currentUrl = string.Empty; if (string.IsNullOrEmpty(environment.FocusedFrame)) { ManualResetEvent synchronizer = new ManualResetEvent(false); /*environment.Browser.Dispatcher.BeginInvoke(() => * { * Uri currentUri = environment.Browser.Source; * if (currentUri != null) * { * currentUrl = currentUri.ToString(); * } * * synchronizer.Set(); * }); * * synchronizer.WaitOne();*/ } else { string currentUrlScript = "return window.location.href;"; string result = this.EvaluateAtom(environment, WebDriverAtoms.ExecuteScript, currentUrlScript, new object[] { }, environment.CreateFrameObject()); return(Response.FromJson(result)); } return(Response.CreateSuccessResponse(currentUrl)); }
/// <summary> /// Executes the command. /// </summary> /// <param name="environment">The <see cref="CommandEnvironment"/> to use in executing the command.</param> /// <param name="parameters">The <see cref="Dictionary{string, object}"/> containing the command parameters.</param> /// <returns>The JSON serialized string representing the command response.</returns> public override Response Execute(CommandEnvironment environment, Dictionary <string, object> parameters) { object element; if (!parameters.TryGetValue("ID", out element)) { return(Response.CreateMissingParametersResponse("ID")); } this.AtomExecutionTimeout = TimeSpan.FromMilliseconds(100); string result = this.EvaluateAtom(environment, WebDriverAtoms.Click, element, environment.CreateFrameObject()); if (string.IsNullOrEmpty(result)) { return(Response.CreateSuccessResponse()); } return(Response.FromJson(result)); }
/// <summary> /// Executes the command. /// </summary> /// <param name="environment">The <see cref="CommandEnvironment"/> to use in executing the command.</param> /// <param name="parameters">The <see cref="Dictionary{string, object}"/> containing the command parameters.</param> /// <returns>The JSON serialized string representing the command response.</returns> public override Response Execute(CommandEnvironment environment, Dictionary <string, object> parameters) { object element = null; object elementId; if (parameters.TryGetValue("element", out elementId) && elementId != null) { element = new Dictionary <string, object>() { { CommandEnvironment.ElementObjectKey, elementId.ToString() } }; } object offsetX = null; if (parameters.ContainsKey("xoffset")) { offsetX = parameters["xoffset"]; } object offsetY = null; if (parameters.ContainsKey("yoffset")) { offsetY = parameters["yoffset"]; } string result = this.EvaluateAtom(environment, WebDriverAtoms.MouseMove, element, offsetX, offsetY, environment.MouseState, environment.CreateFrameObject()); Response atomResponse = Response.FromJson(result); if (atomResponse.Status == WebDriverStatusCode.Success) { Dictionary <string, object> mouseState = atomResponse.Value as Dictionary <string, object>; if (mouseState != null) { environment.MouseState = mouseState; } return(Response.CreateSuccessResponse()); } return(atomResponse); }
/// <summary> /// Executes the command. /// </summary> /// <param name="environment">The <see cref="CommandEnvironment"/> to use in executing the command.</param> /// <param name="parameters">The <see cref="Dictionary{string, object}"/> containing the command parameters.</param> /// <returns>The JSON serialized string representing the command response.</returns> public override Response Execute(CommandEnvironment environment, Dictionary <string, object> parameters) { object windowIdentifier; if (!parameters.TryGetValue("name", out windowIdentifier)) { return(Response.CreateMissingParametersResponse("name")); } if (windowIdentifier.ToString() != CommandEnvironment.GlobalWindowHandle) { string script = "return window.top.name;"; string result = this.EvaluateAtom(environment, WebDriverAtoms.ExecuteScript, script, new object[] { }, environment.CreateFrameObject()); Response interimResponse = Response.FromJson(result); if (interimResponse.Value != null && interimResponse.Value.ToString() == windowIdentifier.ToString()) { return(Response.CreateSuccessResponse()); } return(Response.CreateErrorResponse(WebDriverStatusCode.NoSuchWindow, "No window found")); } return(Response.CreateSuccessResponse()); }
/// <summary> /// Executes the command. /// </summary> /// <param name="environment">The <see cref="CommandEnvironment"/> to use in executing the command.</param> /// <param name="parameters">The <see cref="Dictionary{string, object}"/> containing the command parameters.</param> /// <returns>The JSON serialized string representing the command response.</returns> public override Response Execute(CommandEnvironment environment, Dictionary <string, object> parameters) { object element; if (!parameters.TryGetValue("ID", out element)) { return(Response.CreateMissingParametersResponse("ID")); } object keys; if (!parameters.TryGetValue("value", out keys)) { return(Response.CreateMissingParametersResponse("value")); } string keysAsString = string.Empty; object[] keysAsArray = keys as object[]; if (keysAsArray != null) { foreach (object key in keysAsArray) { keysAsString += key.ToString(); } } // Normalize line endings to single line feed, as that's what the atom expects. keysAsString = keysAsString.Replace("\r\n", "\n"); string result = this.EvaluateAtom(environment, WebDriverAtoms.Type, element, keysAsString, environment.CreateFrameObject()); return(Response.FromJson(result)); }
/// <summary> /// Executes the command. /// </summary> /// <param name="environment">The <see cref="CommandEnvironment"/> to use in executing the command.</param> /// <param name="parameters">The <see cref="Dictionary{string, object}"/> containing the command parameters.</param> /// <returns>The JSON serialized string representing the command response.</returns> public override Response Execute(CommandEnvironment environment, Dictionary <string, object> parameters) { object script; if (!parameters.TryGetValue("script", out script)) { return(Response.CreateMissingParametersResponse("script")); } object args; if (!parameters.TryGetValue("args", out args)) { return(Response.CreateMissingParametersResponse("args")); } string result = this.EvaluateAtom(environment, WebDriverAtoms.ExecuteScript, script, args, environment.CreateFrameObject()); return(Response.FromJson(result)); }
/// <summary> /// Executes the command. /// </summary> /// <param name="environment">The <see cref="CommandEnvironment"/> to use in executing the command.</param> /// <param name="parameters">The <see cref="Dictionary{string, object}"/> containing the command parameters.</param> /// <returns>The JSON serialized string representing the command response.</returns> public override Response Execute(CommandEnvironment environment, Dictionary <string, object> parameters) { object element; if (!parameters.TryGetValue("ID", out element)) { return(Response.CreateMissingParametersResponse("ID")); } string result = this.EvaluateAtom(environment, WebDriverAtoms.Submit, element, environment.CreateFrameObject()); return(Response.FromJson(result)); }
/// <summary> /// Executes the command. /// </summary> /// <param name="environment">The <see cref="CommandEnvironment"/> to use in executing the command.</param> /// <param name="parameters">The <see cref="Dictionary{string, object}"/> containing the command parameters.</param> /// <returns>The JSON serialized string representing the command response.</returns> public override Response Execute(CommandEnvironment environment, Dictionary <string, object> parameters) { CookieCollection cookies = null; ManualResetEvent synchronizer = new ManualResetEvent(false); environment.Browser.Dispatcher.BeginInvoke(() => { if (environment.Browser.Source != null) { cookies = environment.Browser.GetCookies(); } synchronizer.Set(); }); synchronizer.WaitOne(); if (cookies != null) { foreach (Cookie currentCookie in cookies) { this.EvaluateAtom(environment, DeleteCookieScript, currentCookie.Name, environment.CreateFrameObject()); } } return(Response.CreateSuccessResponse()); }
/// <summary> /// Executes the command. /// </summary> /// <param name="environment">The <see cref="CommandEnvironment"/> to use in executing the command.</param> /// <param name="parameters">The <see cref="Dictionary{string, object}"/> containing the command parameters.</param> /// <returns>The JSON serialized string representing the command response.</returns> public override Response Execute(CommandEnvironment environment, Dictionary <string, object> parameters) { object element; if (!parameters.TryGetValue("ID", out element)) { return(Response.CreateMissingParametersResponse("ID")); } object mechanism; if (!parameters.TryGetValue("using", out mechanism)) { return(Response.CreateMissingParametersResponse("using")); } object criteria; if (!parameters.TryGetValue("value", out criteria)) { return(Response.CreateMissingParametersResponse("value")); } Response response; DateTime timeout = DateTime.Now.AddMilliseconds(environment.ImplicitWaitTimeout); do { string result = this.EvaluateAtom(environment, WebDriverAtoms.FindElement, mechanism, criteria, element, environment.CreateFrameObject()); response = Response.FromJson(result); if (response.Status == WebDriverStatusCode.Success) { // Return early for success Dictionary <string, object> foundElement = response.Value as Dictionary <string, object>; if (foundElement != null && foundElement.ContainsKey(CommandEnvironment.ElementObjectKey)) { return(response); } } else if (response.Status != WebDriverStatusCode.NoSuchElement) { if (mechanism.ToString().ToUpperInvariant() != "XPATH" && response.Status == WebDriverStatusCode.InvalidSelector) { continue; } // Also return early for response of not NoSuchElement. return(response); } }while (DateTime.Now < timeout); string errorMessage = string.Format(CultureInfo.InvariantCulture, "No element found for {0} == '{1}'", mechanism.ToString(), criteria.ToString()); response = Response.CreateErrorResponse(WebDriverStatusCode.NoSuchElement, errorMessage); return(response); }
/// <summary> /// Executes the command. /// </summary> /// <param name="environment">The <see cref="CommandEnvironment"/> to use in executing the command.</param> /// <param name="parameters">The <see cref="Dictionary{string, object}"/> containing the command parameters.</param> /// <returns>The JSON serialized string representing the command response.</returns> public override Response Execute(CommandEnvironment environment, Dictionary <string, object> parameters) { object element; if (!parameters.TryGetValue("ID", out element)) { return(Response.CreateMissingParametersResponse("ID")); } string tagNameScript = "return arguments[0].tagName;"; string result = this.EvaluateAtom(environment, WebDriverAtoms.ExecuteScript, tagNameScript, new object[] { element }, environment.CreateFrameObject()); return(Response.FromJson(result)); }
/// <summary> /// Executes the command. /// </summary> /// <param name="environment">The <see cref="CommandEnvironment"/> to use in executing the command.</param> /// <param name="parameters">The <see cref="Dictionary{string, object}"/> containing the command parameters.</param> /// <returns>The JSON serialized string representing the command response.</returns> public override Response Execute(CommandEnvironment environment, Dictionary <string, object> parameters) { object element; if (!parameters.TryGetValue("ID", out element)) { return(Response.CreateMissingParametersResponse("ID")); } object propertyName; if (!parameters.TryGetValue("PROPERTYNAME", out propertyName)) { return(Response.CreateMissingParametersResponse("PROPERTYNAME")); } string result = this.EvaluateAtom(environment, WebDriverAtoms.GetValueOfCssProperty, element, propertyName, environment.CreateFrameObject()); return(Response.FromJson(result)); }
/// <summary> /// Executes the command. /// </summary> /// <param name="environment">The <see cref="CommandEnvironment"/> to use in executing the command.</param> /// <param name="parameters">The <see cref="Dictionary{string, object}"/> containing the command parameters.</param> /// <returns>The JSON serialized string representing the command response.</returns> public override Response Execute(CommandEnvironment environment, Dictionary <string, object> parameters) { object cookieName; if (!parameters.TryGetValue("NAME", out cookieName)) { return(Response.CreateMissingParametersResponse("NAME")); } string result = this.EvaluateAtom(environment, DeleteCookieScript, cookieName, environment.CreateFrameObject()); return(Response.FromJson(result)); }