16
16
17
17
package io .appium .java_client ;
18
18
19
- import static com .google .common .base .Preconditions .checkNotNull ;
20
19
import static io .appium .java_client .remote .MobileCapabilityType .PLATFORM_NAME ;
21
- import static org .apache .commons .lang3 .StringUtils .containsIgnoreCase ;
22
- import static org .apache .commons .lang3 .StringUtils .isBlank ;
23
20
24
- import com .google .common .collect .ImmutableMap ;
25
-
26
- import io .appium .java_client .internal .CapabilityHelpers ;
27
- import io .appium .java_client .internal .JsonToMobileElementConverter ;
28
21
import io .appium .java_client .remote .AppiumCommandExecutor ;
29
22
import io .appium .java_client .remote .AppiumNewSessionCommandPayload ;
30
23
import io .appium .java_client .remote .MobileCapabilityType ;
31
24
import io .appium .java_client .service .local .AppiumDriverLocalService ;
32
25
import io .appium .java_client .service .local .AppiumServiceBuilder ;
33
- import org .openqa .selenium .By ;
34
26
import org .openqa .selenium .Capabilities ;
35
- import org .openqa .selenium .DeviceRotation ;
27
+ import org .openqa .selenium .ImmutableCapabilities ;
36
28
import org .openqa .selenium .MutableCapabilities ;
37
- import org .openqa .selenium .ScreenOrientation ;
38
29
import org .openqa .selenium .SessionNotCreatedException ;
39
- import org .openqa .selenium .WebDriver ;
40
30
import org .openqa .selenium .WebDriverException ;
41
- import org .openqa .selenium .WebElement ;
42
- import org .openqa .selenium .html5 .Location ;
43
- import org .openqa .selenium .remote .CapabilityType ;
44
- import org .openqa .selenium .remote .DesiredCapabilities ;
45
31
import org .openqa .selenium .remote .DriverCommand ;
46
32
import org .openqa .selenium .remote .ErrorHandler ;
47
33
import org .openqa .selenium .remote .ExecuteMethod ;
55
41
import java .lang .reflect .Field ;
56
42
import java .net .URL ;
57
43
import java .util .Arrays ;
58
- import java .util .LinkedHashSet ;
59
- import java .util .List ;
44
+ import java .util .Collections ;
60
45
import java .util .Map ;
61
- import java .util .Set ;
62
46
63
47
/**
64
48
* Default Appium driver implementation.
65
- *
66
- * @param <T> the required type of class which implement {@link WebElement}.
67
- * Instances of the defined type will be returned via findElement* and findElements*
68
- * Warning (!!!). Allowed types:
69
- * {@link WebElement}, {@link org.openqa.selenium.remote.RemoteWebElement},
70
- * {@link MobileElement} and its subclasses that designed
71
- * specifically for each target mobile OS (still Android and iOS)
72
49
*/
73
- @ SuppressWarnings ("unchecked" )
74
- public class AppiumDriver <T extends WebElement >
75
- extends DefaultGenericMobileDriver <T > implements ComparesImages , ExecutesDriverScript , LogsEvents , HasSettings {
50
+ public class AppiumDriver extends RemoteWebDriver implements
51
+ ExecutesMethod ,
52
+ ComparesImages ,
53
+ ExecutesDriverScript ,
54
+ LogsEvents ,
55
+ CanSetElementValue ,
56
+ HasBrowserCheck ,
57
+ HasSettings {
76
58
77
59
private static final ErrorHandler errorHandler = new ErrorHandler (new ErrorCodesMobile (), true );
78
60
// frequently used command parameters
79
61
private final URL remoteAddress ;
80
- private final RemoteLocationContext locationContext ;
62
+ protected final RemoteLocationContext locationContext ;
81
63
private final ExecuteMethod executeMethod ;
82
64
83
65
/**
@@ -94,7 +76,6 @@ public AppiumDriver(HttpCommandExecutor executor, Capabilities capabilities) {
94
76
locationContext = new RemoteLocationContext (executeMethod );
95
77
super .setErrorHandler (errorHandler );
96
78
this .remoteAddress = executor .getAddressOfRemoteServer ();
97
- this .setElementConverter (new JsonToMobileElementConverter (this ));
98
79
}
99
80
100
81
public AppiumDriver (URL remoteAddress , Capabilities desiredCapabilities ) {
@@ -147,95 +128,31 @@ public AppiumDriver(Capabilities desiredCapabilities) {
147
128
*/
148
129
protected static Capabilities updateDefaultPlatformName (Capabilities originalCapabilities ,
149
130
String defaultName ) {
150
- if (originalCapabilities .getCapability (PLATFORM_NAME ) == null ) {
151
- DesiredCapabilities dc = new DesiredCapabilities (originalCapabilities );
152
- dc .setCapability (PLATFORM_NAME , defaultName );
153
- return dc ;
154
- }
155
- return originalCapabilities ;
156
- }
157
-
158
- @ Override
159
- public List <T > findElements (By by ) {
160
- return super .findElements (by );
131
+ return originalCapabilities .getCapability (PLATFORM_NAME ) == null
132
+ ? originalCapabilities .merge (new ImmutableCapabilities (PLATFORM_NAME , defaultName ))
133
+ : originalCapabilities ;
161
134
}
162
135
163
136
@ Override
164
137
public ExecuteMethod getExecuteMethod () {
165
138
return executeMethod ;
166
139
}
167
140
168
- @ Override
169
- public WebDriver context (String name ) {
170
- checkNotNull (name , "Must supply a context name" );
171
- try {
172
- execute (DriverCommand .SWITCH_TO_CONTEXT , ImmutableMap .of ("name" , name ));
173
- return this ;
174
- } catch (WebDriverException e ) {
175
- throw new NoSuchContextException (e .getMessage (), e );
176
- }
177
- }
178
-
179
- @ Override
180
- public Set <String > getContextHandles () {
181
- Response response = execute (DriverCommand .GET_CONTEXT_HANDLES );
182
- Object value = response .getValue ();
183
- try {
184
- List <String > returnedValues = (List <String >) value ;
185
- return new LinkedHashSet <>(returnedValues );
186
- } catch (ClassCastException ex ) {
187
- throw new WebDriverException (
188
- "Returned value cannot be converted to List<String>: " + value , ex );
189
- }
190
- }
191
-
192
- @ Override
193
- public String getContext () {
194
- String contextName =
195
- String .valueOf (execute (DriverCommand .GET_CURRENT_CONTEXT_HANDLE ).getValue ());
196
- if ("null" .equalsIgnoreCase (contextName )) {
197
- return null ;
198
- }
199
- return contextName ;
200
- }
201
-
202
141
/**
203
142
* This method is used to get build version status of running Appium server.
204
143
*
205
144
* @return map containing version details
206
145
*/
207
146
public Map <String , Object > getStatus () {
147
+ //noinspection unchecked
208
148
return (Map <String , Object >) execute (DriverCommand .STATUS ).getValue ();
209
149
}
210
150
211
- @ Override
212
- public DeviceRotation rotation () {
213
- Response response = execute (DriverCommand .GET_SCREEN_ROTATION );
214
- DeviceRotation deviceRotation =
215
- new DeviceRotation ((Map <String , Number >) response .getValue ());
216
- if (deviceRotation .getX () < 0 || deviceRotation .getY () < 0 || deviceRotation .getZ () < 0 ) {
217
- throw new WebDriverException ("Unexpected orientation returned: " + deviceRotation );
218
- }
219
- return deviceRotation ;
220
- }
221
-
222
- @ Override
223
- public void rotate (DeviceRotation rotation ) {
224
- execute (DriverCommand .SET_SCREEN_ROTATION , rotation .parameters ());
225
- }
226
-
227
-
228
- @ Override
229
- public void rotate (ScreenOrientation orientation ) {
230
- execute (DriverCommand .SET_SCREEN_ORIENTATION ,
231
- ImmutableMap .of ("orientation" , orientation .value ().toUpperCase ()));
232
- }
233
-
234
151
/**
235
152
* This method is used to add custom appium commands in Appium 2.0.
236
153
*
237
154
* @param httpMethod the available {@link HttpMethod}.
238
- * @param url The url to URL template as https://www.w3.org/TR/webdriver/#endpoints.
155
+ * @param url The url to URL template as https://www.w3.org/TR/webdriver/#endpoints.
239
156
* @param methodName The name of custom appium command.
240
157
*/
241
158
public void addCommand (HttpMethod httpMethod , String url , String methodName ) {
@@ -257,51 +174,10 @@ public void addCommand(HttpMethod httpMethod, String url, String methodName) {
257
174
((AppiumCommandExecutor ) getCommandExecutor ()).refreshAdditionalCommands ();
258
175
}
259
176
260
- @ Override
261
- public ScreenOrientation getOrientation () {
262
- Response response = execute (DriverCommand .GET_SCREEN_ORIENTATION );
263
- String orientation = response .getValue ().toString ().toLowerCase ();
264
- if (orientation .equals (ScreenOrientation .LANDSCAPE .value ())) {
265
- return ScreenOrientation .LANDSCAPE ;
266
- } else if (orientation .equals (ScreenOrientation .PORTRAIT .value ())) {
267
- return ScreenOrientation .PORTRAIT ;
268
- } else {
269
- throw new WebDriverException ("Unexpected orientation returned: " + orientation );
270
- }
271
- }
272
-
273
- @ Override
274
- public Location location () {
275
- return locationContext .location ();
276
- }
277
-
278
- @ Override
279
- public void setLocation (Location location ) {
280
- locationContext .setLocation (location );
281
- }
282
-
283
177
public URL getRemoteAddress () {
284
178
return remoteAddress ;
285
179
}
286
180
287
- @ Override
288
- public boolean isBrowser () {
289
- String browserName = CapabilityHelpers .getCapability (getCapabilities (),
290
- CapabilityType .BROWSER_NAME , String .class );
291
- if (!isBlank (browserName )) {
292
- try {
293
- return (boolean ) executeScript ("return !!window.navigator;" );
294
- } catch (WebDriverException ign ) {
295
- // ignore
296
- }
297
- }
298
- try {
299
- return !containsIgnoreCase (getContext (), "NATIVE_APP" );
300
- } catch (WebDriverException e ) {
301
- return false ;
302
- }
303
- }
304
-
305
181
@ Override
306
182
protected void startSession (Capabilities capabilities ) {
307
183
Response response = execute (new AppiumNewSessionCommandPayload (capabilities ));
@@ -333,4 +209,14 @@ protected void startSession(Capabilities capabilities) {
333
209
}
334
210
setSessionId (response .getSessionId ());
335
211
}
212
+
213
+ @ Override
214
+ public Response execute (String driverCommand , Map <String , ?> parameters ) {
215
+ return super .execute (driverCommand , parameters );
216
+ }
217
+
218
+ @ Override
219
+ public Response execute (String command ) {
220
+ return super .execute (command , Collections .emptyMap ());
221
+ }
336
222
}
0 commit comments