Skip to content

Commit 8b012e9

Browse files
committed
Merge pull request appium#113 from TikhomirovSergey/interfacesRefactor
appium#105. Act II. AppiumDriver became abstract.
2 parents 5988801 + cadd0b0 commit 8b012e9

21 files changed

+446
-290
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<dependency>
1616
<groupId>org.seleniumhq.selenium</groupId>
1717
<artifactId>selenium-java</artifactId>
18-
<version>2.42.2</version>
18+
<version>2.43.1</version>
1919
</dependency>
2020
<dependency>
2121
<groupId>junit</groupId>

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

Lines changed: 102 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,22 @@
3939

4040
import static io.appium.java_client.MobileCommand.*;
4141

42-
public class AppiumDriver extends RemoteWebDriver implements MobileDriver,
43-
ContextAware, Rotatable, FindsByIosUIAutomation,
44-
FindsByAndroidUIAutomator, FindsByAccessibilityId, LocationContext,
45-
DeviceActionShortcuts, TouchShortcuts, InteractsWithFiles, InteractsWithApps {
42+
public abstract class AppiumDriver extends RemoteWebDriver implements MobileDriver,
43+
ContextAware, Rotatable, FindsByAccessibilityId, LocationContext,
44+
DeviceActionShortcuts, TouchShortcuts, InteractsWithFiles,
45+
InteractsWithApps {
4646

4747
private final static ErrorHandler errorHandler = new ErrorHandler(
4848
new ErrorCodesMobile(), true);
4949
private URL remoteAddress;
5050
private RemoteLocationContext locationContext;
5151
private ExecuteMethod executeMethod;
5252

53-
//frequently used command parameters
53+
// frequently used command parameters
5454
protected final String KEY_CODE = "keycode";
5555
protected final String PATH = "path";
5656
private final String SETTINGS = "settings";
57-
57+
5858
/**
5959
* @param originalCapabilities
6060
* the given {@link Capabilities}
@@ -71,7 +71,7 @@ protected static Capabilities substituteMobilePlatform(
7171
}
7272

7373
/**
74-
* @param param
74+
* @param param
7575
* is a parameter name
7676
* @param value
7777
* is the parameter value
@@ -83,23 +83,25 @@ protected static ImmutableMap<String, Object> getCommandImmutableMap(
8383
builder.put(param, value);
8484
return builder.build();
8585
}
86-
86+
8787
/**
8888
*
89-
* @param params is the array with parameter names
90-
* @param values is the array with parameter values
89+
* @param params
90+
* is the array with parameter names
91+
* @param values
92+
* is the array with parameter values
9193
* @return built {@link ImmutableMap}
9294
*/
9395
protected static ImmutableMap<String, Object> getCommandImmutableMap(
9496
String[] params, Object[] values) {
9597
ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
96-
for (int i=0; i < params.length; i++ ){
97-
if (_isNotNullOrEmpty(params[i]) && _isNotNullOrEmpty(values[i])){
98+
for (int i = 0; i < params.length; i++) {
99+
if (_isNotNullOrEmpty(params[i]) && _isNotNullOrEmpty(values[i])) {
98100
builder.put(params[i], values[i]);
99101
}
100102
}
101103
return builder.build();
102-
}
104+
}
103105

104106
public AppiumDriver(URL remoteAddress, Capabilities desiredCapabilities) {
105107

@@ -194,57 +196,56 @@ public void resetApp() {
194196
execute(MobileCommand.RESET);
195197
}
196198

199+
/**
200+
* @see InteractsWithApps#isAppInstalled(String)
201+
*/
202+
@Override
203+
public boolean isAppInstalled(String bundleId) {
204+
Response response = execute(IS_APP_INSTALLED,
205+
ImmutableMap.of("bundleId", bundleId));
206+
207+
return Boolean.parseBoolean(response.getValue().toString());
208+
}
209+
210+
/**
211+
* @see InteractsWithApps#installApp(String)
212+
*/
213+
@Override
214+
public void installApp(String appPath) {
215+
execute(INSTALL_APP, ImmutableMap.of("appPath", appPath));
216+
}
197217

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-
}
218+
/**
219+
* @see InteractsWithApps#removeApp(String)
220+
*/
221+
@Override
222+
public void removeApp(String bundleId) {
223+
execute(REMOVE_APP, ImmutableMap.of("bundleId", bundleId));
224+
}
225+
226+
/**
227+
* @see InteractsWithApps#launchApp()
228+
*/
229+
@Override
230+
public void launchApp() {
231+
execute(LAUNCH_APP);
232+
}
233+
234+
/**
235+
* @see InteractsWithApps#closeApp()
236+
*/
237+
@Override
238+
public void closeApp() {
239+
execute(CLOSE_APP);
240+
}
241+
242+
/**
243+
* @see InteractsWithApps#runAppInBackground(int)
244+
*/
245+
@Override
246+
public void runAppInBackground(int seconds) {
247+
execute(RUN_APP_IN_BACKGROUND, ImmutableMap.of("seconds", seconds));
248+
}
248249

249250
/**
250251
* Send a key event to the device
@@ -257,13 +258,13 @@ public void sendKeyEvent(int key) {
257258
execute(KEY_EVENT, getCommandImmutableMap(KEY_CODE, key));
258259
}
259260

260-
/**
261-
* @see DeviceActionShortcuts#hideKeyboard()
262-
*/
263-
@Override
264-
public void hideKeyboard() {
265-
execute(HIDE_KEYBOARD);
266-
}
261+
/**
262+
* @see DeviceActionShortcuts#hideKeyboard()
263+
*/
264+
@Override
265+
public void hideKeyboard() {
266+
execute(HIDE_KEYBOARD);
267+
}
267268

268269
/**
269270
* @see InteractsWithFiles#pullFile(String)
@@ -277,8 +278,8 @@ public byte[] pullFile(String remotePath) {
277278
return DatatypeConverter.parseBase64Binary(base64String);
278279
}
279280

280-
/**
281-
* @see InteractsWithFiles#pullFolder(String)
281+
/**
282+
* @see InteractsWithFiles#pullFolder(String)
282283
*/
283284
@Override
284285
public byte[] pullFolder(String remotePath) {
@@ -315,7 +316,7 @@ public void performMultiTouchAction(MultiTouchAction multiAction) {
315316
}
316317

317318
/**
318-
*@see TouchShortcuts#tap(int, WebElement, int)
319+
* @see TouchShortcuts#tap(int, WebElement, int)
319320
*/
320321
@Override
321322
public void tap(int fingers, WebElement element, int duration) {
@@ -398,8 +399,8 @@ public void pinch(int x, int y) {
398399
}
399400

400401
/**
401-
* @see TouchShortcuts#zoom(WebElement)
402-
*/
402+
* @see TouchShortcuts#zoom(WebElement)
403+
*/
403404
@Override
404405
public void zoom(WebElement el) {
405406
MultiTouchAction multiTouch = new MultiTouchAction(this);
@@ -479,18 +480,18 @@ protected void setSetting(AppiumSetting setting, Object value) {
479480
setSettings(getCommandImmutableMap(setting.toString(), value));
480481
}
481482

482-
/**
483-
* Lock the device (bring it to the lock screen) for a given number of
484-
* seconds
485-
*
486-
* @param seconds
487-
* number of seconds to lock the screen for
488-
*/
489-
public void lockScreen(int seconds) {
490-
execute(LOCK, ImmutableMap.of("seconds", seconds));
491-
}
483+
/**
484+
* Lock the device (bring it to the lock screen) for a given number of
485+
* seconds
486+
*
487+
* @param seconds
488+
* number of seconds to lock the screen for
489+
*/
490+
public void lockScreen(int seconds) {
491+
execute(LOCK, ImmutableMap.of("seconds", seconds));
492+
}
492493

493-
@Override
494+
@Override
494495
public WebDriver context(String name) {
495496
if (!_isNotNullOrEmpty(name)) {
496497
throw new IllegalArgumentException("Must supply a context name");
@@ -545,26 +546,6 @@ public ScreenOrientation getOrientation() {
545546
}
546547
}
547548

548-
@Override
549-
public WebElement findElementByIosUIAutomation(String using) {
550-
return findElement("-ios uiautomation", using);
551-
}
552-
553-
@Override
554-
public List<WebElement> findElementsByIosUIAutomation(String using) {
555-
return findElements("-ios uiautomation", using);
556-
}
557-
558-
@Override
559-
public WebElement findElementByAndroidUIAutomator(String using) {
560-
return findElement("-android uiautomator", using);
561-
}
562-
563-
@Override
564-
public List<WebElement> findElementsByAndroidUIAutomator(String using) {
565-
return findElements("-android uiautomator", using);
566-
}
567-
568549
@Override
569550
public WebElement findElementByAccessibilityId(String using) {
570551
return findElement("accessibility id", using);
@@ -612,19 +593,19 @@ public URL getRemoteAddress() {
612593
return remoteAddress;
613594
}
614595

615-
/**
616-
* Checks if a string is null, empty, or whitespace.
617-
*
618-
* @param str
619-
* String to check.
620-
*
621-
* @return True if str is not null or empty.
622-
*/
623-
protected static boolean _isNotNullOrEmpty(String str) {
624-
return str != null && !str.isEmpty() && str.trim().length() > 0;
625-
}
626-
627-
protected static boolean _isNotNullOrEmpty(Object ob) {
628-
return ob != null;
629-
}
596+
/**
597+
* Checks if a string is null, empty, or whitespace.
598+
*
599+
* @param str
600+
* String to check.
601+
*
602+
* @return True if str is not null or empty.
603+
*/
604+
protected static boolean _isNotNullOrEmpty(String str) {
605+
return str != null && !str.isEmpty() && str.trim().length() > 0;
606+
}
607+
608+
protected static boolean _isNotNullOrEmpty(Object ob) {
609+
return ob != null;
610+
}
630611
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.appium.java_client;
22

3+
34
public interface DeviceActionShortcuts {
45

56
/**
@@ -14,7 +15,7 @@ public interface DeviceActionShortcuts {
1415
*
1516
* @param key code for the key pressed on the device
1617
*
17-
* @see AndroidKeyCode
18+
* @see io.appium.java_client.android.AndroidKeyCode
1819
* @see io.appium.java_client.ios.IOSKeyCode
1920
*/
2021
public void sendKeyEvent(int key);

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.appium.java_client.android;
22

3-
import io.appium.java_client.AndroidKeyCode;
43
import io.appium.java_client.DeviceActionShortcuts;
54

65
public interface AndroidDeviceActionShortcuts extends DeviceActionShortcuts {

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
package io.appium.java_client.android;
22

33
import com.google.common.collect.ImmutableMap;
4-
import io.appium.java_client.AndroidKeyCode;
4+
55
import io.appium.java_client.AppiumDriver;
66
import io.appium.java_client.AppiumSetting;
7+
import io.appium.java_client.FindsByAndroidUIAutomator;
78
import io.appium.java_client.NetworkConnectionSetting;
89
import io.appium.java_client.remote.MobilePlatform;
10+
911
import org.openqa.selenium.Capabilities;
12+
import org.openqa.selenium.WebElement;
1013
import org.openqa.selenium.remote.Response;
1114

1215
import java.net.URL;
16+
import java.util.List;
1317

1418
import static com.google.common.base.Preconditions.checkArgument;
1519
import static io.appium.java_client.MobileCommand.*;
1620
import static io.appium.java_client.remote.MobileCapabilityType.*;
1721

1822
public class AndroidDriver extends AppiumDriver implements
1923
AndroidDeviceActionShortcuts, HasAppStrings, HasNetworkConnection, PushesFiles,
20-
StartsActivity {
24+
StartsActivity, FindsByAndroidUIAutomator {
2125

2226
private static final String ANDROID_PLATFORM = MobilePlatform.ANDROID;
2327

@@ -223,5 +227,15 @@ public boolean isLocked() {
223227
public void ignoreUnimportantViews(Boolean compress) {
224228
setSetting(AppiumSetting.IGNORE_UNIMPORTANT_VIEWS, compress);
225229
}
230+
231+
@Override
232+
public WebElement findElementByAndroidUIAutomator(String using) {
233+
return findElement("-android uiautomator", using);
234+
}
235+
236+
@Override
237+
public List<WebElement> findElementsByAndroidUIAutomator(String using) {
238+
return findElements("-android uiautomator", using);
239+
}
226240

227241
}

0 commit comments

Comments
 (0)