Skip to content

Commit

Permalink
Merge pull request #45 from 2gis/open-close-app
Browse files Browse the repository at this point in the history
Add launchApp, closeApp support
  • Loading branch information
skyline-gleb committed Jul 20, 2015
2 parents 0dae5aa + c726382 commit da4987d
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 19 deletions.
12 changes: 12 additions & 0 deletions Winium/Winium.StoreApps.Common/DriverCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public static class DriverCommand
{
#region Static Fields

#region Selenium

/// <summary>
/// Represents the AcceptAlert command
///
Expand Down Expand Up @@ -466,5 +468,15 @@ public static class DriverCommand
public static readonly string UploadFile = "uploadFile";

#endregion

#region Appium

public static readonly string LaunchApp = "launchApp";

public static readonly string CloseApp = "closeApp";

#endregion

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace Winium.StoreApps.Driver.CommandExecutors
{
using System;
using System.Threading;

using Winium.StoreApps.Common;
using Winium.StoreApps.Driver.Automator;

internal class CloseAppExecutor : CommandExecutorBase
{
#region Public Methods and Operators

public static void CloseApp(Automator automator)
{
var remoteCommand = new Command(DriverCommand.CloseApp);
automator.CommandForwarder.ForwardCommand(remoteCommand);
Thread.Sleep(TimeSpan.FromMilliseconds(500));
}

#endregion

#region Methods

protected override string DoImpl()
{
CloseApp(this.Automator);

return this.JsonResponse();
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@ internal object ExecuteMobileScript(string command)
case "OnScreenKeyboard.Disable":
this.Automator.EmulatorController.TypeKey(Keys.PageDown);
break;
case "App.Open":
LaunchAppExecutor.LaunchApp(this.Automator);
break;
case "App.Close":
CloseAppExecutor.CloseApp(this.Automator);
break;
default:
const string url =
const string Url =
"https://github.com/2gis/windows-universal-app-driver/wiki/Command-Execute-Script#press-hardware-button";
var msg = string.Format(
"Unknown 'mobile:' script command '{0}'. See {1} for supported commands.",
command ?? string.Empty,
url);
Url);
throw new AutomationException(msg, ResponseStatus.JavaScriptError);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Winium.StoreApps.Driver.CommandExecutors
{
using Winium.StoreApps.Driver.Automator;

internal class LaunchAppExecutor : CommandExecutorBase
{
#region Public Methods and Operators

public static void LaunchApp(Automator automator)
{
automator.Deployer.Launch();
}

#endregion

#region Methods

protected override string DoImpl()
{
LaunchApp(this.Automator);

return this.JsonResponse();
}

#endregion
}
}
59 changes: 46 additions & 13 deletions Winium/Winium.StoreApps.Driver/EmulatorHelpers/Deployer81.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ public class Deployer81 : IDeployer

private IAppManifestInfo appManifestInfo;

private IRemoteApplication application;
private IDevice device;

private bool installed = false;

private IRemoteApplication remoteApplication;

#endregion

Expand Down Expand Up @@ -80,24 +84,51 @@ public string DeviceName

#endregion

#region Public Methods and Operators
#region Properties

public void Install()
private IAppManifestInfo AppManifestInfo
{
this.appManifestInfo = Utils.ReadAppManifestInfoFromPackage(this.appPath);
get
{
return this.appManifestInfo
?? (this.appManifestInfo = Utils.ReadAppManifestInfoFromPackage(this.appPath));
}
}

Utils.InstallApplication(this.deviceInfo, this.appManifestInfo, DeploymentOptions.None, this.appPath);
private IDevice Device
{
get
{
return this.device ?? (this.device = this.connectableDevice.Connect(true));
}
}

var device = this.connectableDevice.Connect(true);
this.application = device.GetApplication(this.appManifestInfo.ProductId);
device.Activate();
private IRemoteApplication RemoteApplication
{
get
{
return this.remoteApplication
?? (this.remoteApplication = this.Device.GetApplication(this.AppManifestInfo.ProductId));
}
}

#endregion

#region Public Methods and Operators

public void Install()
{
Utils.InstallApplication(this.deviceInfo, this.AppManifestInfo, DeploymentOptions.None, this.appPath);
this.installed = true;

Logger.Info("Successfully deployed using Microsoft.Phone.Tools.Deploy");
}

public void Launch()
{
this.application.Launch();
this.Device.Activate();

this.RemoteApplication.Launch();
}

public void ReciveFiles(Dictionary<string, string> files)
Expand All @@ -112,7 +143,7 @@ public void SendFiles(Dictionary<string, string> files)
return;
}

var isolatedStore = this.application.GetIsolatedStore("Local");
var isolatedStore = this.RemoteApplication.GetIsolatedStore("Local");
foreach (var file in files)
{
var phoneDirectoryName = Path.GetDirectoryName(file.Value);
Expand All @@ -133,14 +164,16 @@ public void Terminate()

public void Uninstall()
{
if (this.application == null)
if (!this.installed)
{
Logger.Debug("Could not uninstall application that is already uninstalled.");
return;
}

this.application.Uninstall();
this.application = null;
this.remoteApplication.Uninstall();
this.remoteApplication = null;

this.device.Disconnect();
}

#endregion
Expand Down
13 changes: 10 additions & 3 deletions Winium/Winium.StoreApps.Driver/UriDispatchTables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ internal class UriDispatchTables

public UriDispatchTables(Uri prefix)
{
this.InitializeCommandDictionary();
this.InitializeSeleniumCommandDictionary();
this.InitializeAppiumCommandDictionary();
this.ConstructDispatcherTables(prefix);
}

Expand All @@ -46,7 +47,7 @@ public UriTemplateMatch Match(string httpMethod, Uri uriToMatch)

#region Methods

internal UriTemplateTable FindDispatcherTable(string httpMethod)
private UriTemplateTable FindDispatcherTable(string httpMethod)
{
UriTemplateTable tableToReturn = null;
switch (httpMethod)
Expand Down Expand Up @@ -88,7 +89,7 @@ private void ConstructDispatcherTables(Uri prefix)
this.deleteDispatcherTable.MakeReadOnly(false);
}

private void InitializeCommandDictionary()
private void InitializeSeleniumCommandDictionary()
{
this.commandDictionary.Add(DriverCommand.DefineDriverMapping, new CommandInfo("POST", "/config/drivers"));
this.commandDictionary.Add(DriverCommand.Status, new CommandInfo("GET", "/status"));
Expand Down Expand Up @@ -288,6 +289,12 @@ private void InitializeCommandDictionary()
this.commandDictionary.Add(DriverCommand.UploadFile, new CommandInfo("POST", "/session/{sessionId}/file"));
}

private void InitializeAppiumCommandDictionary()
{
this.commandDictionary.Add(DriverCommand.LaunchApp, new CommandInfo("POST", "/session/{sessionId}/appium/app/launch"));
this.commandDictionary.Add(DriverCommand.CloseApp, new CommandInfo("POST", "/session/{sessionId}/appium/app/close"));
}

#endregion
}
}
2 changes: 2 additions & 0 deletions Winium/Winium.StoreApps.Driver/Winium.StoreApps.Driver.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
<ItemGroup>
<Compile Include="Automator\Automator.cs" />
<Compile Include="Automator\Capabilities.cs" />
<Compile Include="CommandExecutors\CloseAppExecutor.cs" />
<Compile Include="CommandExecutors\LaunchAppExecutor.cs" />
<Compile Include="CommandExecutors\SubmitElementExecutor.cs" />
<Compile Include="CommandExecutorDispatchTable.cs" />
<Compile Include="CommandExecutors\ClickElementExecutor.cs" />
Expand Down
10 changes: 10 additions & 0 deletions Winium/Winium.StoreApps.InnerServer/AutomationServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ private async void HandleRequest(StreamSocket socket)
await writer.StoreAsync();

socket.Dispose();

if (this.automator.DoAfterResponseOnce == null)
{
return;
}

var localDoAfterResponseOnce = this.automator.DoAfterResponseOnce;
this.automator.DoAfterResponseOnce = null;

localDoAfterResponseOnce();
}

