diff --git a/src/Winium.Desktop.Driver/CommandExecutors/ExecuteScriptExecutor.cs b/src/Winium.Desktop.Driver/CommandExecutors/ExecuteScriptExecutor.cs index 6f5b09c..90a47c0 100644 --- a/src/Winium.Desktop.Driver/CommandExecutors/ExecuteScriptExecutor.cs +++ b/src/Winium.Desktop.Driver/CommandExecutors/ExecuteScriptExecutor.cs @@ -2,17 +2,37 @@ { #region using - using System; + using System.Collections.Generic; + using System.Linq; + using System.Windows.Automation; using Newtonsoft.Json.Linq; + using Winium.Cruciatus.Elements; using Winium.Cruciatus.Extensions; using Winium.StoreApps.Common; + using Winium.StoreApps.Common.Exceptions; #endregion internal class ExecuteScriptExecutor : CommandExecutorBase { + #region Constants + + internal const string HelpArgumentsErrorMsg = "Arguments error. See {0} for more information."; + + internal const string HelpUnknownScriptMsg = "Unknown script command '{0} {1}'. See {2} for supported commands."; + + internal const string HelpUrlAutomationScript = + "https://github.com/2gis/Winium.Desktop/wiki/Command-Execute-Script#use-ui-automation-patterns-on-element"; + + internal const string HelpUrlInputScript = + "https://github.com/2gis/Winium.Desktop/wiki/Command-Execute-Script#simulate-input"; + + internal const string HelpUrlScript = "https://github.com/2gis/Winium.Desktop/wiki/Command-Execute-Script"; + + #endregion + #region Methods protected override string DoImpl() @@ -38,14 +58,35 @@ protected override string DoImpl() case "input": this.ExecuteInputScript(command); break; + case "automation": + this.ExecuteAutomationScript(command); + break; default: - var msg = string.Format("Unknown script command '{0} {1}'", prefix, command); - return this.JsonResponse(ResponseStatus.JavaScriptError, msg); + var msg = string.Format(HelpUnknownScriptMsg, prefix, command, HelpUrlScript); + throw new AutomationException(msg, ResponseStatus.JavaScriptError); } return this.JsonResponse(); } + private void ExecuteAutomationScript(string command) + { + var args = (JArray)this.ExecutedCommand.Parameters["args"]; + var elementId = args[0]["ELEMENT"].ToString(); + + var element = this.Automator.Elements.GetRegisteredElement(elementId); + + switch (command) + { + case "ValuePattern.SetValue": + this.ValuePatternSetValue(element, args); + break; + default: + var msg = string.Format(HelpUnknownScriptMsg, "automation:", command, HelpUrlAutomationScript); + throw new AutomationException(msg, ResponseStatus.JavaScriptError); + } + } + private void ExecuteInputScript(string command) { var args = (JArray)this.ExecutedCommand.Parameters["args"]; @@ -59,10 +100,23 @@ private void ExecuteInputScript(string command) element.ClickWithPressedCtrl(); return; default: - throw new NotImplementedException(string.Format("Input-command {0} is not implemented", command)); + var msg = string.Format(HelpUnknownScriptMsg, "input:", command, HelpUrlInputScript); + throw new AutomationException(msg, ResponseStatus.JavaScriptError); } } + private void ValuePatternSetValue(CruciatusElement element, IEnumerable args) + { + var value = args.ElementAtOrDefault(1); + if (value == null) + { + var msg = string.Format(HelpArgumentsErrorMsg, HelpUrlAutomationScript); + throw new AutomationException(msg, ResponseStatus.JavaScriptError); + } + + element.GetPattern(ValuePattern.Pattern).SetValue(value.ToString()); + } + #endregion } } diff --git a/src/Winium.Desktop.Driver/Winium.Desktop.Driver.csproj b/src/Winium.Desktop.Driver/Winium.Desktop.Driver.csproj index ab1db2e..2d487ab 100644 --- a/src/Winium.Desktop.Driver/Winium.Desktop.Driver.csproj +++ b/src/Winium.Desktop.Driver/Winium.Desktop.Driver.csproj @@ -61,9 +61,9 @@ ..\packages\InputSimulator.1.0.4.0\lib\net20\WindowsInput.dll True - + False - ..\packages\Winium.Cruciatus.2.5.0\lib\net45\Winium.Cruciatus.dll + ..\packages\Winium.Cruciatus.2.6.0\lib\net45\Winium.Cruciatus.dll @@ -143,4 +143,4 @@ --> - + \ No newline at end of file diff --git a/src/Winium.Desktop.Driver/packages.config b/src/Winium.Desktop.Driver/packages.config index 8736187..c9c1c3a 100644 --- a/src/Winium.Desktop.Driver/packages.config +++ b/src/Winium.Desktop.Driver/packages.config @@ -5,5 +5,5 @@ - + \ No newline at end of file