Skip to content

Commit 24c7747

Browse files
Merge pull request #518 from SrinivasanTarget/AndroidDeviceCommands
Added Android device commands
2 parents b8fd774 + 358bdce commit 24c7747

File tree

7 files changed

+88
-2
lines changed

7 files changed

+88
-2
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ public class MobileCommand {
5656
//Android
5757
protected static final String CURRENT_ACTIVITY = "currentActivity";
5858
protected static final String END_TEST_COVERAGE = "endTestCoverage";
59+
protected static final String GET_DISPLAY_DENSITY = "getDisplayDensity";
5960
protected static final String GET_NETWORK_CONNECTION = "getNetworkConnection";
61+
protected static final String GET_SYSTEM_BARS = "getSystemBars";
62+
protected static final String IS_KEYBOARD_SHOWN = "isKeyboardShown";
6063
protected static final String IS_LOCKED = "isLocked";
6164
protected static final String LONG_PRESS_KEY_CODE = "longPressKeyCode";
6265
protected static final String OPEN_NOTIFICATIONS = "openNotifications";
@@ -102,7 +105,10 @@ private static Map<String, CommandInfo> createCommandRepository() {
102105
getC("/session/:sessionId/appium/device/current_activity"));
103106
result.put(END_TEST_COVERAGE,
104107
postC("/session/:sessionId/appium/app/end_test_coverage"));
108+
result.put(GET_DISPLAY_DENSITY, getC("/session/:sessionId/appium/device/display_density"));
105109
result.put(GET_NETWORK_CONNECTION, getC("/session/:sessionId/network_connection"));
110+
result.put(GET_SYSTEM_BARS, getC("/session/:sessionId/appium/device/system_bars"));
111+
result.put(IS_KEYBOARD_SHOWN, getC("/session/:sessionId/appium/device/is_keyboard_shown"));
106112
result.put(IS_LOCKED, postC("/session/:sessionId/appium/device/is_locked"));
107113
result.put(LONG_PRESS_KEY_CODE,
108114
postC("/session/:sessionId/appium/device/long_press_keycode"));

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
public class AndroidDriver<T extends WebElement>
4949
extends AppiumDriver<T>
5050
implements PressesKeyCode, HasNetworkConnection, PushesFiles, StartsActivity,
51-
FindsByAndroidUIAutomator<T>, LocksAndroidDevice, HasSettings {
51+
FindsByAndroidUIAutomator<T>, LocksAndroidDevice, HasSettings, HasDeviceDetails {
5252

5353
private static final String ANDROID_PLATFORM = MobilePlatform.ANDROID;
5454

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

+36
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ public class AndroidMobileCommandHelper extends MobileCommand {
6363
END_TEST_COVERAGE, prepareArguments(parameters, values));
6464
}
6565

66+
/**
67+
* This method forms a {@link java.util.Map} of parameters to
68+
* Retrieve the display density of the Android device.
69+
*
70+
* @return a key-value pair. The key is the command name. The value is a
71+
* {@link java.util.Map} command arguments.
72+
*/
73+
public static Map.Entry<String, Map<String, ?>> getDisplayDensityCommand() {
74+
return new AbstractMap.SimpleEntry<>(
75+
GET_DISPLAY_DENSITY, ImmutableMap.<String, Object>of());
76+
}
77+
6678
/**
6779
* This method forms a {@link java.util.Map} of parameters for the
6880
* getting of a network connection value.
@@ -75,6 +87,30 @@ public class AndroidMobileCommandHelper extends MobileCommand {
7587
GET_NETWORK_CONNECTION, ImmutableMap.<String, Object>of());
7688
}
7789

90+
/**
91+
* This method forms a {@link java.util.Map} of parameters to
92+
* Retrieve visibility and bounds information of the status and navigation bars.
93+
*
94+
* @return a key-value pair. The key is the command name. The value is a
95+
* {@link java.util.Map} command arguments.
96+
*/
97+
public static Map.Entry<String, Map<String, ?>> getSystemBarsCommand() {
98+
return new AbstractMap.SimpleEntry<>(
99+
GET_SYSTEM_BARS, ImmutableMap.<String, Object>of());
100+
}
101+
102+
/**
103+
* This method forms a {@link java.util.Map} of parameters for the
104+
* checking of the keyboard state (is it shown or not).
105+
*
106+
* @return a key-value pair. The key is the command name. The value is a
107+
* {@link java.util.Map} command arguments.
108+
*/
109+
public static Map.Entry<String, Map<String, ?>> isKeyboardShownCommand() {
110+
return new AbstractMap.SimpleEntry<>(
111+
IS_KEYBOARD_SHOWN, ImmutableMap.<String, Object>of());
112+
}
113+
78114
/**
79115
* This method forms a {@link java.util.Map} of parameters for the
80116
* checking of the device state (is it locked or not).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package io.appium.java_client.android;
2+
3+
import static io.appium.java_client.android.AndroidMobileCommandHelper.getDisplayDensityCommand;
4+
import static io.appium.java_client.android.AndroidMobileCommandHelper.getSystemBarsCommand;
5+
import static io.appium.java_client.android.AndroidMobileCommandHelper.isKeyboardShownCommand;
6+
7+
import io.appium.java_client.CommandExecutionHelper;
8+
import io.appium.java_client.ExecutesMethod;
9+
10+
import java.util.Map;
11+
12+
public interface HasDeviceDetails extends ExecutesMethod {
13+
/*
14+
Retrieve the display density of the Android device.
15+
*/
16+
default Long getDisplayDensity() {
17+
return CommandExecutionHelper.execute(this, getDisplayDensityCommand());
18+
}
19+
20+
/*
21+
Retrieve visibility and bounds information of the status and navigation bars.
22+
*/
23+
default Map<String, String> getSystemBars() {
24+
return CommandExecutionHelper.execute(this, getSystemBarsCommand());
25+
}
26+
27+
/**
28+
* Check if the keyboard is displayed.
29+
*
30+
* @return true if keyboard is displayed. False otherwise
31+
*/
32+
default boolean isKeyboardShown() {
33+
return CommandExecutionHelper.execute(this, isKeyboardShownCommand());
34+
}
35+
}

src/test/java/io/appium/java_client/android/AndroidDriverTest.java

+6
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,10 @@ public class AndroidDriverTest extends BaseAndroidTest {
134134
Map<?,?> map = (Map<?, ?>) driver.getSessionDetail("desired");
135135
assertNotEquals(map.size(), 0);
136136
}
137+
138+
@Test public void deviceDetailsAndKeyboardTest() {
139+
assertFalse(driver.isKeyboardShown());
140+
assertNotNull(driver.getDisplayDensity());
141+
assertNotEquals(0, driver.getSystemBars().size());
142+
}
137143
}

src/test/java/io/appium/java_client/android/AndroidSearchingTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.junit.Before;
2626
import org.junit.Test;
2727

28+
2829
public class AndroidSearchingTest extends BaseAndroidTest {
2930

3031
@Before

src/test/java/io/appium/java_client/android/BaseAndroidTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import io.appium.java_client.remote.MobileCapabilityType;
2020
import io.appium.java_client.service.local.AppiumDriverLocalService;
21+
import io.appium.java_client.service.local.AppiumServerHasNotBeenStartedLocallyException;
2122

2223
import org.junit.AfterClass;
2324
import org.junit.BeforeClass;
@@ -38,7 +39,8 @@ public class BaseAndroidTest {
3839
service.start();
3940

4041
if (service == null || !service.isRunning()) {
41-
throw new RuntimeException("An appium server node is not started!");
42+
throw new AppiumServerHasNotBeenStartedLocallyException(
43+
"An appium server node is not started!");
4244
}
4345

4446
File appDir = new File("src/test/java/io/appium/java_client");

0 commit comments

Comments
 (0)