Skip to content

Commit 0c8c43b

Browse files
Merge pull request #470 from TikhomirovSergey/master
Migration to Java 8. API with default implementation.
2 parents 4b2dfd2 + 3815ea7 commit 0c8c43b

31 files changed

+465
-591
lines changed

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

Lines changed: 0 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,11 @@
1919

2020
import static com.google.common.base.Preconditions.checkNotNull;
2121

22-
import static io.appium.java_client.MobileCommand.CLOSE_APP;
23-
import static io.appium.java_client.MobileCommand.GET_DEVICE_TIME;
2422
import static io.appium.java_client.MobileCommand.GET_SESSION;
2523
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.LAUNCH_APP;
31-
import static io.appium.java_client.MobileCommand.PERFORM_MULTI_TOUCH;
32-
import static io.appium.java_client.MobileCommand.PERFORM_TOUCH_ACTION;
33-
import static io.appium.java_client.MobileCommand.PULL_FILE;
34-
import static io.appium.java_client.MobileCommand.PULL_FOLDER;
35-
import static io.appium.java_client.MobileCommand.REMOVE_APP;
36-
import static io.appium.java_client.MobileCommand.RUN_APP_IN_BACKGROUND;
3724
import static io.appium.java_client.MobileCommand.SET_SETTINGS;
3825
import static io.appium.java_client.MobileCommand.prepareArguments;
3926

40-
import com.google.common.collect.ImmutableList;
4127
import com.google.common.collect.ImmutableMap;
4228
import com.google.gson.JsonObject;
4329
import com.google.gson.JsonParser;
@@ -75,7 +61,6 @@
7561
import java.util.List;
7662
import java.util.Map;
7763
import java.util.Set;
78-
import javax.xml.bind.DatatypeConverter;
7964

8065
/**
8166
* @param <T> the required type of class which implement {@link org.openqa.selenium.WebElement}.
@@ -231,123 +216,10 @@ public List<T> findElementsByXPath(String using) {
231216
return super.findElementsByAccessibilityId(using);
232217
}
233218

234-
@Override protected Response execute(String command) {
235-
return super.execute(command, ImmutableMap.<String, Object>of());
236-
}
237-
238219
@Override public ExecuteMethod getExecuteMethod() {
239220
return executeMethod;
240221
}
241222

242-
/**
243-
* @see InteractsWithApps#resetApp().
244-
*/
245-
@Override public void resetApp() {
246-
execute(MobileCommand.RESET);
247-
}
248-
249-
/**
250-
* @see InteractsWithApps#isAppInstalled(String).
251-
*/
252-
@Override public boolean isAppInstalled(String bundleId) {
253-
Response response = execute(IS_APP_INSTALLED, ImmutableMap.of("bundleId", bundleId));
254-
255-
return Boolean.parseBoolean(response.getValue().toString());
256-
}
257-
258-
/**
259-
* @see InteractsWithApps#installApp(String).
260-
*/
261-
@Override public void installApp(String appPath) {
262-
execute(INSTALL_APP, ImmutableMap.of("appPath", appPath));
263-
}
264-
265-
/**
266-
* @see InteractsWithApps#removeApp(String).
267-
*/
268-
@Override public void removeApp(String bundleId) {
269-
execute(REMOVE_APP, ImmutableMap.of("bundleId", bundleId));
270-
}
271-
272-
/**
273-
* @see InteractsWithApps#launchApp().
274-
*/
275-
@Override public void launchApp() {
276-
execute(LAUNCH_APP);
277-
}
278-
279-
/**
280-
* @see InteractsWithApps#closeApp().
281-
*/
282-
@Override public void closeApp() {
283-
execute(CLOSE_APP);
284-
}
285-
286-
/**
287-
* @see InteractsWithApps#runAppInBackground(int).
288-
*/
289-
@Override public void runAppInBackground(int seconds) {
290-
execute(RUN_APP_IN_BACKGROUND, ImmutableMap.of("seconds", seconds));
291-
}
292-
293-
/**
294-
* @see DeviceActionShortcuts#getDeviceTime().
295-
*/
296-
@Override public String getDeviceTime() {
297-
Response response = execute(GET_DEVICE_TIME);
298-
return response.getValue().toString();
299-
}
300-
301-
/**
302-
* @see DeviceActionShortcuts#hideKeyboard().
303-
*/
304-
@Override public void hideKeyboard() {
305-
execute(HIDE_KEYBOARD);
306-
}
307-
308-
/**
309-
* @see InteractsWithFiles#pullFile(String).
310-
*/
311-
@Override public byte[] pullFile(String remotePath) {
312-
Response response = execute(PULL_FILE, ImmutableMap.of("path", remotePath));
313-
String base64String = response.getValue().toString();
314-
315-
return DatatypeConverter.parseBase64Binary(base64String);
316-
}
317-
318-
/**
319-
* @see InteractsWithFiles#pullFolder(String).
320-
*/
321-
@Override
322-
public byte[] pullFolder(String remotePath) {
323-
Response response = execute(PULL_FOLDER, ImmutableMap.of("path", remotePath));
324-
String base64String = response.getValue().toString();
325-
326-
return DatatypeConverter.parseBase64Binary(base64String);
327-
}
328-
329-
/**
330-
* @see PerformsTouchActions#performTouchAction(TouchAction).
331-
*/
332-
@SuppressWarnings("rawtypes")
333-
@Override public TouchAction performTouchAction(
334-
TouchAction touchAction) {
335-
ImmutableMap<String, ImmutableList> parameters = touchAction.getParameters();
336-
execute(PERFORM_TOUCH_ACTION, parameters);
337-
return touchAction;
338-
}
339-
340-
/**
341-
* @see PerformsTouchActions#performMultiTouchAction(MultiTouchAction).
342-
*/
343-
@Override
344-
@SuppressWarnings({"rawtypes"})
345-
public void performMultiTouchAction(
346-
MultiTouchAction multiAction) {
347-
ImmutableMap<String, ImmutableList> parameters = multiAction.getParameters();
348-
execute(PERFORM_MULTI_TOUCH, parameters);
349-
}
350-
351223
/**
352224
* @see TouchShortcuts#tap(int, WebElement, int).
353225
*/
@@ -604,38 +476,6 @@ protected void setSetting(AppiumSetting setting, Object value) {
604476
locationContext.setLocation(location);
605477
}
606478

