Skip to content

Commit

Permalink
Merge pull request #470 from TikhomirovSergey/master
Browse files Browse the repository at this point in the history
Migration to Java 8. API with default implementation.
  • Loading branch information
TikhomirovSergey authored Sep 17, 2016
2 parents 4b2dfd2 + 3815ea7 commit 0c8c43b
Show file tree
Hide file tree
Showing 31 changed files with 465 additions and 591 deletions.
160 changes: 0 additions & 160 deletions src/main/java/io/appium/java_client/AppiumDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,11 @@

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

import static io.appium.java_client.MobileCommand.CLOSE_APP;
import static io.appium.java_client.MobileCommand.GET_DEVICE_TIME;
import static io.appium.java_client.MobileCommand.GET_SESSION;
import static io.appium.java_client.MobileCommand.GET_SETTINGS;
import static io.appium.java_client.MobileCommand.GET_STRINGS;
import static io.appium.java_client.MobileCommand.HIDE_KEYBOARD;
import static io.appium.java_client.MobileCommand.INSTALL_APP;
import static io.appium.java_client.MobileCommand.IS_APP_INSTALLED;
import static io.appium.java_client.MobileCommand.LAUNCH_APP;
import static io.appium.java_client.MobileCommand.PERFORM_MULTI_TOUCH;
import static io.appium.java_client.MobileCommand.PERFORM_TOUCH_ACTION;
import static io.appium.java_client.MobileCommand.PULL_FILE;
import static io.appium.java_client.MobileCommand.PULL_FOLDER;
import static io.appium.java_client.MobileCommand.REMOVE_APP;
import static io.appium.java_client.MobileCommand.RUN_APP_IN_BACKGROUND;
import static io.appium.java_client.MobileCommand.SET_SETTINGS;
import static io.appium.java_client.MobileCommand.prepareArguments;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
Expand Down Expand Up @@ -75,7 +61,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.xml.bind.DatatypeConverter;

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

@Override protected Response execute(String command) {
return super.execute(command, ImmutableMap.<String, Object>of());
}

@Override public ExecuteMethod getExecuteMethod() {
return executeMethod;
}

/**
* @see InteractsWithApps#resetApp().
*/
@Override public void resetApp() {
execute(MobileCommand.RESET);
}

/**
* @see InteractsWithApps#isAppInstalled(String).
*/
@Override public boolean isAppInstalled(String bundleId) {
Response response = execute(IS_APP_INSTALLED, ImmutableMap.of("bundleId", bundleId));

return Boolean.parseBoolean(response.getValue().toString());
}

/**
* @see InteractsWithApps#installApp(String).
*/
@Override public void installApp(String appPath) {
execute(INSTALL_APP, ImmutableMap.of("appPath", appPath));
}

/**
* @see InteractsWithApps#removeApp(String).
*/
@Override public void removeApp(String bundleId) {
execute(REMOVE_APP, ImmutableMap.of("bundleId", bundleId));
}

/**
* @see InteractsWithApps#launchApp().
*/
@Override public void launchApp() {
execute(LAUNCH_APP);
}

/**
* @see InteractsWithApps#closeApp().
*/
@Override public void closeApp() {
execute(CLOSE_APP);
}

/**
* @see InteractsWithApps#runAppInBackground(int).
*/
@Override public void runAppInBackground(int seconds) {
execute(RUN_APP_IN_BACKGROUND, ImmutableMap.of("seconds", seconds));
}

/**
* @see DeviceActionShortcuts#getDeviceTime().
*/
@Override public String getDeviceTime() {
Response response = execute(GET_DEVICE_TIME);
return response.getValue().toString();
}

/**
* @see DeviceActionShortcuts#hideKeyboard().
*/
@Override public void hideKeyboard() {
execute(HIDE_KEYBOARD);
}

/**
* @see InteractsWithFiles#pullFile(String).
*/
@Override public byte[] pullFile(String remotePath) {
Response response = execute(PULL_FILE, ImmutableMap.of("path", remotePath));
String base64String = response.getValue().toString();

return DatatypeConverter.parseBase64Binary(base64String);
}

/**
* @see InteractsWithFiles#pullFolder(String).
*/
@Override
public byte[] pullFolder(String remotePath) {
Response response = execute(PULL_FOLDER, ImmutableMap.of("path", remotePath));
String base64String = response.getValue().toString();

return DatatypeConverter.parseBase64Binary(base64String);
}

/**
* @see PerformsTouchActions#performTouchAction(TouchAction).
*/
@SuppressWarnings("rawtypes")
@Override public TouchAction performTouchAction(
TouchAction touchAction) {
ImmutableMap<String, ImmutableList> parameters = touchAction.getParameters();
execute(PERFORM_TOUCH_ACTION, parameters);
return touchAction;
}

/**
* @see PerformsTouchActions#performMultiTouchAction(MultiTouchAction).
*/
@Override
@SuppressWarnings({"rawtypes"})
public void performMultiTouchAction(
MultiTouchAction multiAction) {
ImmutableMap<String, ImmutableList> parameters = multiAction.getParameters();
execute(PERFORM_MULTI_TOUCH, parameters);
}

/**
* @see TouchShortcuts#tap(int, WebElement, int).
*/
Expand Down Expand Up @@ -604,38 +476,6 @@ protected void setSetting(AppiumSetting setting, Object value) {
locationContext.setLocation(location);
}

/**
* @return a map with localized strings defined in the app.
* @see HasAppStrings#getAppStringMap().
*/
@Override public Map<String, String> getAppStringMap() {
Response response = execute(GET_STRINGS);
return (Map<String, String>) response.getValue();
}

