forked from appium/dotnet-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAndroidCommandExecutionHelper.cs
More file actions
326 lines (292 loc) · 15.5 KB
/
Copy pathAndroidCommandExecutionHelper.cs
File metadata and controls
326 lines (292 loc) · 15.5 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//See the NOTICE file distributed with this work for additional
//information regarding copyright ownership.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
using System;
using OpenQA.Selenium.Appium.Interfaces;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using OpenQA.Selenium.Appium.Enums;
namespace OpenQA.Selenium.Appium.Android
{
public sealed class AndroidCommandExecutionHelper : AppiumCommandExecutionHelper
{
public static string GetCurrentActivity(IExecuteMethod executeMethod)
{
return executeMethod.Execute(DriverCommand.ExecuteScript, new Dictionary<string, object> {
["script"] = "mobile:getCurrentActivity",
["args"] = Array.Empty<object>()
}).Value.ToString();
}
public static string GetCurrentPackage(IExecuteMethod executeMethod)
{
return executeMethod.Execute(DriverCommand.ExecuteScript, new Dictionary<string, object> {
["script"] = "mobile:getCurrentPackage",
["args"] = Array.Empty<object>()
}).Value.ToString();
}
#region Device Network
public static void ToggleLocationServices(IExecuteMethod executeMethod) {
executeMethod.Execute(DriverCommand.ExecuteScript, new Dictionary<string, object> {
["script"] = "mobile:toggleGps",
["args"] = Array.Empty<object>()
});
}
public static void GsmCall(IExecuteMethod executeMethod, string number, GsmCallActions gsmCallAction) {
executeMethod.Execute(DriverCommand.ExecuteScript, new Dictionary<string, object> {
["script"] = "mobile:gsmCall",
["args"] = new object[] {
new Dictionary<string, object>() {
["phoneNumber"] = number,
["action"] = gsmCallAction.ToString().ToLowerInvariant()
}
}
});
}
public static void SendSms(IExecuteMethod executeMethod, string number, string message) {
executeMethod.Execute(DriverCommand.ExecuteScript, new Dictionary<string, object> {
["script"] = "mobile:sendSms",
["args"] = new object[] {
new Dictionary<string, object>() {
["phoneNumber"] = number,
["message"] = message
}
}
});
}
public static void SetGsmStrength(IExecuteMethod executeMethod, GsmSignalStrength gsmSignalStrength)
{
executeMethod.Execute(DriverCommand.ExecuteScript, new Dictionary<string, object> {
["script"] = "mobile:gsmSignal",
["args"] = new object[] {
new Dictionary<string, object>() {
["strength"] = gsmSignalStrength,
}
}
});
}
public static void SetGsmVoice(IExecuteMethod executeMethod, GsmVoiceState gsmVoiceState)
{
executeMethod.Execute(DriverCommand.ExecuteScript, new Dictionary<string, object> {
["script"] = "mobile:gsmVoice",
["args"] = new object[] {
new Dictionary<string, object>()
{
["state"] = gsmVoiceState.ToString().ToLowerInvariant(),
}
}
});
}
#endregion
#region Device Performance
public static object[] GetPerformanceDataTypes(IExecuteMethod executeMethod) {
return executeMethod.Execute(DriverCommand.ExecuteScript, new Dictionary<string, object> {
["script"] = "mobile:getPerformanceDataTypes",
["args"] = Array.Empty<object>()
}).Value as object[];
}
private static Dictionary<string, object> CreatePerformanceDataRequest(params (string Name, object Value)[] args)
{
var argDict = new Dictionary<string, object>();
foreach (var arg in args)
{
argDict[arg.Name] = arg.Value;
}
return new Dictionary<string, object>
{
["script"] = "mobile:getPerformanceData",
["args"] = argDict.Count == 0 ? Array.Empty<object>() : new object[] { argDict }
};
}
public static object[] GetPerformanceData(IExecuteMethod executeMethod, string packageName, string dataType) =>
executeMethod.Execute(DriverCommand.ExecuteScript,
CreatePerformanceDataRequest(
(nameof(packageName), packageName),
(nameof(dataType), dataType)
)
).Value as object[];
public static object[] GetPerformanceData(IExecuteMethod executeMethod, string packageName, string dataType, int dataReadTimeout) =>
executeMethod.Execute(
DriverCommand.ExecuteScript,
CreatePerformanceDataRequest(
(nameof(packageName), packageName),
(nameof(dataType), dataType),
(nameof(dataReadTimeout), dataReadTimeout)
)
).Value as object[];
#endregion
#region Device System
public static void OpenNotifications(IExecuteMethod executeMethod)
{
executeMethod.Execute(DriverCommand.ExecuteScript, new Dictionary<string, object> {
["script"] = "mobile:openNotifications",
["args"] = Array.Empty<object>()
});
}
public static IDictionary<string, object> GetSystemBars(IExecuteMethod executeMethod)
{
return executeMethod.Execute(DriverCommand.ExecuteScript, new Dictionary<string, object> {
["script"] = "mobile:getSystemBars",
["args"] = Array.Empty<object>()
}).Value as IDictionary<string, object>;
}
public static float GetDisplayDensity(IExecuteMethod executeMethod)
{
return Convert.ToSingle(
executeMethod.Execute(DriverCommand.ExecuteScript, new Dictionary<string, object> {
["script"] = "mobile:getDisplayDensity",
["args"] = Array.Empty<object>()
}).Value);
}
#endregion
#region Activity
/// <summary>
/// Start an Android activity by providing its package name and activity name.
/// For documentation, see <see href="https://github.com/appium/appium-uiautomator2-driver#mobile-startactivity">mobile:startActivity</see>.
/// </summary>
/// <param name="executeMethod">The execute method</param>
/// <param name="intent">The full name of the activity intent to start, e.g. "com.myapp/.MyActivity"</param>
/// <param name="arguments">Optional dictionary of additional arguments. Values from this dictionary take priority over other parameters.</param>
/// <param name="user">The user ID for which the activity is started. The current user is used by default.</param>
/// <param name="wait">Set to true to block the method call until the Activity Manager's process returns the control to the system. false by default.</param>
/// <param name="stop">Set to true to force stop the target app before starting the activity. false by default.</param>
/// <param name="windowingMode">The windowing mode to launch the activity into. Check WindowConfiguration.java for possible values (constants starting with WINDOWING_MODE_).</param>
/// <param name="activityType">The activity type to launch the activity as. Check WindowConfiguration.java for possible values (constants starting with ACTIVITY_TYPE_).</param>
/// <param name="action">Action name. The actual value for the Activity Manager's -a argument.</param>
/// <param name="uri">Unified resource identifier. The actual value for the Activity Manager's -d argument.</param>
/// <param name="mimeType">Mime type. The actual value for the Activity Manager's -t argument.</param>
/// <param name="identifier">Optional identifier. The actual value for the Activity Manager's -i argument.</param>
/// <param name="categories">One or more category names. The actual value(s) for the Activity Manager's -c argument.</param>
/// <param name="component">Component name. The actual value for the Activity Manager's -n argument.</param>
/// <param name="package">Package name. The actual value for the Activity Manager's -p argument.</param>
/// <param name="extras">Optional intent arguments. Must be represented as an array of arrays, where each subarray contains two or three string items: value type, key (variable name) and the value itself. Supported value types: s (string), sn (null), z (boolean), i (integer), l (long), f (float), u (uri), cn (component name), ia (Integer[]), ial (List<Integer>), la (Long[]), lal (List<Long>), fa (Float[]), fal (List<Float>), sa (String[]), sal (List<String>).</param>
/// <param name="flags">Intent startup-specific flags as a hexadecimal string. Check Intent documentation for available flag values (constants starting with FLAG_ACTIVITY_). Flag values can be merged using logical 'or' operation, e.g. "0x10200000".</param>
public static void StartActivity(
IExecuteMethod executeMethod,
string intent,
Dictionary<string, object> arguments = null,
string user = null,
bool? wait = null,
bool? stop = null,
int? windowingMode = null,
int? activityType = null,
string action = null,
string uri = null,
string mimeType = null,
string identifier = null,
string[] categories = null,
string component = null,
string package = null,
string[][] extras = null,
string flags = null)
{
if (string.IsNullOrEmpty(intent))
{
throw new ArgumentException("Activity intent must be set to a non-empty string", nameof(intent));
}
var args = new Dictionary<string, object> { ["intent"] = intent };
if (!string.IsNullOrEmpty(user))
args["user"] = user;
if (wait.HasValue)
args["wait"] = wait.Value;
if (stop.HasValue)
args["stop"] = stop.Value;
if (windowingMode.HasValue)
args["windowingMode"] = windowingMode.Value;
if (activityType.HasValue)
args["activityType"] = activityType.Value;
if (!string.IsNullOrEmpty(action))
args["action"] = action;
if (!string.IsNullOrEmpty(uri))
args["uri"] = uri;
if (!string.IsNullOrEmpty(mimeType))
args["mimeType"] = mimeType;
if (!string.IsNullOrEmpty(identifier))
args["identifier"] = identifier;
if (categories != null && categories.Length > 0)
args["categories"] = categories;
if (!string.IsNullOrEmpty(component))
args["component"] = component;
if (!string.IsNullOrEmpty(package))
args["package"] = package;
if (extras != null && extras.Length > 0)
args["extras"] = extras;
if (!string.IsNullOrEmpty(flags))
args["flags"] = flags;
// Apply arguments dictionary last to give it priority over other parameters
if (arguments != null)
{
foreach (var kvp in arguments)
{
args[kvp.Key] = kvp.Value;
}
}
executeMethod.Execute(DriverCommand.ExecuteScript, new Dictionary<string, object>
{
["script"] = "mobile:startActivity",
["args"] = new object[] { args }
});
}
#endregion
/// <summary>
/// Install an app on the Android device using mobile: installApp script.
/// For documentation, see <see href="https://github.com/appium/appium-uiautomator2-driver?tab=readme-ov-file#mobile-installapp">mobile: installApp</see>.
/// </summary>
/// <param name="executeMethod">The execute method</param>
/// <param name="appPath">Full path to the .apk on the local filesystem or a remote URL.</param>
/// <param name="timeout">Optional timeout in milliseconds to wait for the app installation to complete. 60000ms by default.</param>
/// <param name="allowTestPackages">Optional flag to allow test packages installation. false by default.</param>
/// <param name="useSdcard">Optional flag to install the app on sdcard instead of device memory. false by default.</param>
/// <param name="grantPermissions">Optional flag to grant all permissions requested in the app manifest automatically after installation. false by default.</param>
/// <param name="replace">Optional flag to upgrade/reinstall if app is already present. true by default.</param>
/// <param name="checkVersion">Optional flag to skip installation if device has equal or greater app version. false by default.</param>
public static void InstallApp(
IExecuteMethod executeMethod,
string appPath,
int? timeout = null,
bool? allowTestPackages = null,
bool? useSdcard = null,
bool? grantPermissions = null,
bool? replace = null,
bool? checkVersion = null)
{
var args = new Dictionary<string, object> { { "appPath", appPath } };
if (timeout.HasValue)
args["timeout"] = timeout.Value;
if (allowTestPackages.HasValue)
args["allowTestPackages"] = allowTestPackages.Value;
if (useSdcard.HasValue)
args["useSdcard"] = useSdcard.Value;
if (grantPermissions.HasValue)
args["grantPermissions"] = grantPermissions.Value;
if (replace.HasValue)
args["replace"] = replace.Value;
if (checkVersion.HasValue)
args["checkVersion"] = checkVersion.Value;
executeMethod.Execute(DriverCommand.ExecuteScript, new Dictionary<string, object>
{
["script"] = "mobile: installApp",
["args"] = new object[] { args }
});
}
public static Dictionary<string, object> GetSettings(IExecuteMethod executeMethod) =>
(Dictionary<string, object>) executeMethod.Execute(AppiumDriverCommand.GetSettings).Value;
public static void SetSetting(IExecuteMethod executeMethod, string setting, object value)
{
var settings = new Dictionary<string, object>()
{[setting] = value};
var parameters = new Dictionary<string, object>()
{["settings"] = settings};
executeMethod.Execute(AppiumDriverCommand.UpdateSettings, parameters);
}
}
}