Skip to content

Commit

Permalink
[UI Testing] Implement PressEnter method (#22112)
Browse files Browse the repository at this point in the history
* Implement PressEnter method on appium

* IsKeyboardShown not implemented on Windows

* Updated test
  • Loading branch information
jsuarezruiz authored Apr 29, 2024
1 parent e33d176 commit 8a54162
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/Controls/tests/UITests/Tests/Issues/Issue16386.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using NUnit.Framework;
using OpenQA.Selenium;
using UITest.Appium;
using UITest.Core;

Expand All @@ -20,13 +19,12 @@ public void HittingEnterKeySendsDone()
this.IgnoreIfPlatforms(new[]
{
TestDevice.Mac,
TestDevice.iOS,
TestDevice.Windows,
TestDevice.Windows
});

App.WaitForElement("HardwareEnterKeyEntry");
App.Tap("HardwareEnterKeyEntry");
App.SendKeys(66);
App.PressEnter();
App.WaitForElement("Success");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ protected override CommandResponse DismissKeyboard(IDictionary<string, object> p
return CommandResponse.SuccessEmptyResponse;
}

protected override CommandResponse PressEnter(IDictionary<string, object> parameters)
{
if (_app.Driver is AndroidDriver android)
{
// 66 - KEYCODE_ENTER
// More information: https://developer.android.com/reference/android/view/KeyEvent.html
android.PressKeyCode(66);
return CommandResponse.SuccessEmptyResponse;
}

return CommandResponse.FailedEmptyResponse;
}

protected override CommandResponse PressVolumeDown(IDictionary<string, object> parameters)
{
if (_app.Driver is AndroidDriver android)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ protected override CommandResponse DismissKeyboard(IDictionary<string, object> p
return CommandResponse.SuccessEmptyResponse;
}

protected override CommandResponse PressEnter(IDictionary<string, object> parameters)
{
try
{
if (_app.Driver.IsKeyboardShown())
{
_app.Driver.SwitchTo().ActiveElement().SendKeys(Keys.Return);
}
}
catch (InvalidElementStateException)
{
return CommandResponse.FailedEmptyResponse;
}

return CommandResponse.SuccessEmptyResponse;
}

protected override CommandResponse PressVolumeDown(IDictionary<string, object> parameters)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class AppiumVirtualKeyboardActions : ICommandExecutionGroup
{
const string IsKeyboardShownCommand = "isKeyboardShown";
const string HideKeyboardCommand = "dismissKeyboard";
const string PressEnterCommand = "pressEnter";
const string PressVolumeDownCommand = "pressVolumeDown";
const string PressVolumeUpCommand = "pressVolumeUp";

Expand All @@ -14,6 +15,7 @@ public class AppiumVirtualKeyboardActions : ICommandExecutionGroup
{
IsKeyboardShownCommand,
HideKeyboardCommand,
PressEnterCommand,
PressVolumeDownCommand,
PressVolumeUpCommand,
};
Expand All @@ -34,6 +36,7 @@ public CommandResponse Execute(string commandName, IDictionary<string, object> p
{
IsKeyboardShownCommand => IsKeyboardShown(parameters),
HideKeyboardCommand => DismissKeyboard(parameters),
PressEnterCommand => PressEnter(parameters),
PressVolumeDownCommand => PressVolumeDown(parameters),
PressVolumeUpCommand => PressVolumeUp(parameters),
_ => CommandResponse.FailedEmptyResponse,
Expand All @@ -50,6 +53,11 @@ protected virtual CommandResponse DismissKeyboard(IDictionary<string, object> pa
return CommandResponse.SuccessEmptyResponse;
}

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

protected virtual CommandResponse PressVolumeDown(IDictionary<string, object> parameters)
{
return CommandResponse.SuccessEmptyResponse;
Expand Down
1 change: 1 addition & 0 deletions src/TestUtils/src/UITest.Appium/AppiumWindowsApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class AppiumWindowsApp : AppiumApp, IWindowsApp
public AppiumWindowsApp(Uri remoteAddress, IConfig config)
: base(new WindowsDriver(remoteAddress, GetOptions(config)), config)
{

}

public override ApplicationState AppState
Expand Down
9 changes: 9 additions & 0 deletions src/TestUtils/src/UITest.Appium/HelperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,15 @@ public static void PressVolumeDown(this IApp app)
app.CommandExecutor.Execute("pressVolumeDown", ImmutableDictionary<string, object>.Empty);
}

/// <summary>
/// Presses the enter key in the app.
/// </summary>
/// <param name="app">Represents the main gateway to interact with an app.</param>
public static void PressEnter(this IApp app)
{
app.CommandExecutor.Execute("pressEnter", ImmutableDictionary<string, object>.Empty);
}

/// <summary>
/// Performs a left to right swipe gesture on the screen.
/// </summary>
Expand Down

0 comments on commit 8a54162

Please sign in to comment.