Skip to content

Commit

Permalink
Merge pull request #10 from 2gis/fix-driver-responses
Browse files Browse the repository at this point in the history
Fixes c# selenium binding not working with driver 👍
  • Loading branch information
NickAb committed Mar 2, 2015
2 parents 4a4c9db + 8dfcf5c commit a6f8563
Show file tree
Hide file tree
Showing 24 changed files with 81 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static Dictionary<HttpStatusCode, string> StatusCodeDescriptors

public static string ResponseString(HttpStatusCode statusCode, string content)
{
var contentType = statusCode == HttpStatusCode.BadRequest ? PlainTextContentType : JsonContentType;
var contentType = IsClientError((int)statusCode) ? PlainTextContentType : JsonContentType;

string statusDescription;
StatusCodeDescriptors.TryGetValue(statusCode, out statusDescription);
Expand All @@ -59,6 +59,11 @@ public static string ResponseString(HttpStatusCode statusCode, string content)
return responseString.ToString();
}

public static bool IsClientError(int code)
{
return code >= 400 && code < 500;
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public override string DoImpl()
}

invokeProv.Invoke();
return null;
return this.JsonResponse();
}

throw new AutomationException("No alert is displayed", ResponseStatus.NoAlertOpenError);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ public virtual string DoImpl()
throw new NotImplementedException();
}

/// <summary>
/// The JsonResponse with SUCCESS status and NULL value.
/// </summary>
public string JsonResponse()
{
return JsonConvert.SerializeObject(new JsonResponse(this.Session, ResponseStatus.Success, null));
}

