forked from appium/java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AndroidDriver.java
291 lines (267 loc) · 11.5 KB
/
AndroidDriver.java
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
/*
* 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.
*/
package io.appium.java_client.android;
import io.appium.java_client.AppiumClientConfig;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.CommandExecutionHelper;
import io.appium.java_client.ExecuteCDPCommand;
import io.appium.java_client.HasAppStrings;
import io.appium.java_client.HasDeviceTime;
import io.appium.java_client.HasOnScreenKeyboard;
import io.appium.java_client.HidesKeyboard;
import io.appium.java_client.InteractsWithApps;
import io.appium.java_client.LocksDevice;
import io.appium.java_client.PerformsTouchActions;
import io.appium.java_client.PullsFiles;
import io.appium.java_client.PushesFiles;
import io.appium.java_client.SupportsLegacyAppManagement;
import io.appium.java_client.android.connection.HasNetworkConnection;
import io.appium.java_client.android.geolocation.SupportsExtendedGeolocationCommands;
import io.appium.java_client.android.nativekey.PressesKey;
import io.appium.java_client.battery.HasBattery;
import io.appium.java_client.remote.SupportsContextSwitching;
import io.appium.java_client.remote.SupportsLocation;
import io.appium.java_client.remote.SupportsRotation;
import io.appium.java_client.screenrecording.CanRecordScreen;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import io.appium.java_client.ws.StringWebSocketClient;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Platform;
import org.openqa.selenium.remote.HttpCommandExecutor;
import org.openqa.selenium.remote.html5.RemoteLocationContext;
import org.openqa.selenium.remote.http.ClientConfig;
import org.openqa.selenium.remote.http.HttpClient;
import java.net.URL;
import static io.appium.java_client.android.AndroidMobileCommandHelper.endTestCoverageCommand;
import static io.appium.java_client.android.AndroidMobileCommandHelper.openNotificationsCommand;
import static io.appium.java_client.android.AndroidMobileCommandHelper.toggleLocationServicesCommand;
/**
* Android driver implementation.
*/
public class AndroidDriver extends AppiumDriver implements
PressesKey,
SupportsRotation,
SupportsContextSwitching,
SupportsLocation,
PerformsTouchActions,
HidesKeyboard,
HasDeviceTime,
PullsFiles,
InteractsWithApps,
SupportsLegacyAppManagement,
HasAppStrings,
HasNetworkConnection,
PushesFiles,
StartsActivity,
LocksDevice,
HasAndroidSettings,
HasAndroidDeviceDetails,
HasSupportedPerformanceDataType,
AuthenticatesByFinger,
HasOnScreenKeyboard,
CanRecordScreen,
SupportsSpecialEmulatorCommands,
SupportsNetworkStateManagement,
ListensToLogcatMessages,
HasAndroidClipboard,
HasBattery<AndroidBatteryInfo>,
ExecuteCDPCommand,
CanReplaceElementValue,
SupportsExtendedGeolocationCommands {
private static final String ANDROID_PLATFORM = Platform.ANDROID.name();
private StringWebSocketClient logcatClient;
/**
* Creates a new instance based on command {@code executor} and {@code capabilities}.
*
* @param executor is an instance of {@link HttpCommandExecutor}
* or class that extends it. Default commands or another vendor-specific
* commands may be specified there.
* @param capabilities take a look at {@link Capabilities}
*/
public AndroidDriver(HttpCommandExecutor executor, Capabilities capabilities) {
super(executor, ensurePlatformName(capabilities, ANDROID_PLATFORM));
}
/**
* Creates a new instance based on Appium server URL and {@code capabilities}.
*
* @param remoteAddress is the address of remotely/locally started Appium server
* @param capabilities take a look at {@link Capabilities}
*/
public AndroidDriver(URL remoteAddress, Capabilities capabilities) {
super(remoteAddress, ensurePlatformName(capabilities, ANDROID_PLATFORM));
}
/**
* Creates a new instance based on Appium server URL, HTTP client factory and {@code capabilities}.
*
* @param remoteAddress is the address of remotely/locally started Appium server
* @param httpClientFactory take a look at {@link HttpClient.Factory}
* @param capabilities take a look at {@link Capabilities}
*/
public AndroidDriver(
URL remoteAddress, HttpClient.Factory httpClientFactory, Capabilities capabilities) {
super(remoteAddress, httpClientFactory, ensurePlatformName(capabilities, ANDROID_PLATFORM));
}
/**
* Creates a new instance based on Appium driver local service and {@code capabilities}.
*
* @param service take a look at {@link AppiumDriverLocalService}
* @param capabilities take a look at {@link Capabilities}
*/
public AndroidDriver(AppiumDriverLocalService service, Capabilities capabilities) {
super(service, ensurePlatformName(capabilities, ANDROID_PLATFORM));
}
/**
* Creates a new instance based on Appium driver local service, HTTP client factory and {@code capabilities}.
*
* @param service take a look at {@link AppiumDriverLocalService}
* @param httpClientFactory take a look at {@link HttpClient.Factory}
* @param capabilities take a look at {@link Capabilities}
*/
public AndroidDriver(
AppiumDriverLocalService service, HttpClient.Factory httpClientFactory, Capabilities capabilities) {
super(service, httpClientFactory, ensurePlatformName(capabilities, ANDROID_PLATFORM));
}
/**
* Creates a new instance based on Appium service builder and {@code capabilities}.
*
* @param builder take a look at {@link AppiumServiceBuilder}
* @param capabilities take a look at {@link Capabilities}
*/
public AndroidDriver(AppiumServiceBuilder builder, Capabilities capabilities) {
super(builder, ensurePlatformName(capabilities, ANDROID_PLATFORM));
}
/**
* Creates a new instance based on Appium service builder, HTTP client factory and {@code capabilities}.
*
* @param builder take a look at {@link AppiumServiceBuilder}
* @param httpClientFactory take a look at {@link HttpClient.Factory}
* @param capabilities take a look at {@link Capabilities}
*/
public AndroidDriver(AppiumServiceBuilder builder, HttpClient.Factory httpClientFactory,
Capabilities capabilities) {
super(builder, httpClientFactory, ensurePlatformName(capabilities, ANDROID_PLATFORM));
}
/**
* Creates a new instance based on HTTP client factory and {@code capabilities}.
*
* @param httpClientFactory take a look at {@link HttpClient.Factory}
* @param capabilities take a look at {@link Capabilities}
*/
public AndroidDriver(HttpClient.Factory httpClientFactory, Capabilities capabilities) {
super(httpClientFactory, ensurePlatformName(capabilities, ANDROID_PLATFORM));
}
/**
* Creates a new instance based on the given ClientConfig and {@code capabilities}.
* The HTTP client is default client generated by {@link HttpCommandExecutor#getDefaultClientFactory}.
* For example:
*
* <pre>
*
* ClientConfig clientConfig = ClientConfig.defaultConfig()
* .baseUri(URI.create("WebDriver URL"))
* .readTimeout(Duration.ofMinutes(5));
* UiAutomator2Options options = new UiAutomator2Options();
* AndroidDriver driver = new AndroidDriver(clientConfig, options);
*
* </pre>
*
* @param clientConfig take a look at {@link ClientConfig}
* @param capabilities take a look at {@link Capabilities}
*
*/
public AndroidDriver(ClientConfig clientConfig, Capabilities capabilities) {
super(AppiumClientConfig.fromClientConfig(clientConfig), ensurePlatformName(capabilities,
ANDROID_PLATFORM));
}
/**
* Creates a new instance based on the given ClientConfig and {@code capabilities}.
* The HTTP client is default client generated by {@link HttpCommandExecutor#getDefaultClientFactory}.
* For example:
*
* <pre>
*
* AppiumClientConfig appiumClientConfig = AppiumClientConfig.defaultConfig()
* .directConnect(true)
* .baseUri(URI.create("WebDriver URL"))
* .readTimeout(Duration.ofMinutes(5));
* UiAutomator2Options options = new UiAutomator2Options();
* AndroidDriver driver = new AndroidDriver(appiumClientConfig, options);
*
* </pre>
*
* @param appiumClientConfig take a look at {@link AppiumClientConfig}
* @param capabilities take a look at {@link Capabilities}
*
*/
public AndroidDriver(AppiumClientConfig appiumClientConfig, Capabilities capabilities) {
super(appiumClientConfig, ensurePlatformName(capabilities, ANDROID_PLATFORM));
}
/**
* Creates a new instance based on {@code capabilities}.
*
* @param capabilities take a look at {@link Capabilities}
*/
public AndroidDriver(Capabilities capabilities) {
super(ensurePlatformName(capabilities, ANDROID_PLATFORM));
}
/**
* This is a special constructor used to connect to a running driver instance.
* It does not do any necessary verifications, but rather assumes the given
* driver session is already running at `remoteSessionAddress`.
* The maintenance of driver state(s) is the caller's responsibility.
* !!! This API is supposed to be used for **debugging purposes only**.
*
* @param remoteSessionAddress The address of the **running** session including the session identifier.
* @param automationName The name of the target automation.
*/
public AndroidDriver(URL remoteSessionAddress, String automationName) {
super(remoteSessionAddress, ANDROID_PLATFORM, automationName);
}
/**
* Get test-coverage data.
*
* @param intent intent to broadcast.
* @param path path to .ec file.
*/
public void endTestCoverage(String intent, String path) {
CommandExecutionHelper.execute(this, endTestCoverageCommand(intent, path));
}
/**
* Open the notification shade, on Android devices.
*/
public void openNotifications() {
CommandExecutionHelper.execute(this, openNotificationsCommand());
}
public void toggleLocationServices() {
CommandExecutionHelper.execute(this, toggleLocationServicesCommand());
}
@Override
public AndroidBatteryInfo getBatteryInfo() {
return new AndroidBatteryInfo(CommandExecutionHelper.executeScript(this, "mobile: batteryInfo"));
}
@Override
public RemoteLocationContext getLocationContext() {
return locationContext;
}
@Override
public synchronized StringWebSocketClient getLogcatClient() {
if (logcatClient == null) {
logcatClient = new StringWebSocketClient();
}
return logcatClient;
}
}