Skip to content

Commit

Permalink
Merge pull request #445 from TikhomirovSergey/convenient_commands
Browse files Browse the repository at this point in the history
Convenient access to commands
  • Loading branch information
TikhomirovSergey authored Jul 29, 2016
2 parents 0e9746b + 8acd617 commit 09eede8
Show file tree
Hide file tree
Showing 9 changed files with 611 additions and 212 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.53.0</version>
<version>2.53.1</version>
<exclusions>
<exclusion>
<groupId>cglib</groupId>
Expand Down
45 changes: 5 additions & 40 deletions src/main/java/io/appium/java_client/AppiumDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
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;
Expand All @@ -46,7 +47,6 @@
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;

import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Dimension;
Expand All @@ -57,7 +57,6 @@
import org.openqa.selenium.WebElement;
import org.openqa.selenium.html5.Location;

import org.openqa.selenium.remote.CommandInfo;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.DriverCommand;
import org.openqa.selenium.remote.ErrorHandler;
Expand All @@ -67,7 +66,6 @@
import org.openqa.selenium.remote.Response;
import org.openqa.selenium.remote.html5.RemoteLocationContext;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.http.HttpMethod;
import org.openqa.selenium.remote.internal.JsonToWebElementConverter;

import java.lang.reflect.Constructor;
Expand Down Expand Up @@ -189,39 +187,6 @@ protected static Capabilities substituteMobilePlatform(Capabilities originalCapa
return dc;
}

/**
* @param param is a parameter name.
* @param value is the parameter value.
* @return built {@link ImmutableMap}.
*/
protected static ImmutableMap<String, Object> getCommandImmutableMap(String param,
Object value) {
ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
builder.put(param, value);
return builder.build();
}

/**
* @param params is the array with parameter names.
* @param values is the array with parameter values.
* @return built {@link ImmutableMap}.
*/
protected static ImmutableMap<String, Object> getCommandImmutableMap(String[] params,
Object[] values) {
ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
for (int i = 0; i < params.length; i++) {
if (!StringUtils.isBlank(params[i]) && (values[i] != null)) {
builder.put(params[i], values[i]);
}
}
return builder.build();
}

@SuppressWarnings("unused")
private static CommandInfo deleteC(String url) {
return new CommandInfo(url, HttpMethod.DELETE);
}

