generated from jfversluis/Plugin.Maui.Feature
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathAppiumLifecycleActions.cs
152 lines (127 loc) · 4.26 KB
/
AppiumLifecycleActions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Appium.Windows;
using Plugin.Maui.UITestHelpers.Core;
namespace Plugin.Maui.UITestHelpers.Appium
{
public class AppiumLifecycleActions : ICommandExecutionGroup
{
const string LaunchAppCommand = "launchApp";
const string BackgroundAppCommand = "backgroundApp";
const string ForegroundAppCommand = "foregroundApp";
const string ResetAppCommand = "resetApp";
const string CloseAppCommand = "closeApp";
const string BackCommand = "back";
protected readonly AppiumApp _app;
readonly List<string> _commands = new()
{
LaunchAppCommand,
ForegroundAppCommand,
BackgroundAppCommand,
ResetAppCommand,
CloseAppCommand,
BackCommand
};
public AppiumLifecycleActions(AppiumApp app)
{
_app = app;
}
public bool IsCommandSupported(string commandName)
{
return _commands.Contains(commandName, StringComparer.OrdinalIgnoreCase);
}
public CommandResponse Execute(string commandName, IDictionary<string, object> parameters)
{
return commandName switch
{
LaunchAppCommand => LaunchApp(parameters),
ForegroundAppCommand => ForegroundApp(parameters),
BackgroundAppCommand => BackgroundApp(parameters),
ResetAppCommand => ResetApp(parameters),
CloseAppCommand => CloseApp(parameters),
BackCommand => Back(parameters),
_ => CommandResponse.FailedEmptyResponse,
};
}
CommandResponse LaunchApp(IDictionary<string, object> parameters)
{
if (_app?.Driver is null)
return CommandResponse.FailedEmptyResponse;
if (_app.GetTestDevice() == TestDevice.Mac)
{
_app.Driver.ExecuteScript("macos: activateApp", new Dictionary<string, object>
{
{ "bundleId", _app.GetAppId() },
});
}
else if (_app.Driver is WindowsDriver windowsDriver)
{
// Appium driver removed the LaunchApp method in 5.0.0, so we need to use the executeScript method instead
// Currently the appium-windows-driver reports the following commands as compatible:
// startRecordingScreen,stopRecordingScreen,launchApp,closeApp,deleteFile,deleteFolder,
// click,scroll,clickAndDrag,hover,keys,setClipboard,getClipboard
windowsDriver.ExecuteScript("windows: launchApp", [_app.GetAppId()]);
}
else
{
_app.Driver.ActivateApp(_app.GetAppId());
}
return CommandResponse.SuccessEmptyResponse;
}
CommandResponse ForegroundApp(IDictionary<string, object> parameters)
{
if (_app?.Driver is null)
return CommandResponse.FailedEmptyResponse;
_app.Driver.ActivateApp(_app.GetAppId());
return CommandResponse.SuccessEmptyResponse;
}
CommandResponse BackgroundApp(IDictionary<string, object> parameters)
{
if (_app?.Driver is null)
return CommandResponse.FailedEmptyResponse;
_app.Driver.BackgroundApp();
if (_app.GetTestDevice() == TestDevice.Android)
Thread.Sleep(500);
return CommandResponse.SuccessEmptyResponse;
}
CommandResponse ResetApp(IDictionary<string, object> parameters)
{
if (_app?.Driver is null)
return CommandResponse.FailedEmptyResponse;
CloseApp(parameters);
LaunchApp(parameters);
return CommandResponse.SuccessEmptyResponse;
}
CommandResponse CloseApp(IDictionary<string, object> parameters)
{
if (_app?.Driver is null)
return CommandResponse.FailedEmptyResponse;
if (_app.AppState == ApplicationState.NotRunning)
return CommandResponse.SuccessEmptyResponse;
if (_app.GetTestDevice() == TestDevice.Mac)
{
_app.Driver.ExecuteScript("macos: terminateApp", new Dictionary<string, object>
{
{ "bundleId", _app.GetAppId() },
});
}
else if (_app.Driver is WindowsDriver windowsDriver)
{
// This is still here for now, but it looks like it will get removed just like
// LaunchApp was in 5.0.0, in which case we may need to use:
// windowsDriver.ExecuteScript("windows: closeApp", [_app.GetAppId()]);
windowsDriver.CloseApp();
}
else
_app.Driver.TerminateApp(_app.GetAppId());
return CommandResponse.SuccessEmptyResponse;
}
CommandResponse Back(IDictionary<string, object> parameters)
{
if (_app?.Driver is null)
return CommandResponse.FailedEmptyResponse;
// Navigate backwards in the history, if possible.
_app.Driver.Navigate().Back();
return CommandResponse.SuccessEmptyResponse;
}
}
}