Skip to content

Commit

Permalink
Fix dismiss keyboard command
Browse files Browse the repository at this point in the history
  • Loading branch information
sbanni committed Oct 5, 2023
1 parent 0931589 commit 67d2ac5
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

namespace UITest.Appium
{
public class AppiumAndroidVirtualKeyboardActions : AppiumVirtualKeyboardActions
{
public AppiumAndroidVirtualKeyboardActions(AppiumApp app)
: base(app)
{
}
public class AppiumAndroidVirtualKeyboardActions : AppiumVirtualKeyboardActions
{
public AppiumAndroidVirtualKeyboardActions(AppiumApp app)
: base(app)
{
}

protected override CommandResponse DismissKeyboard(IDictionary<string, object> parameters)
{
if (_app.Driver.IsKeyboardShown() == true)
{
_app.Driver.HideKeyboard();
}
return CommandResponse.SuccessEmptyResponse;
}
}
protected override CommandResponse DismissKeyboard(IDictionary<string, object> parameters)
{
if (_app.Driver.IsKeyboardShown())
{
_app.Driver.HideKeyboard();
}
return CommandResponse.SuccessEmptyResponse;
}
}
}
157 changes: 79 additions & 78 deletions src/TestUtils/src/UITest.Appium/Actions/AppiumIOSPointerActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,100 +3,101 @@

