Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/main/java/io/appium/java_client/AppiumDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
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.IS_LOCKED;
import static io.appium.java_client.MobileCommand.KEY_EVENT;
import static io.appium.java_client.MobileCommand.PRESS_KEY_CODE;
import static io.appium.java_client.MobileCommand.LONG_PRESS_KEY_CODE;
import static io.appium.java_client.MobileCommand.LAUNCH_APP;
import static io.appium.java_client.MobileCommand.LOCK;
import static io.appium.java_client.MobileCommand.OPEN_NOTIFICATIONS;
Expand Down Expand Up @@ -201,11 +202,11 @@ private AppiumDriver(CommandExecutor executor, Capabilities capabilities){
super.setErrorHandler(errorHandler);
}

public AppiumDriver(URL remoteAddress, Capabilities desiredCapabilities) {
this(new AppiumCommandExecutor(
public AppiumDriver(URL remoteAddress, Capabilities desiredCapabilities) {
this(new AppiumCommandExecutor(
getMobileCommands(), remoteAddress), desiredCapabilities);
this.remoteAddress = remoteAddress;
}
}

public AppiumDriver(AppiumDriverLocalService service, Capabilities desiredCapabilities) {
this(new AppiumCommandExecutor(
Expand Down Expand Up @@ -669,8 +670,10 @@ private static ImmutableMap<String, CommandInfo> getMobileCommands(){
builder.put(RESET, postC("/session/:sessionId/appium/app/reset"))
.put(GET_STRINGS,
postC("/session/:sessionId/appium/app/strings"))
.put(KEY_EVENT,
postC("/session/:sessionId/appium/device/keyevent"))
.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,
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/io/appium/java_client/MobileCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public interface MobileCommand {

String RESET = "reset";
String GET_STRINGS = "getStrings";
String KEY_EVENT = "keyEvent";
String PRESS_KEY_CODE = "pressKeyCode";
String LONG_PRESS_KEY_CODE = "longPressKeyCode";
String CURRENT_ACTIVITY = "currentActivity";
String SET_VALUE = "setValue";
String PULL_FILE = "pullFile";
Expand All @@ -53,4 +54,5 @@ public interface MobileCommand {
String SET_SETTINGS = "setSettings";
String START_ACTIVITY = "startActivity";
String TOGGLE_LOCATION_SERVICES = "toggleLocationServices";

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface AndroidDeviceActionShortcuts extends DeviceActionShortcuts {
* @param key
* code for the key pressed on the device
*/
public void sendKeyEvent(int key);
public void pressKeyCode(int key);

/**
* Send a key event along with an Android metastate to an Android device
Expand All @@ -22,5 +22,25 @@ public interface AndroidDeviceActionShortcuts extends DeviceActionShortcuts {
* @see AndroidKeyCode
* @see AndroidKeyMetastate
*/
public void sendKeyEvent(int key, Integer metastate);
public void pressKeyCode(int key, Integer metastate);

/**
* Send a long key event to the device
*
* @param key
* code for the key pressed on the device
*/
public void longPressKeyCode(int key);

/**
* Send a long key event along with an Android metastate to an Android device
* Metastates are things like *shift* to get uppercase characters
*
* @param key code for the key pressed on the Android device
* @param metastate metastate for the keypress
*
* @see AndroidKeyCode
* @see AndroidKeyMetastate
*/
public void longPressKeyCode(int key, Integer metastate);
}
38 changes: 33 additions & 5 deletions src/main/java/io/appium/java_client/android/AndroidDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ static String UiScrollable(String uiSelector) {
* code for the key pressed on the device
*/
@Override
public void sendKeyEvent(int key) {
execute(KEY_EVENT, getCommandImmutableMap(KEY_CODE, key));
public void pressKeyCode(int key) {
execute(PRESS_KEY_CODE, getCommandImmutableMap(KEY_CODE, key));
}

/**
Expand All @@ -118,13 +118,41 @@ public void sendKeyEvent(int key) {
*
* @see AndroidKeyCode
* @see AndroidKeyMetastate
* @see AndroidDeviceActionShortcuts#sendKeyEvent(int, Integer)
* @see AndroidDeviceActionShortcuts#pressKeyCode(int, Integer)
*/
@Override
public void sendKeyEvent(int key, Integer metastate) {
public void pressKeyCode(int key, Integer metastate) {
String[] parameters = new String[] { KEY_CODE, METASTATE_PARAM };
Object[] values = new Object[] { key, metastate };
execute(KEY_EVENT, getCommandImmutableMap(parameters, values));
execute(PRESS_KEY_CODE, getCommandImmutableMap(parameters, values));
}

/**
* Send a long key event to the device
*
* @param key
* code for the long key pressed on the device
*/
@Override
public void longPressKeyCode(int key) {
execute(PRESS_KEY_CODE, getCommandImmutableMap(KEY_CODE, key));
}

/**
* @param key
* code for the long key pressed on the Android device
* @param metastate
* metastate for the long key press
*
* @see AndroidKeyCode
* @see AndroidKeyMetastate
* @see AndroidDeviceActionShortcuts#pressKeyCode(int, Integer)
*/
@Override
public void longPressKeyCode(int key, Integer metastate) {
String[] parameters = new String[] { KEY_CODE, METASTATE_PARAM };
Object[] values = new Object[] { key, metastate };
execute(PRESS_KEY_CODE, getCommandImmutableMap(parameters, values));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,18 @@ public void getStringsWithLanguageTest() {
}

@Test
public void keyEventTest() {
driver.sendKeyEvent(AndroidKeyCode.HOME);
public void pressKeyCodeTest() {
driver.pressKeyCode(AndroidKeyCode.HOME);
}

@Test
public void keyEventWithMetastateTest() {
driver.sendKeyEvent(AndroidKeyCode.SPACE, AndroidKeyMetastate.META_SHIFT_ON);
public void pressKeyCodeWithMetastateTest() {
driver.pressKeyCode(AndroidKeyCode.SPACE, AndroidKeyMetastate.META_SHIFT_ON);
}

@Test
public void longPressKeyCodeTest() {
driver.longPressKeyCode(AndroidKeyCode.HOME);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void elementGestureTest(){
e2.swipe(SwipeElementDirection.LEFT, 10, 20, 1000);
System.out.println("LEFT Right border - 10 Left border + 20");

driver.sendKeyEvent(AndroidKeyCode.BACK);
driver.pressKeyCode(AndroidKeyCode.BACK);
e2 = driver.findElementByClassName("android.widget.TextView");
e2.swipe(SwipeElementDirection.DOWN,1000);
System.out.println("DOWN");
Expand Down