public string JsonResponse(ResponseStatus status, object value)
{
if (status != ResponseStatus.Success && value == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public override string DoImpl()
throw new AutomationException(msg, ResponseStatus.JavaScriptError);
}

return this.JsonResponse(ResponseStatus.Success, string.Empty);
return this.JsonResponse();
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public override string DoImpl()

if (attributeName == null)
{
return this.JsonResponse(ResponseStatus.Success, null);
return this.JsonResponse();
}

/* GetAttribute command should return: null if no property was found,
Expand All @@ -47,7 +47,7 @@ public override string DoImpl()
}
catch (AutomationException)
{
return this.JsonResponse(ResponseStatus.Success, null);
return this.JsonResponse();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace WindowsUniversalAppDriver.InnerServer.Commands
namespace WindowsUniversalAppDriver.InnerServer.Commands.Helpers
{
using System;
using System.Reflection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public override string DoImpl()
}

TrySetText(textbox, this.KeyString);
return this.JsonResponse(ResponseStatus.Success, null);
return this.JsonResponse();
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<Compile Include="Commands\LocationInViewCommand.cs" />
<Compile Include="Commands\OrientationCommand.cs" />
<Compile Include="Commands\PageSourceCommand.cs" />
<Compile Include="Commands\ScreenCoordinatesHelper.cs" />
<Compile Include="Commands\Helpers\ScreenCoordinatesHelper.cs" />
<Compile Include="Commands\GetElementTagNameCommand.cs" />
<Compile Include="Commands\TextCommand.cs" />
<Compile Include="Commands\ValueCommand.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protected override string DoImpl()

this.Automator.EmulatorController.LeftButtonClick(location.Value);

return null;
return this.JsonResponse();
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ protected override string DoImpl()
{
this.Automator.Deployer.Uninstall();

return null;
return this.JsonResponse();
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ public string Do()
}
catch (NotImplementedException exception)
{
return HttpResponseHelper.ResponseString(HttpStatusCode.NotImplemented, exception.Message);
return HttpResponseHelper.ResponseString(
HttpStatusCode.NotImplemented,
this.JsonResponse(ResponseStatus.UnknownCommand, exception.Message));
}
catch (Exception exception)
{
Expand All @@ -70,6 +72,14 @@ protected virtual string DoImpl()
throw new InvalidOperationException("DoImpl should never be called in CommandExecutorBase");
}

/// <summary>
/// The JsonResponse with SUCCESS status and NULL value.
/// </summary>
protected string JsonResponse()
{
return this.JsonResponse(ResponseStatus.Success, null);
}

protected string JsonResponse(ResponseStatus status, object value)
{
return JsonConvert.SerializeObject(new JsonResponse(this.Automator.Session, status, value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected override string DoImpl()
break;
}

return null;
return this.JsonResponse();
}

internal void ExecuteMobileScript(string command)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
namespace WindowsUniversalAppDriver.CommandExecutors
{
#region

using WindowsUniversalAppDriver.Common;

#endregion

internal class GetCurrentWindowHandleExecutor : CommandExecutorBase
{
#region Methods

protected override string DoImpl()
{
// TODO: There is only one window for windows phone app, so it must be OK, or not?
return "current";
return this.JsonResponse(ResponseStatus.Success, "current");
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
namespace WindowsUniversalAppDriver.CommandExecutors
{
#region

using System.Windows.Forms;

#endregion

internal class GoBackExecutor : CommandExecutorBase
{
#region Methods
Expand All @@ -11,7 +15,7 @@ protected override string DoImpl()
// F1 is shortcut for "Back" hardware button
this.Automator.EmulatorController.TypeKey(Keys.F1);

return null;
return this.JsonResponse();
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ protected override string DoImpl()
{
this.Automator.EmulatorController.LeftButtonClick();

return null;
return this.JsonResponse();
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ protected override string DoImpl()
{
this.Automator.EmulatorController.LeftButtonDown();

return null;
return this.JsonResponse();
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
namespace WindowsUniversalAppDriver.CommandExecutors
{
#region

using System;
using System.Drawing;
using System.Globalization;

using WindowsUniversalAppDriver.Automator;

#endregion

internal class MouseMoveToExecutor : CommandExecutorBase
{
#region Methods
Expand All @@ -30,7 +34,7 @@ protected override string DoImpl()
this.Automator.UpdatedOrientationForEmulatorController();
this.Automator.EmulatorController.MoveCursorTo(coordinates);

return null;
return this.JsonResponse();
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ protected override string DoImpl()
{
this.Automator.EmulatorController.LeftButtonUp();

return null;
return this.JsonResponse();
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ protected override string DoImpl()
// Gives sometime to load visuals (needed only in case of slow emulation)
Thread.Sleep(this.Automator.ActualCapabilities.LaunchDelay);

var jsonResponse = this.JsonResponse(ResponseStatus.Success, this.Automator.ActualCapabilities);

return jsonResponse;
return this.JsonResponse(ResponseStatus.Success, this.Automator.ActualCapabilities);
}

private EmulatorController CreateEmulatorController(bool withFallback)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
namespace WindowsUniversalAppDriver.CommandExecutors
{
using WindowsUniversalAppDriver.Common;

internal class ScreenshotExecutor : CommandExecutorBase
{
#region Methods

protected override string DoImpl()
{
return this.Automator.EmulatorController.TakeScreenshot();
var base64Screenshot = this.Automator.EmulatorController.TakeScreenshot();
return this.JsonResponse(ResponseStatus.Success, base64Screenshot);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
namespace WindowsUniversalAppDriver.CommandExecutors
{
#region

using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

#endregion

internal class SendKeysToElementExecutor : CommandExecutorBase
{
#region Methods
Expand Down Expand Up @@ -42,7 +46,7 @@ protected override string DoImpl()
this.Automator.EmulatorController.TypeKey(magicKey);
}

return null;
return this.JsonResponse();
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
namespace WindowsUniversalAppDriver.CommandExecutors
{
#region

using System;
using System.Drawing;
using System.Globalization;

using WindowsUniversalAppDriver.Automator;
using WindowsUniversalAppDriver.EmulatorHelpers;

#endregion

internal class TouchFlickExecutor : CommandExecutorBase
{
#region Methods
Expand Down Expand Up @@ -38,7 +42,7 @@ protected override string DoImpl()
this.Automator.EmulatorController.PerformGesture(new FlickGesture(startPoint, xSpeed, ySpeed));
}

return null;
return this.JsonResponse();
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
namespace WindowsUniversalAppDriver.CommandExecutors
{
#region

using System;
using System.Drawing;
using System.Globalization;

using WindowsUniversalAppDriver.Automator;
using WindowsUniversalAppDriver.EmulatorHelpers;

#endregion

internal class TouchScrollExecutor : CommandExecutorBase
{
#region Methods
Expand All @@ -30,7 +34,7 @@ protected override string DoImpl()

this.Automator.EmulatorController.PerformGesture(new ScrollGesture(startPoint, xOffset, yOffset));

return null;
return this.JsonResponse();
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
namespace WindowsUniversalAppDriver.CommandExecutors
{
#region

using WindowsUniversalAppDriver.Automator;
using WindowsUniversalAppDriver.EmulatorHelpers;

#endregion

internal class TouchSingleTapExecutor : CommandExecutorBase
{
#region Methods
Expand All @@ -14,13 +18,13 @@ protected override string DoImpl()
var elementId = Automator.GetValue<string>(this.ExecutedCommand.Parameters, "element");
if (elementId == null)
{
return null;
return this.JsonResponse();
}

var tapPoint = this.Automator.RequestElementLocation(elementId).GetValueOrDefault();
this.Automator.EmulatorController.PerformGesture(new TapGesture(tapPoint));

return null;
return this.JsonResponse();
}

#endregion
Expand Down

0 comments on commit a6f8563

Please sign in to comment.