namespace UITest.Appium
{
public class AppiumIOSPointerActions : ICommandExecutionGroup
{
const string DoubleClickCommand = "doubleClick";
const string DragAndDropCommand = "dragAndDrop";
public class AppiumIOSPointerActions : ICommandExecutionGroup
{
const string DoubleClickCommand = "doubleClick";
const string DragAndDropCommand = "dragAndDrop";

readonly List<string> _commands = new()
{
DoubleClickCommand
};
readonly AppiumApp _appiumApp;
readonly List<string> _commands = new()
{
DoubleClickCommand,
DragAndDropCommand
};
readonly AppiumApp _appiumApp;

public AppiumIOSPointerActions(AppiumApp appiumApp)
{
_appiumApp = appiumApp;
}
public AppiumIOSPointerActions(AppiumApp appiumApp)
{
_appiumApp = appiumApp;
}

public bool IsCommandSupported(string commandName)
{
return _commands.Contains(commandName, StringComparer.OrdinalIgnoreCase);
}
public bool IsCommandSupported(string commandName)
{
return _commands.Contains(commandName, StringComparer.OrdinalIgnoreCase);
}

public CommandResponse Execute(string commandName, IDictionary<string, object> parameters)
{
return commandName switch
{
DoubleClickCommand => DoubleClick(parameters),
DragAndDropCommand => DragAndDrop(parameters),
_ => CommandResponse.FailedEmptyResponse,
};
}
public CommandResponse Execute(string commandName, IDictionary<string, object> parameters)
{
return commandName switch
{
DoubleClickCommand => DoubleClick(parameters),
DragAndDropCommand => DragAndDrop(parameters),
_ => CommandResponse.FailedEmptyResponse,
};
}

CommandResponse DoubleClick(IDictionary<string, object> parameters)
{
var element = GetAppiumElement(parameters["element"]);
CommandResponse DoubleClick(IDictionary<string, object> parameters)
{
var element = GetAppiumElement(parameters["element"]);

if (element != null)
{
_appiumApp.Driver.ExecuteScript("mobile: doubleTap", new Dictionary<string, object>
{
{ "elementId", element.Id },
});
}
if (element != null)
{
_appiumApp.Driver.ExecuteScript("mobile: doubleTap", new Dictionary<string, object>
{
{ "elementId", element.Id },
});
}

return CommandResponse.SuccessEmptyResponse;
}
return CommandResponse.SuccessEmptyResponse;
}

CommandResponse DragAndDrop(IDictionary<string, object> actionParams)
{
AppiumElement? sourceAppiumElement = GetAppiumElement(actionParams["sourceElement"]);
AppiumElement? destinationAppiumElement = GetAppiumElement(actionParams["destinationElement"]);
CommandResponse DragAndDrop(IDictionary<string, object> actionParams)
{
AppiumElement? sourceAppiumElement = GetAppiumElement(actionParams["sourceElement"]);
AppiumElement? destinationAppiumElement = GetAppiumElement(actionParams["destinationElement"]);

if (sourceAppiumElement != null && destinationAppiumElement != null)
{
var sourceCenterX = sourceAppiumElement.Location.X + (sourceAppiumElement.Size.Width / 2);
var sourceCenterY = sourceAppiumElement.Location.Y + (sourceAppiumElement.Size.Height / 2);
var destCenterX = destinationAppiumElement.Location.X + (destinationAppiumElement.Size.Width / 2);
var destCenterY = destinationAppiumElement.Location.Y + (destinationAppiumElement.Size.Height / 2);
if (sourceAppiumElement != null && destinationAppiumElement != null)
{
var sourceCenterX = sourceAppiumElement.Location.X + (sourceAppiumElement.Size.Width / 2);
var sourceCenterY = sourceAppiumElement.Location.Y + (sourceAppiumElement.Size.Height / 2);
var destCenterX = destinationAppiumElement.Location.X + (destinationAppiumElement.Size.Width / 2);
var destCenterY = destinationAppiumElement.Location.Y + (destinationAppiumElement.Size.Height / 2);

// iOS doesn't seem to work with the action API, so we are using script calls
_appiumApp.Driver.ExecuteScript("mobile: dragFromToWithVelocity", new Dictionary<string, object>
{
{ "pressDuration", 1 }, // Length of time to hold after click before start dragging
// iOS doesn't seem to work with the action API, so we are using script calls
_appiumApp.Driver.ExecuteScript("mobile: dragFromToWithVelocity", new Dictionary<string, object>
{
{ "pressDuration", 1 }, // Length of time to hold after click before start dragging
{ "holdDuration", .1 }, // Length of time to hold before releasing
{ "velocity", CalculateDurationForSwipe(sourceCenterX, sourceCenterY, destCenterX,destCenterY, 500) }, // How fast to drag
// from/to are absolute screen coordinates unless 'element' is specified then everything will be relative
{ "fromX", sourceCenterX},
{ "fromY", sourceCenterY },
{ "toX", destCenterX },
{ "toY", destCenterY }
});
return CommandResponse.SuccessEmptyResponse;
}
return CommandResponse.FailedEmptyResponse;
}
{ "fromY", sourceCenterY },
{ "toX", destCenterX },
{ "toY", destCenterY }
});
return CommandResponse.SuccessEmptyResponse;
}
return CommandResponse.FailedEmptyResponse;
}

static AppiumElement? GetAppiumElement(object element)
{
if (element is AppiumElement appiumElement)
{
return appiumElement;
}
else if (element is AppiumDriverElement driverElement)
{
return driverElement.AppiumElement;
}
static AppiumElement? GetAppiumElement(object element)
{
if (element is AppiumElement appiumElement)
{
return appiumElement;
}
else if (element is AppiumDriverElement driverElement)
{
return driverElement.AppiumElement;
}

return null;
}
return null;
}

static int CalculateDurationForSwipe(int startX, int startY, int endX, int endY, int pixelsPerSecond)
{
var distance = Math.Sqrt(Math.Pow(startX - endX, 2) + Math.Pow(startY - endY, 2));
static int CalculateDurationForSwipe(int startX, int startY, int endX, int endY, int pixelsPerSecond)
{
var distance = Math.Sqrt(Math.Pow(startX - endX, 2) + Math.Pow(startY - endY, 2));

return (int)(distance / (pixelsPerSecond / 1000.0));
}
}
return (int)(distance / (pixelsPerSecond / 1000.0));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@

namespace UITest.Appium
{
public class AppiumIOSVirtualKeyboardActions : AppiumVirtualKeyboardActions
{
public AppiumIOSVirtualKeyboardActions(AppiumApp app)
: base(app)
{
}
public class AppiumIOSVirtualKeyboardActions : AppiumVirtualKeyboardActions
{
public AppiumIOSVirtualKeyboardActions(AppiumApp app)
: base(app)
{
}

protected override CommandResponse DismissKeyboard(IDictionary<string, object> parameters)
{
try
{
if (_app.Driver.IsKeyboardShown() == true)
{
_app.Driver.HideKeyboard("return");
}
}
catch (InvalidElementStateException)
{
return CommandResponse.FailedEmptyResponse;
}
protected override CommandResponse DismissKeyboard(IDictionary<string, object> parameters)
{
try
{
if (_app.Driver.IsKeyboardShown())
{
_app.Driver.HideKeyboard("return");
}
}
catch (InvalidElementStateException)
{
return CommandResponse.FailedEmptyResponse;
}

return CommandResponse.SuccessEmptyResponse;
}
}
return CommandResponse.SuccessEmptyResponse;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,46 @@

namespace UITest.Appium
{
public class AppiumVirtualKeyboardActions : ICommandExecutionGroup
{
const string IsKeyboardShownCommand = "isKeyboardShown";
const string HideKeyboardCommand = "hideKeyboard";
public class AppiumVirtualKeyboardActions : ICommandExecutionGroup
{
const string IsKeyboardShownCommand = "isKeyboardShown";
const string HideKeyboardCommand = "dismissKeyboard";

protected readonly AppiumApp _app;
readonly List<string> _commands = new()
{
IsKeyboardShownCommand,
HideKeyboardCommand,
};
protected readonly AppiumApp _app;
readonly List<string> _commands = new()
{
IsKeyboardShownCommand,
HideKeyboardCommand,
};

public AppiumVirtualKeyboardActions(AppiumApp app)
{
_app = app;
}
public AppiumVirtualKeyboardActions(AppiumApp app)
{
_app = app;
}

public bool IsCommandSupported(string commandName)
{
return _commands.Contains(commandName, StringComparer.OrdinalIgnoreCase);
}
public bool IsCommandSupported(string commandName)
{
return _commands.Contains(commandName, StringComparer.OrdinalIgnoreCase);
}

public CommandResponse Execute(string commandName, IDictionary<string, object> parameters)
{
return commandName switch
{
IsKeyboardShownCommand => IsKeyboardShown(parameters),
HideKeyboardCommand => DismissKeyboard(parameters),
_ => CommandResponse.FailedEmptyResponse,
};
}
public CommandResponse Execute(string commandName, IDictionary<string, object> parameters)
{
return commandName switch
{
IsKeyboardShownCommand => IsKeyboardShown(parameters),
HideKeyboardCommand => DismissKeyboard(parameters),
_ => CommandResponse.FailedEmptyResponse,
};
}

CommandResponse IsKeyboardShown(IDictionary<string, object> parameters)
{
return new CommandResponse(_app.Driver.IsKeyboardShown(), CommandResponseResult.Success);
}
CommandResponse IsKeyboardShown(IDictionary<string, object> parameters)
{
return new CommandResponse(_app.Driver.IsKeyboardShown(), CommandResponseResult.Success);
}

protected virtual CommandResponse DismissKeyboard(IDictionary<string, object> parameters)
{
return CommandResponse.SuccessEmptyResponse;
}
}
protected virtual CommandResponse DismissKeyboard(IDictionary<string, object> parameters)
{
return CommandResponse.SuccessEmptyResponse;
}
}
}

0 comments on commit 67d2ac5

Please sign in to comment.