Skip to content

Commit

Permalink
Merge pull request #25 from 2gis/feature/ui-automation-patterns
Browse files Browse the repository at this point in the history
Add implementation "automation: ValuePattern.SetValue" script command
  • Loading branch information
NickAb committed Jun 30, 2015
2 parents 49a998f + ec9734b commit 30a72bb
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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"];
Expand All @@ -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<JToken> args)
{
var value = args.ElementAtOrDefault(1);
if (value == null)
{
var msg = string.Format(HelpArgumentsErrorMsg, HelpUrlAutomationScript);
throw new AutomationException(msg, ResponseStatus.JavaScriptError);
}

element.GetPattern<ValuePattern>(ValuePattern.Pattern).SetValue(value.ToString());
}

#endregion
}
}
6 changes: 3 additions & 3 deletions src/Winium.Desktop.Driver/Winium.Desktop.Driver.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@
<HintPath>..\packages\InputSimulator.1.0.4.0\lib\net20\WindowsInput.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Winium.Cruciatus, Version=2.0.5654.22251, Culture=neutral, processorArchitecture=MSIL">
<Reference Include="Winium.Cruciatus, Version=2.0.5658.24940, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Winium.Cruciatus.2.5.0\lib\net45\Winium.Cruciatus.dll</HintPath>
<HintPath>..\packages\Winium.Cruciatus.2.6.0\lib\net45\Winium.Cruciatus.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -143,4 +143,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
2 changes: 1 addition & 1 deletion src/Winium.Desktop.Driver/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net45" userInstalled="true" />
<package id="NLog" version="3.1.0.0" targetFramework="net451" userInstalled="true" />
<package id="Selenium.WebDriver" version="2.46.0" targetFramework="net451" userInstalled="true" />
<package id="Winium.Cruciatus" version="2.5.0" targetFramework="net451" userInstalled="true" />
<package id="Winium.Cruciatus" version="2.6.0" targetFramework="net451" />
</packages>

0 comments on commit 30a72bb

Please sign in to comment.