Skip to content

Commit 81be93b

Browse files
committed
more refactoring. moved tests around, added AppiumDriverTests
1 parent dddb062 commit 81be93b

File tree

7 files changed

+124
-242
lines changed

7 files changed

+124
-242
lines changed

src/main/java/io/appium/java_client/AppiumDriver.java

Lines changed: 100 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -17,71 +17,27 @@
1717

1818
package io.appium.java_client;
1919

20-
import static io.appium.java_client.MobileCommand.CLOSE_APP;
21-
import static io.appium.java_client.MobileCommand.COMPLEX_FIND;
22-
import static io.appium.java_client.MobileCommand.CURRENT_ACTIVITY;
23-
import static io.appium.java_client.MobileCommand.END_TEST_COVERAGE;
24-
import static io.appium.java_client.MobileCommand.GET_NETWORK_CONNECTION;
25-
import static io.appium.java_client.MobileCommand.GET_SETTINGS;
26-
import static io.appium.java_client.MobileCommand.GET_STRINGS;
27-
import static io.appium.java_client.MobileCommand.HIDE_KEYBOARD;
28-
import static io.appium.java_client.MobileCommand.INSTALL_APP;
29-
import static io.appium.java_client.MobileCommand.IS_APP_INSTALLED;
30-
import static io.appium.java_client.MobileCommand.IS_LOCKED;
31-
import static io.appium.java_client.MobileCommand.KEY_EVENT;
32-
import static io.appium.java_client.MobileCommand.LAUNCH_APP;
33-
import static io.appium.java_client.MobileCommand.LOCK;
34-
import static io.appium.java_client.MobileCommand.OPEN_NOTIFICATIONS;
35-
import static io.appium.java_client.MobileCommand.PERFORM_MULTI_TOUCH;
36-
import static io.appium.java_client.MobileCommand.PERFORM_TOUCH_ACTION;
37-
import static io.appium.java_client.MobileCommand.PULL_FILE;
38-
import static io.appium.java_client.MobileCommand.PULL_FOLDER;
39-
import static io.appium.java_client.MobileCommand.PUSH_FILE;
40-
import static io.appium.java_client.MobileCommand.REMOVE_APP;
41-
import static io.appium.java_client.MobileCommand.RESET;
42-
import static io.appium.java_client.MobileCommand.RUN_APP_IN_BACKGROUND;
43-
import static io.appium.java_client.MobileCommand.SET_NETWORK_CONNECTION;
44-
import static io.appium.java_client.MobileCommand.SET_SETTINGS;
45-
import static io.appium.java_client.MobileCommand.SET_VALUE;
46-
import static io.appium.java_client.MobileCommand.SHAKE;
47-
import static io.appium.java_client.MobileCommand.START_ACTIVITY;
20+
import com.google.common.collect.ImmutableList;
21+
import com.google.common.collect.ImmutableMap;
22+
import com.google.gson.JsonObject;
23+
import com.google.gson.JsonParser;
4824
import io.appium.java_client.internal.JsonToMobileElementConverter;
4925
import io.appium.java_client.remote.MobileCapabilityType;
26+
import org.openqa.selenium.*;
27+
import org.openqa.selenium.html5.Location;
28+
import org.openqa.selenium.html5.LocationContext;
29+
import org.openqa.selenium.remote.*;
30+
import org.openqa.selenium.remote.html5.RemoteLocationContext;
31+
import org.openqa.selenium.remote.http.HttpMethod;
5032

33+
import javax.xml.bind.DatatypeConverter;
5134
import java.net.URL;
5235
import java.util.LinkedHashSet;
5336
import java.util.List;
5437
import java.util.Map;
5538
import java.util.Set;
5639

57-
import javax.xml.bind.DatatypeConverter;
58-
59-
import org.openqa.selenium.Capabilities;
60-
import org.openqa.selenium.ContextAware;
61-
import org.openqa.selenium.Dimension;
62-
import org.openqa.selenium.Point;
63-
import org.openqa.selenium.Rotatable;
64-
import org.openqa.selenium.ScreenOrientation;
65-
import org.openqa.selenium.WebDriver;
66-
import org.openqa.selenium.WebDriverException;
67-
import org.openqa.selenium.WebElement;
68-
import org.openqa.selenium.html5.Location;
69-
import org.openqa.selenium.html5.LocationContext;
70-
import org.openqa.selenium.remote.CommandInfo;
71-
import org.openqa.selenium.remote.DesiredCapabilities;
72-
import org.openqa.selenium.remote.DriverCommand;
73-
import org.openqa.selenium.remote.ErrorHandler;
74-
import org.openqa.selenium.remote.ExecuteMethod;
75-
import org.openqa.selenium.remote.HttpCommandExecutor;
76-
import org.openqa.selenium.remote.RemoteWebDriver;
77-
import org.openqa.selenium.remote.Response;
78-
import org.openqa.selenium.remote.html5.RemoteLocationContext;
79-
import org.openqa.selenium.remote.http.HttpMethod;
80-
81-
import com.google.common.collect.ImmutableList;
82-
import com.google.common.collect.ImmutableMap;
83-
import com.google.gson.JsonObject;
84-
import com.google.gson.JsonParser;
40+
import static io.appium.java_client.MobileCommand.*;
8541