private async void ListenerConnectionReceived(
Expand Down
7 changes: 7 additions & 0 deletions Winium/Winium.StoreApps.InnerServer/Automator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ public Automator(UIElement visualRoot)
{
this.VisualRoot = GetTrueVisualRoot(visualRoot);
this.WebElements = new AutomatorElements();
this.DoAfterResponseOnce = null;
}

#endregion

#region Public Properties

public Action DoAfterResponseOnce { get; set; }

public UIElement VisualRoot { get; private set; }

public AutomatorElements WebElements { get; private set; }
Expand Down Expand Up @@ -137,6 +140,10 @@ public string ProcessCommand(string content)
{
commandToExecute = new GetElementTagNameCommand { ElementId = elementId };
}
else if (command.Equals(DriverCommand.CloseApp))
{
commandToExecute = new CloseAppCommand();
}
else
{
throw new NotImplementedException("Not implemented: " + command);
Expand Down
22 changes: 22 additions & 0 deletions Winium/Winium.StoreApps.InnerServer/Commands/CloseAppCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Winium.StoreApps.InnerServer.Commands
{
#region

using Windows.UI.Xaml;

#endregion

internal class CloseAppCommand : CommandBase
{
#region Public Methods and Operators

public override string DoImpl()
{
this.Automator.DoAfterResponseOnce = () => Application.Current.Exit();

return this.JsonResponse();
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ internal static bool IsUserVisible(this FrameworkElement element, UIElement visu
return false;
}


while (true)
{
if (element.Visibility != Visibility.Visible || !element.IsHitTestVisible || !(element.Opacity > 0))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<Compile Include="AutomatorElements.cs" />
<Compile Include="Commands\AlertCommand.cs" />
<Compile Include="Commands\AlertTextCommand.cs" />
<Compile Include="Commands\CloseAppCommand.cs" />
<Compile Include="Commands\ClickCommand.cs" />
<Compile Include="Commands\CommandBase.cs" />
<Compile Include="Commands\DisplayedCommand.cs" />
Expand Down

0 comments on commit da4987d

Please sign in to comment.