607-
/**
608-
* @return a map with localized strings defined in the app.
609-
* @see HasAppStrings#getAppStringMap().
610-
*/
611-
@Override public Map<String, String> getAppStringMap() {
612-
Response response = execute(GET_STRINGS);
613-
return (Map<String, String>) response.getValue();
614-
}
615-
616-
/**
617-
* @param language strings language code.
618-
* @return a map with localized strings defined in the app.
619-
* @see HasAppStrings#getAppStringMap(String).
620-
*/
621-
@Override public Map<String, String> getAppStringMap(String language) {
622-
Response response = execute(GET_STRINGS, prepareArguments("language", language));
623-
return (Map<String, String>) response.getValue();
624-
}
625-
626-
/**
627-
* @param language strings language code.
628-
* @param stringFile strings filename.
629-
* @return a map with localized strings defined in the app.
630-
* @see HasAppStrings#getAppStringMap(String, String).
631-
*/
632-
@Override public Map<String, String> getAppStringMap(String language, String stringFile) {
633-
String[] parameters = new String[] {"language", "stringFile"};
634-
Object[] values = new Object[] {language, stringFile};
635-
Response response = execute(GET_STRINGS, prepareArguments(parameters, values));
636-
return (Map<String, String>) response.getValue();
637-
}
638-
639479
private TouchAction createTap(WebElement element, int duration) {
640480
TouchAction tap = new TouchAction(this);
641481
return tap.press(element).waitAction(duration).release();

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@
2222

2323
public final class CommandExecutionHelper {
2424

25-
public static <T extends Object> T execute(MobileElement element,
25+
public static <T extends Object> T execute(ExecutesMethod executesMethod,
2626
Map.Entry<String, Map<String, ?>> keyValuePair) {
27-
return handleResponse(element.execute(keyValuePair.getKey(), keyValuePair.getValue()));
27+
return handleResponse(executesMethod.execute(keyValuePair.getKey(), keyValuePair.getValue()));
2828
}
2929

30-
public static <T extends Object> T execute(MobileDriver driver,
31-
Map.Entry<String, Map<String, ?>> keyValuePair) {
32-
return handleResponse(driver.execute(keyValuePair.getKey(), keyValuePair.getValue()));
30+
public static <T extends Object> T execute(ExecutesMethod executesMethod, String command) {
31+
return handleResponse(executesMethod.execute(command));
3332
}
3433

3534
private static <T extends Object> T handleResponse(Response responce) {

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

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.appium.java_client;
1818

19+
import com.google.common.collect.ImmutableMap;
1920
import org.openqa.selenium.By;
2021
import org.openqa.selenium.Capabilities;
2122
import org.openqa.selenium.WebDriverException;
@@ -40,6 +41,10 @@ public DefaultGenericMobileDriver(CommandExecutor executor, Capabilities desired
4041
return super.execute(driverCommand, parameters);
4142
}
4243

44+
@Override public Response execute(String command) {
45+
return super.execute(command, ImmutableMap.<String, Object>of());
46+
}
47+
4348
@Override public List findElements(By by) {
4449
return super.findElements(by);
4550
}
@@ -138,20 +143,6 @@ public List findElementsByXPath(String using) {
138143
return super.findElementsByXPath(using);
139144
}
140145

141-
/**
142-
* @throws WebDriverException This method is not applicable with browser/webview UI.
143-
*/
144-
@Override public T findElementByAccessibilityId(String using) throws WebDriverException {
145-
return (T) findElement(MobileSelector.ACCESSIBILITY.toString(), using);
146-
}
147-
148-
/**
149-
* @throws WebDriverException This method is not applicable with browser/webview UI.
150-
*/
151-
@Override public List findElementsByAccessibilityId(String using) throws WebDriverException {
152-
return (List<T>) findElements(MobileSelector.ACCESSIBILITY.toString(), using);
153-
}
154-
155146
/**
156147
* Mouse doesn't work on mobile devices and emulators.
157148
*/

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.appium.java_client;
1818

19+
import com.google.common.collect.ImmutableMap;
1920
import org.openqa.selenium.By;
2021
import org.openqa.selenium.WebDriverException;
2122
import org.openqa.selenium.WebElement;
@@ -27,12 +28,16 @@
2728

2829
@SuppressWarnings({"unchecked", "rawtypes"})
2930
abstract class DefaultGenericMobileElement<T extends WebElement> extends RemoteWebElement
30-
implements FindsByAccessibilityId<T>, TouchableElement<T> {
31+
implements TouchableElement<T> {
3132

3233
@Override public Response execute(String driverCommand, Map<String, ?> parameters) {
3334
return super.execute(driverCommand, parameters);
3435
}
3536

37+
@Override public Response execute(String command) {
38+
return super.execute(command, ImmutableMap.<String, Object>of());
39+
}
40+
3641
@Override public List findElements(By by) {
3742
return super.findElements(by);
3843
}
@@ -131,14 +136,6 @@ public List findElementsByXPath(String using) {
131136
return super.findElementsByXPath(using);
132137
}
133138

134-
@Override public T findElementByAccessibilityId(String using) {
135-
return (T) findElement(MobileSelector.ACCESSIBILITY.toString(), using);
136-
}
137-
138-
@Override public List findElementsByAccessibilityId(String using) {
139-
return findElements(MobileSelector.ACCESSIBILITY.toString(), using);
140-
}
141-
142139
/**
143140
* @throws WebDriverException because it may not work against native app UI.
144141
*/

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,29 @@
1616

1717
package io.appium.java_client;
1818

19+
import static io.appium.java_client.MobileCommand.GET_DEVICE_TIME;
20+
import static io.appium.java_client.MobileCommand.HIDE_KEYBOARD;
1921

20-
public interface DeviceActionShortcuts {
22+
import org.openqa.selenium.remote.Response;
23+
24+
public interface DeviceActionShortcuts extends ExecutesMethod {
2125

2226
/**
2327
* Hides the keyboard if it is showing.
2428
* On iOS, there are multiple strategies for hiding the keyboard.
2529
* Defaults to the "tapOutside" strategy (taps outside the keyboard).
2630
* Switch to using hideKeyboard(HideKeyboardStrategy.PRESS_KEY, "Done") if this doesn't work.
2731
*/
28-
void hideKeyboard();
32+
default void hideKeyboard() {
33+
execute(HIDE_KEYBOARD);
34+
}
2935

3036
/*
3137
Gets device date and time for both iOS(Supports only real device) and Android devices
3238
*/
33-
String getDeviceTime();
39+
default String getDeviceTime() {
40+
Response response = execute(GET_DEVICE_TIME);
41+
return response.getValue().toString();
42+
}
3443

3544
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* See the NOTICE file distributed with this work for additional
5+
* information regarding copyright ownership.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.appium.java_client;
18+
19+
import org.openqa.selenium.remote.Response;
20+
21+
import java.util.Map;
22+
23+
public interface ExecutesMethod {
24+
/**
25+
* Executes JSONWP command and returns a response
26+
*
27+
* @param driverCommand a JSONWP command
28+
* @param parameters map of command parameters
29+
* @return a result response
30+
*/
31+
Response execute(String driverCommand, Map<String, ?> parameters);
32+
33+
/**
34+
* Executes JSONWP command and returns a response
35+
*
36+
* @param driverCommand a JSONWP command
37+
* @return a result response
38+
*/
39+
Response execute(String driverCommand);
40+
}

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,25 @@
1616

1717
package io.appium.java_client;
1818

19+
import org.openqa.selenium.WebDriverException;
1920
import org.openqa.selenium.WebElement;
2021

2122
import java.util.List;
2223

23-
public interface FindsByAccessibilityId<T extends WebElement> {
24-
T findElementByAccessibilityId(String using);
24+
public interface FindsByAccessibilityId<T extends WebElement> extends FindsByFluentSelector<T> {
25+
/**
26+
* @throws WebDriverException This method is not
27+
* applicable with browser/webview UI.
28+
*/
29+
default T findElementByAccessibilityId(String using) {
30+
return findElement(MobileSelector.ACCESSIBILITY.toString(), using);
31+
}
2532

26-
List<T> findElementsByAccessibilityId(String using);
33+
/**
34+
* @throws WebDriverException This method is not
35+
* applicable with browser/webview UI.
36+
*/
37+
default List<T> findElementsByAccessibilityId(String using) {
38+
return findElements(MobileSelector.ACCESSIBILITY.toString(), using);
39+
}
2740
}

0 commit comments

Comments
 (0)