@Override public List<T> findElements(By by) {
return super.findElements(by);
}
Expand Down Expand Up @@ -572,7 +537,7 @@ public JsonObject getSettings() {
* @param settings Map of setting keys and values.
*/
private void setSettings(ImmutableMap<?, ?> settings) {
execute(SET_SETTINGS, getCommandImmutableMap("settings", settings));
execute(SET_SETTINGS, prepareArguments("settings", settings));
}

/**
Expand All @@ -584,7 +549,7 @@ private void setSettings(ImmutableMap<?, ?> settings) {
* @param value value of the setting.
*/
protected void setSetting(AppiumSetting setting, Object value) {
setSettings(getCommandImmutableMap(setting.toString(), value));
setSettings(prepareArguments(setting.toString(), value));
}

@Override public WebDriver context(String name) {
Expand Down Expand Up @@ -654,7 +619,7 @@ protected void setSetting(AppiumSetting setting, Object value) {
* @see HasAppStrings#getAppStringMap(String).
*/
@Override public Map<String, String> getAppStringMap(String language) {
Response response = execute(GET_STRINGS, getCommandImmutableMap("language", language));
Response response = execute(GET_STRINGS, prepareArguments("language", language));
return (Map<String, String>) response.getValue();
}

Expand All @@ -667,7 +632,7 @@ protected void setSetting(AppiumSetting setting, Object value) {
@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, getCommandImmutableMap(parameters, values));
Response response = execute(GET_STRINGS, prepareArguments(parameters, values));
return (Map<String, String>) response.getValue();
}

Expand Down
41 changes: 41 additions & 0 deletions src/main/java/io/appium/java_client/CommandExecutionHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 final class CommandExecutionHelper {

public static <T extends Object> T execute(MobileElement element,
Map.Entry<String, Map<String, ?>> keyValuePair) {
return handleResponse(element.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()));
}

private static <T extends Object> T handleResponse(Response responce) {
if (responce != null) {
return (T) responce.getValue();
}
return null;
}
}
209 changes: 134 additions & 75 deletions src/main/java/io/appium/java_client/MobileCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,103 +18,162 @@

import com.google.common.collect.ImmutableMap;

import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.remote.CommandInfo;
import org.openqa.selenium.remote.http.HttpMethod;

import java.util.HashMap;
import java.util.Map;

/**
* The repository of mobile commands defined in the Mobile JSON
* wire protocol.
*/
public class MobileCommand {
//General
protected static final String RESET = "reset";
protected static final String GET_STRINGS = "getStrings";
protected static final String SET_VALUE = "setValue";
protected static final String PULL_FILE = "pullFile";
protected static final String PULL_FOLDER = "pullFolder";
protected static final String HIDE_KEYBOARD = "hideKeyboard";
protected static final String RUN_APP_IN_BACKGROUND = "runAppInBackground";
protected static final String PERFORM_TOUCH_ACTION = "performTouchAction";
protected static final String PERFORM_MULTI_TOUCH = "performMultiTouch";
protected static final String IS_APP_INSTALLED = "isAppInstalled";
protected static final String INSTALL_APP = "installApp";
protected static final String REMOVE_APP = "removeApp";
protected static final String LAUNCH_APP = "launchApp";
protected static final String CLOSE_APP = "closeApp";
protected static final String LOCK = "lock";
protected static final String COMPLEX_FIND = "complexFind";
protected static final String GET_SETTINGS = "getSettings";
protected static final String SET_SETTINGS = "setSettings";
protected static final String GET_DEVICE_TIME = "getDeviceTime";
protected static final String GET_SESSION = "getSession";
//iOS
protected static final String SHAKE = "shake";
//Android
protected static final String CURRENT_ACTIVITY = "currentActivity";
protected static final String END_TEST_COVERAGE = "endTestCoverage";
protected static final String GET_NETWORK_CONNECTION = "getNetworkConnection";
protected static final String IS_LOCKED = "isLocked";
protected static final String LONG_PRESS_KEY_CODE = "longPressKeyCode";
protected static final String OPEN_NOTIFICATIONS = "openNotifications";
protected static final String PRESS_KEY_CODE = "pressKeyCode";
protected static final String PUSH_FILE = "pushFile";
protected static final String SET_NETWORK_CONNECTION = "setNetworkConnection";
protected static final String START_ACTIVITY = "startActivity";
protected static final String TOGGLE_LOCATION_SERVICES = "toggleLocationServices";
protected static final String UNLOCK = "unlock";
protected static final String REPLACE_VALUE = "replaceValue";

public static final String RESET = "reset";
public static final String GET_STRINGS = "getStrings";
public static final String PRESS_KEY_CODE = "pressKeyCode";
public static final String LONG_PRESS_KEY_CODE = "longPressKeyCode";
public static final String CURRENT_ACTIVITY = "currentActivity";
public static final String SET_VALUE = "setValue";
public static final String REPLACE_VALUE = "replaceValue";
public static final String PULL_FILE = "pullFile";
public static final String PUSH_FILE = "pushFile";
public static final String PULL_FOLDER = "pullFolder";
public static final String HIDE_KEYBOARD = "hideKeyboard";
public static final String RUN_APP_IN_BACKGROUND = "runAppInBackground";
public static final String PERFORM_TOUCH_ACTION = "performTouchAction";
public static final String PERFORM_MULTI_TOUCH = "performMultiTouch";
public static final String IS_APP_INSTALLED = "isAppInstalled";
public static final String INSTALL_APP = "installApp";
public static final String REMOVE_APP = "removeApp";
public static final String LAUNCH_APP = "launchApp";
public static final String CLOSE_APP = "closeApp";
public static final String END_TEST_COVERAGE = "endTestCoverage";
public static final String LOCK = "lock";
public static final String IS_LOCKED = "isLocked";
public static final String SHAKE = "shake";
public static final String COMPLEX_FIND = "complexFind";
public static final String OPEN_NOTIFICATIONS = "openNotifications";
public static final String GET_NETWORK_CONNECTION = "getNetworkConnection";
public static final String SET_NETWORK_CONNECTION = "setNetworkConnection";
public static final String GET_SETTINGS = "getSettings";
public static final String SET_SETTINGS = "setSettings";
public static final String START_ACTIVITY = "startActivity";
public static final String TOGGLE_LOCATION_SERVICES = "toggleLocationServices";
public static final String GET_DEVICE_TIME = "getDeviceTime";
public static final String UNLOCK = "unlock";
public static final String GET_SESSION = "getSession";
public static final Map<String, CommandInfo> commandRepository = getMobileCommands();
public static final Map<String, CommandInfo> commandRepository = createCommandRepository();

private static Map<String, CommandInfo> createCommandRepository() {
HashMap<String, CommandInfo> result = new HashMap<String, CommandInfo>();
result.put(RESET, postC("/session/:sessionId/appium/app/reset"));
result.put(GET_STRINGS, postC("/session/:sessionId/appium/app/strings"));
result.put(SET_VALUE, postC("/session/:sessionId/appium/element/:id/value"));
result.put(PULL_FILE, postC("/session/:sessionId/appium/device/pull_file"));
result.put(PULL_FOLDER, postC("/session/:sessionId/appium/device/pull_folder"));
result.put(HIDE_KEYBOARD, postC("/session/:sessionId/appium/device/hide_keyboard"));
result.put(RUN_APP_IN_BACKGROUND, postC("/session/:sessionId/appium/app/background"));
result.put(PERFORM_TOUCH_ACTION, postC("/session/:sessionId/touch/perform"));
result.put(PERFORM_MULTI_TOUCH, postC("/session/:sessionId/touch/multi/perform"));
result.put(IS_APP_INSTALLED, postC("/session/:sessionId/appium/device/app_installed"));
result.put(INSTALL_APP, postC("/session/:sessionId/appium/device/install_app"));
result.put(REMOVE_APP, postC("/session/:sessionId/appium/device/remove_app"));
result.put(LAUNCH_APP, postC("/session/:sessionId/appium/app/launch"));
result.put(CLOSE_APP, postC("/session/:sessionId/appium/app/close"));
result.put(LOCK, postC("/session/:sessionId/appium/device/lock"));
result.put(COMPLEX_FIND, postC("/session/:sessionId/appium/app/complex_find"));
result.put(GET_SETTINGS, getC("/session/:sessionId/appium/settings"));
result.put(SET_SETTINGS, postC("/session/:sessionId/appium/settings"));
result.put(GET_DEVICE_TIME, getC("/session/:sessionId/appium/device/system_time"));
result.put(GET_SESSION,getC("/session/:sessionId/"));
//iOS
result.put(SHAKE, postC("/session/:sessionId/appium/device/shake"));
//Android
result.put(CURRENT_ACTIVITY,
getC("/session/:sessionId/appium/device/current_activity"));
result.put(END_TEST_COVERAGE,
postC("/session/:sessionId/appium/app/end_test_coverage"));
result.put(GET_NETWORK_CONNECTION, getC("/session/:sessionId/network_connection"));
result.put(IS_LOCKED, postC("/session/:sessionId/appium/device/is_locked"));
result.put(LONG_PRESS_KEY_CODE,
postC("/session/:sessionId/appium/device/long_press_keycode"));
result.put(OPEN_NOTIFICATIONS,
postC("/session/:sessionId/appium/device/open_notifications"));
result.put(PRESS_KEY_CODE,
postC("/session/:sessionId/appium/device/press_keycode"));
result.put(PUSH_FILE, postC("/session/:sessionId/appium/device/push_file"));
result.put(SET_NETWORK_CONNECTION,
postC("/session/:sessionId/network_connection"));
result.put(START_ACTIVITY,
postC("/session/:sessionId/appium/device/start_activity"));
result.put(TOGGLE_LOCATION_SERVICES,
postC("/session/:sessionId/appium/device/toggle_location_services"));
result.put(UNLOCK, postC("/session/:sessionId/appium/device/unlock"));
result.put(REPLACE_VALUE, postC("/session/:sessionId/appium/element/:id/replace_value"));
return result;
}

/**
* This methods forms GET commands.
*
* @param url is the command URL
* @return an instance of {@link org.openqa.selenium.remote.CommandInfo}
*/
public static CommandInfo getC(String url) {
return new CommandInfo(url, HttpMethod.GET);
}

/**
* This methods forms POST commands.
*
* @param url is the command URL
* @return an instance of {@link org.openqa.selenium.remote.CommandInfo}
*/
public static CommandInfo postC(String url) {
return new CommandInfo(url, HttpMethod.POST);
}

private static Map<String, CommandInfo> getMobileCommands() {
if (commandRepository != null) {
return commandRepository;
}
/**
* This methods forms DELETE commands.
*
* @param url is the command URL
* @return an instance of {@link org.openqa.selenium.remote.CommandInfo}
*/
public static CommandInfo deleteC(String url) {
return new CommandInfo(url, HttpMethod.DELETE);
}

ImmutableMap.Builder<String, CommandInfo> builder = ImmutableMap.builder();
builder.put(RESET, postC("/session/:sessionId/appium/app/reset"))
.put(GET_STRINGS, postC("/session/:sessionId/appium/app/strings"))
.put(PRESS_KEY_CODE, postC("/session/:sessionId/appium/device/press_keycode"))
.put(LONG_PRESS_KEY_CODE, postC("/session/:sessionId/appium/device/long_press_keycode"))
.put(CURRENT_ACTIVITY, getC("/session/:sessionId/appium/device/current_activity"))
.put(SET_VALUE, postC("/session/:sessionId/appium/element/:id/value"))
.put(REPLACE_VALUE, postC("/session/:sessionId/appium/element/:id/replace_value"))
.put(PULL_FILE, postC("/session/:sessionId/appium/device/pull_file"))
.put(PULL_FOLDER, postC("/session/:sessionId/appium/device/pull_folder"))
.put(HIDE_KEYBOARD, postC("/session/:sessionId/appium/device/hide_keyboard"))
.put(PUSH_FILE, postC("/session/:sessionId/appium/device/push_file"))
.put(RUN_APP_IN_BACKGROUND, postC("/session/:sessionId/appium/app/background"))
.put(PERFORM_TOUCH_ACTION, postC("/session/:sessionId/touch/perform"))
.put(PERFORM_MULTI_TOUCH, postC("/session/:sessionId/touch/multi/perform"))
.put(IS_APP_INSTALLED, postC("/session/:sessionId/appium/device/app_installed"))
.put(INSTALL_APP, postC("/session/:sessionId/appium/device/install_app"))
.put(REMOVE_APP, postC("/session/:sessionId/appium/device/remove_app"))
.put(LAUNCH_APP, postC("/session/:sessionId/appium/app/launch"))
.put(CLOSE_APP, postC("/session/:sessionId/appium/app/close"))
.put(END_TEST_COVERAGE, postC("/session/:sessionId/appium/app/end_test_coverage"))
.put(LOCK, postC("/session/:sessionId/appium/device/lock"))
.put(IS_LOCKED, postC("/session/:sessionId/appium/device/is_locked"))
.put(SHAKE, postC("/session/:sessionId/appium/device/shake"))
.put(COMPLEX_FIND, postC("/session/:sessionId/appium/app/complex_find"))
.put(OPEN_NOTIFICATIONS, postC("/session/:sessionId/appium/device/open_notifications"))
.put(GET_NETWORK_CONNECTION, getC("/session/:sessionId/network_connection"))
.put(SET_NETWORK_CONNECTION, postC("/session/:sessionId/network_connection"))
.put(GET_SETTINGS, getC("/session/:sessionId/appium/settings"))
.put(SET_SETTINGS, postC("/session/:sessionId/appium/settings"))
.put(START_ACTIVITY, postC("/session/:sessionId/appium/device/start_activity"))
.put(TOGGLE_LOCATION_SERVICES,
postC("/session/:sessionId/appium/device/toggle_location_services"))
.put(GET_DEVICE_TIME, getC("/session/:sessionId/appium/device/system_time"))
.put(UNLOCK, postC("/session/:sessionId/appium/device/unlock"))
.put(GET_SESSION,getC("/session/:sessionId/"));
/**
* @param param is a parameter name.
* @param value is the parameter value.
* @return built {@link ImmutableMap}.
*/
protected static ImmutableMap<String, Object> prepareArguments(String param,
Object value) {
ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
builder.put(param, value);
return builder.build();
}

/**
* @param params is the array with parameter names.
* @param values is the array with parameter values.
* @return built {@link ImmutableMap}.
*/
protected static ImmutableMap<String, Object> prepareArguments(String[] params,
Object[] values) {
ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
for (int i = 0; i < params.length; i++) {
if (!StringUtils.isBlank(params[i]) && (values[i] != null)) {
builder.put(params[i], values[i]);
}
}
return builder.build();
}
}
Loading

0 comments on commit 09eede8

Please sign in to comment.