/**
* @param language strings language code.
* @return a map with localized strings defined in the app.
* @see HasAppStrings#getAppStringMap(String).
*/
@Override public Map<String, String> getAppStringMap(String language) {
Response response = execute(GET_STRINGS, prepareArguments("language", language));
return (Map<String, String>) response.getValue();
}

/**
* @param language strings language code.
* @param stringFile strings filename.
* @return a map with localized strings defined in the app.
* @see HasAppStrings#getAppStringMap(String, String).
*/
@Override public Map<String, String> getAppStringMap(String language, String stringFile) {
String[] parameters = new String[] {"language", "stringFile"};
Object[] values = new Object[] {language, stringFile};
Response response = execute(GET_STRINGS, prepareArguments(parameters, values));
return (Map<String, String>) response.getValue();
}

private TouchAction createTap(WebElement element, int duration) {
TouchAction tap = new TouchAction(this);
return tap.press(element).waitAction(duration).release();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@

public final class CommandExecutionHelper {

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

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

private static <T extends Object> T handleResponse(Response responce) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.appium.java_client;

import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriverException;
Expand All @@ -40,6 +41,10 @@ public DefaultGenericMobileDriver(CommandExecutor executor, Capabilities desired
return super.execute(driverCommand, parameters);
}

@Override public Response execute(String command) {
return super.execute(command, ImmutableMap.<String, Object>of());
}

@Override public List findElements(By by) {
return super.findElements(by);
}
Expand Down Expand Up @@ -138,20 +143,6 @@ public List findElementsByXPath(String using) {
return super.findElementsByXPath(using);
}

/**
* @throws WebDriverException This method is not applicable with browser/webview UI.
*/
@Override public T findElementByAccessibilityId(String using) throws WebDriverException {
return (T) findElement(MobileSelector.ACCESSIBILITY.toString(), using);
}

/**
* @throws WebDriverException This method is not applicable with browser/webview UI.
*/
@Override public List findElementsByAccessibilityId(String using) throws WebDriverException {
return (List<T>) findElements(MobileSelector.ACCESSIBILITY.toString(), using);
}

/**
* Mouse doesn't work on mobile devices and emulators.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.appium.java_client;

import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
Expand All @@ -27,12 +28,16 @@

@SuppressWarnings({"unchecked", "rawtypes"})
abstract class DefaultGenericMobileElement<T extends WebElement> extends RemoteWebElement
implements FindsByAccessibilityId<T>, TouchableElement<T> {
implements TouchableElement<T> {

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

@Override public Response execute(String command) {
return super.execute(command, ImmutableMap.<String, Object>of());
}

@Override public List findElements(By by) {
return super.findElements(by);
}
Expand Down Expand Up @@ -131,14 +136,6 @@ public List findElementsByXPath(String using) {
return super.findElementsByXPath(using);
}

@Override public T findElementByAccessibilityId(String using) {
return (T) findElement(MobileSelector.ACCESSIBILITY.toString(), using);
}

@Override public List findElementsByAccessibilityId(String using) {
return findElements(MobileSelector.ACCESSIBILITY.toString(), using);
}

/**
* @throws WebDriverException because it may not work against native app UI.
*/
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/io/appium/java_client/DeviceActionShortcuts.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,29 @@

package io.appium.java_client;

import static io.appium.java_client.MobileCommand.GET_DEVICE_TIME;
import static io.appium.java_client.MobileCommand.HIDE_KEYBOARD;

public interface DeviceActionShortcuts {
import org.openqa.selenium.remote.Response;

public interface DeviceActionShortcuts extends ExecutesMethod {

/**
* Hides the keyboard if it is showing.
* On iOS, there are multiple strategies for hiding the keyboard.
* Defaults to the "tapOutside" strategy (taps outside the keyboard).
* Switch to using hideKeyboard(HideKeyboardStrategy.PRESS_KEY, "Done") if this doesn't work.
*/
void hideKeyboard();
default void hideKeyboard() {
execute(HIDE_KEYBOARD);
}

/*
Gets device date and time for both iOS(Supports only real device) and Android devices
*/
String getDeviceTime();
default String getDeviceTime() {
Response response = execute(GET_DEVICE_TIME);
return response.getValue().toString();
}

}
40 changes: 40 additions & 0 deletions src/main/java/io/appium/java_client/ExecutesMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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;

import org.openqa.selenium.remote.Response;

import java.util.Map;

public interface ExecutesMethod {
/**
* Executes JSONWP command and returns a response
*
* @param driverCommand a JSONWP command
* @param parameters map of command parameters
* @return a result response
*/
Response execute(String driverCommand, Map<String, ?> parameters);

/**
* Executes JSONWP command and returns a response
*
* @param driverCommand a JSONWP command
* @return a result response
*/
Response execute(String driverCommand);
}
19 changes: 16 additions & 3 deletions src/main/java/io/appium/java_client/FindsByAccessibilityId.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,25 @@

package io.appium.java_client;

import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;

import java.util.List;

public interface FindsByAccessibilityId<T extends WebElement> {
T findElementByAccessibilityId(String using);
public interface FindsByAccessibilityId<T extends WebElement> extends FindsByFluentSelector<T> {
/**
* @throws WebDriverException This method is not
* applicable with browser/webview UI.
*/
default T findElementByAccessibilityId(String using) {
return findElement(MobileSelector.ACCESSIBILITY.toString(), using);
}

List<T> findElementsByAccessibilityId(String using);
/**
* @throws WebDriverException This method is not
* applicable with browser/webview UI.
*/
default List<T> findElementsByAccessibilityId(String using) {
return findElements(MobileSelector.ACCESSIBILITY.toString(), using);
}
}
Loading

0 comments on commit 0c8c43b

Please sign in to comment.