8642
public class AppiumDriver extends RemoteWebDriver implements MobileDriver,
8743
ContextAware, Rotatable, FindsByIosUIAutomation,
@@ -138,7 +94,7 @@ protected static ImmutableMap<String, Object> getCommandImmutableMap(
13894
String[] params, Object[] values) {
13995
ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
14096
for (int i=0; i < params.length; i++ ){
141-
if (_isNotNullOrEmpty(params[i])){
97+
if (_isNotNullOrEmpty(params[i]) && _isNotNullOrEmpty(values[i])){
14298
builder.put(params[i], values[i]);
14399
}
144100
}
@@ -238,6 +194,58 @@ public void resetApp() {
238194
execute(MobileCommand.RESET);
239195
}
240196

197+
198+
/**
199+
* @see InteractsWithApps#isAppInstalled(String)
200+
*/
201+
@Override
202+
public boolean isAppInstalled(String bundleId) {
203+
Response response = execute(IS_APP_INSTALLED,
204+
ImmutableMap.of("bundleId", bundleId));
205+
206+
return Boolean.parseBoolean(response.getValue().toString());
207+
}
208+
209+
/**
210+
* @see InteractsWithApps#installApp(String)
211+
*/
212+
@Override
213+
public void installApp(String appPath) {
214+
execute(INSTALL_APP, ImmutableMap.of("appPath", appPath));
215+
}
216+
217+
/**
218+
* @see InteractsWithApps#removeApp(String)
219+
*/
220+
@Override
221+
public void removeApp(String bundleId) {
222+
execute(REMOVE_APP, ImmutableMap.of("bundleId", bundleId));
223+
}
224+
225+
/**
226+
* @see InteractsWithApps#launchApp()
227+
*/
228+
@Override
229+
public void launchApp() {
230+
execute(LAUNCH_APP);
231+
}
232+
233+
/**
234+
* @see InteractsWithApps#closeApp()
235+
*/
236+
@Override
237+
public void closeApp() {
238+
execute(CLOSE_APP);
239+
}
240+
241+
/**
242+
* @see InteractsWithApps#runAppInBackground(int)
243+
*/
244+
@Override
245+
public void runAppInBackground(int seconds) {
246+
execute(RUN_APP_IN_BACKGROUND, ImmutableMap.of("seconds", seconds));
247+
}
248+
241249
/**
242250
* Send a key event to the device
243251
*
@@ -249,17 +257,13 @@ public void sendKeyEvent(int key) {
249257
execute(KEY_EVENT, getCommandImmutableMap(KEY_CODE, key));
250258
}
251259

252-
/**
253-
* Checks if a string is null, empty, or whitespace.
254-
*
255-
* @param str
256-
* String to check.
257-
*
258-
* @return True if str is not null or empty.
259-
*/
260-
protected static boolean _isNotNullOrEmpty(String str) {
261-
return str != null && !str.isEmpty() && str.trim().length() > 0;
262-
}
260+
/**
261+
* @see DeviceActionShortcuts#hideKeyboard()
262+
*/
263+
@Override
264+
public void hideKeyboard() {
265+
execute(HIDE_KEYBOARD);
266+
}
263267

264268
/**
265269
* @see InteractsWithFiles#pullFile(String)
@@ -285,22 +289,6 @@ public byte[] pullFolder(String remotePath) {
285289
return DatatypeConverter.parseBase64Binary(base64String);
286290
}
287291

288-
/**
289-
* @see DeviceActionShortcuts#hideKeyboard()
290-
*/
291-
@Override
292-
public void hideKeyboard() {
293-
execute(HIDE_KEYBOARD);
294-
}
295-
296-
/**
297-
* @see InteractsWithApps#runAppInBackground(int)
298-
*/
299-
@Override
300-
public void runAppInBackground(int seconds) {
301-
execute(RUN_APP_IN_BACKGROUND, ImmutableMap.of("seconds", seconds));
302-
}
303-
304292
/**
305293
* @see PerformsTouchActions#performTouchAction(TouchAction)
306294
*/
@@ -482,49 +470,6 @@ public void zoom(int x, int y) {
482470
multiTouch.perform();
483471
}
484472

485-
/**
486-
* @see InteractsWithApps#isAppInstalled(String)
487-
*/
488-
@Override
489-
public boolean isAppInstalled(String bundleId) {
490-
Response response = execute(IS_APP_INSTALLED,
491-
ImmutableMap.of("bundleId", bundleId));
492-
493-
return Boolean.parseBoolean(response.getValue().toString());
494-
}
495-
496-
/**
497-
* @see InteractsWithApps#installApp(String)
498-
*/
499-
@Override
500-
public void installApp(String appPath) {
501-
execute(INSTALL_APP, ImmutableMap.of("appPath", appPath));
502-
}
503-
504-
/**
505-
* @see InteractsWithApps#removeApp(String)
506-
*/
507-
@Override
508-
public void removeApp(String bundleId) {
509-
execute(REMOVE_APP, ImmutableMap.of("bundleId", bundleId));
510-
}
511-
512-
/**
513-
* @see InteractsWithApps#launchApp()
514-
*/
515-
@Override
516-
public void launchApp() {
517-
execute(LAUNCH_APP);
518-
}
519-
520-
/**
521-
* @see InteractsWithApps#closeApp()
522-
*/
523-
@Override
524-
public void closeApp() {
525-
execute(CLOSE_APP);
526-
}
527-
528473
/**
529474
* Get settings stored for this test session It's probably better to use a
530475
* convenience function, rather than use this function directly. Try finding
@@ -568,7 +513,18 @@ protected void setSetting(AppiumSetting setting, Object value) {
568513
setSettings(getCommandImmutableMap(setting.toString(), value));
569514
}
570515

571-
@Override
516+
/**
517+
* Lock the device (bring it to the lock screen) for a given number of
518+
* seconds
519+
*
520+
* @param seconds
521+
* number of seconds to lock the screen for
522+
*/
523+
public void lockScreen(int seconds) {
524+
execute(LOCK, ImmutableMap.of("seconds", seconds));
525+
}
526+
527+
@Override
572528
public WebDriver context(String name) {
573529
if (!_isNotNullOrEmpty(name)) {
574530
throw new IllegalArgumentException("Must supply a context name");
@@ -689,4 +645,20 @@ private static CommandInfo deleteC(String url) {
689645
public URL getRemoteAddress() {
690646
return remoteAddress;
691647
}
648+
649+
/**
650+
* Checks if a string is null, empty, or whitespace.
651+
*
652+
* @param str
653+
* String to check.
654+
*
655+
* @return True if str is not null or empty.
656+
*/
657+
protected static boolean _isNotNullOrEmpty(String str) {
658+
return str != null && !str.isEmpty() && str.trim().length() > 0;
659+
}
660+
661+
protected static boolean _isNotNullOrEmpty(Object ob) {
662+
return ob != null;
663+
}
692664
}

src/main/java/io/appium/java_client/DeviceActionShortcuts.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public interface DeviceActionShortcuts {
1515
* @param key code for the key pressed on the device
1616
*
1717
* @see AndroidKeyCode
18-
* @see IOSKeyCode
18+
* @see io.appium.java_client.ios.IOSKeyCode
1919
*/
2020
public void sendKeyEvent(int key);
2121

src/main/java/io/appium/java_client/android/AndroidDriver.java

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,19 @@
11
package io.appium.java_client.android;
22

3-
import static com.google.common.base.Preconditions.checkArgument;
4-
import static io.appium.java_client.MobileCommand.CURRENT_ACTIVITY;
5-
import static io.appium.java_client.MobileCommand.END_TEST_COVERAGE;
6-
import static io.appium.java_client.MobileCommand.GET_NETWORK_CONNECTION;
7-
import static io.appium.java_client.MobileCommand.GET_STRINGS;
8-
import static io.appium.java_client.MobileCommand.IS_LOCKED;
9-
import static io.appium.java_client.MobileCommand.KEY_EVENT;
10-
import static io.appium.java_client.MobileCommand.OPEN_NOTIFICATIONS;
11-
import static io.appium.java_client.MobileCommand.PUSH_FILE;
12-
import static io.appium.java_client.MobileCommand.SET_NETWORK_CONNECTION;
13-
import static io.appium.java_client.MobileCommand.START_ACTIVITY;
14-
import static io.appium.java_client.remote.MobileCapabilityType.APP_ACTIVITY;
15-
import static io.appium.java_client.remote.MobileCapabilityType.APP_PACKAGE;
16-
import static io.appium.java_client.remote.MobileCapabilityType.APP_WAIT_ACTIVITY;
17-
import static io.appium.java_client.remote.MobileCapabilityType.APP_WAIT_PACKAGE;
3+
import com.google.common.collect.ImmutableMap;
184
import io.appium.java_client.AndroidKeyCode;
195
import io.appium.java_client.AppiumDriver;
206
import io.appium.java_client.AppiumSetting;
217
import io.appium.java_client.NetworkConnectionSetting;
228
import io.appium.java_client.remote.MobilePlatform;
23-
24-
import java.net.URL;
25-
269
import org.openqa.selenium.Capabilities;
2710
import org.openqa.selenium.remote.Response;
2811

29-
import com.google.common.collect.ImmutableMap;
12+
import java.net.URL;
13+
14+
import static com.google.common.base.Preconditions.checkArgument;
15+
import static io.appium.java_client.MobileCommand.*;
16+
import static io.appium.java_client.remote.MobileCapabilityType.*;
3017

3118
public class AndroidDriver extends AppiumDriver implements
3219
AndroidDeviceActionShortcuts, HasAppStrings, HasNetworkConnection, PushesFiles,
@@ -48,7 +35,7 @@ public AndroidDriver(URL remoteAddress, Capabilities desiredCapabilities) {
4835
ANDROID_PLATFORM));
4936
}
5037

51-
/**
38+
/**
5239
* @param key
5340
* code for the key pressed on the Android device
5441
* @param metastate

0 commit comments

Comments
 (0)