Skip to content

Commit 3ffce8a

Browse files
committed
Merge pull request #5 from Jonahss/accessibility
accessibility id locator strategy, app reset, get app strings, send key event
2 parents 6d35898 + bb4c50b commit 3ffce8a

16 files changed

+504
-6
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
+Copyright 2014 Appium contributors
3+
+Copyright 2014 Software Freedom Conservancy
4+
+
5+
+Licensed under the Apache License, Version 2.0 (the "License");
6+
+you may not use this file except in compliance with the License.
7+
+You may obtain a copy of the License at
8+
+
9+
+ http://www.apache.org/licenses/LICENSE-2.0
10+
+
11+
+Unless required by applicable law or agreed to in writing, software
12+
+distributed under the License is distributed on an "AS IS" BASIS,
13+
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
+See the License for the specific language governing permissions and
15+
+limitations under the License.
16+
+ */
17+
18+
package io.appium.java_client;
19+
20+
/**
21+
* Some common key codes for Android Key Events
22+
*/
23+
public interface AndroidKeyCode {
24+
25+
int BACK = 4;
26+
int BACKSPACE = 67;
27+
int DEL = 67;
28+
int ENTER = 66;
29+
int HOME = 3;
30+
int MENU = 82;
31+
int SETTINGS = 176;
32+
int SPACE = 62;
33+
34+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
+Copyright 2014 Appium contributors
3+
+Copyright 2014 Software Freedom Conservancy
4+
+
5+
+Licensed under the Apache License, Version 2.0 (the "License");
6+
+you may not use this file except in compliance with the License.
7+
+You may obtain a copy of the License at
8+
+
9+
+ http://www.apache.org/licenses/LICENSE-2.0
10+
+
11+
+Unless required by applicable law or agreed to in writing, software
12+
+distributed under the License is distributed on an "AS IS" BASIS,
13+
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
+See the License for the specific language governing permissions and
15+
+limitations under the License.
16+
+ */
17+
18+
package io.appium.java_client;
19+
20+
/**
21+
* Metastates for Android Key Events
22+
*/
23+
public interface AndroidKeyMetastate {
24+
25+
int META_ALT_LEFT_ON = 16;
26+
int META_ALT_ON = 2;
27+
int META_ALT_RIGHT_ON = 32;
28+
int META_CAPS_LOCK_ON = 1048576;
29+
int META_CTRL_LEFT_ON = 8192;
30+
int META_CTRL_ON = 4096;
31+
int META_CTRL_RIGHT_ON = 16384;
32+
int META_FUNCTION_ON = 8;
33+
int META_META_LEFT_ON = 131072;
34+
int META_META_ON = 65536;
35+
int META_META_RIGHT_ON = 262144;
36+
int META_NUM_LOCK_ON = 2097152;
37+
int META_SCROLL_LOCK_ON = 4194304;
38+
int META_SHIFT_LEFT_ON = 64;
39+
int META_SHIFT_ON = 1;
40+
int META_SHIFT_RIGHT_ON = 128;
41+
int META_SYM_ON = 4;
42+
}

src/io/appium/java_client/AppiumDriver.java

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
+Copyright 2014 Appium contributors
23
+Copyright 2014 Software Freedom Conservancy
34
+
45
+Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,7 +17,6 @@
1617

1718
package io.appium.java_client;
1819

19-
2020
import com.google.common.collect.ImmutableMap;
2121
import org.openqa.selenium.*;
2222
import org.openqa.selenium.remote.*;
@@ -27,16 +27,24 @@
2727
import java.util.Map;
2828
import java.util.Set;
2929

30+
import static io.appium.java_client.MobileCommand.*;
31+
3032
public class AppiumDriver extends RemoteWebDriver implements MobileDriver, ContextAware, FindsByIosUIAutomation,
31-
FindsByAndroidUIAutomator {
33+
FindsByAndroidUIAutomator, FindsByAccessibilityId {
3234

3335
private final MobileErrorHandler errorHandler = new MobileErrorHandler();
3436

3537
public AppiumDriver(URL remoteAddress, Capabilities desiredCapabilities){
3638

3739
super(remoteAddress, desiredCapabilities);
3840

39-
ImmutableMap<String, CommandInfo> mobileCommands = ImmutableMap.<String, CommandInfo>of();
41+
ImmutableMap.Builder<String, CommandInfo> builder = ImmutableMap.builder();
42+
builder
43+
.put(RESET, postC("/session/:sessionId/appium/app/reset"))
44+
.put(GET_STRINGS, getC("/session/:sessionId/appium/app/strings"))
45+
.put(KEY_EVENT, postC("/session/:sessionId/appium/device/keyevent"))
46+
;
47+
ImmutableMap<String, CommandInfo> mobileCommands = builder.build();
4048

4149
HttpCommandExecutor mobileExecutor = new HttpCommandExecutor(mobileCommands, remoteAddress);
4250
super.setCommandExecutor(mobileExecutor);
@@ -61,6 +69,26 @@ protected Response execute(String command) {
6169
}
6270

6371

72+
public void resetApp() {
73+
execute(MobileCommand.RESET);
74+
}
75+
76+
public String getAppStrings() {
77+
Response response = execute(GET_STRINGS);
78+
return response.getValue().toString();
79+
}
80+
81+
public void sendKeyEvent(int key) {
82+
sendKeyEvent(key, null);
83+
}
84+
85+
public void sendKeyEvent(int key, Integer metastate) {
86+
ImmutableMap.Builder builder = ImmutableMap.builder();
87+
builder.put("keycode", key);
88+
if (metastate != null) { builder.put("metastate", metastate); }
89+
ImmutableMap<String, Integer> parameters = builder.build();
90+
execute(KEY_EVENT, parameters);
91+
}
6492

6593

6694
@Override
@@ -114,4 +142,26 @@ public WebElement findElementByAndroidUIAutomator(String using) {
114142
public List<WebElement> findElementsByAndroidUIAutomator(String using) {
115143
return findElements("-android uiautomator", using);
116144
}
145+
146+
@Override
147+
public WebElement findElementByAccessibilityId(String using) {
148+
return findElement("accessibility id", using);
149+
}
150+
151+
@Override
152+
public List<WebElement> findElementsByAccessibilityId(String using) {
153+
return findElements("accessibility id", using);
154+
}
155+
156+
private static CommandInfo getC(String url) {
157+
return new CommandInfo(url, HttpVerb.GET);
158+
}
159+
160+
private static CommandInfo postC(String url) {
161+
return new CommandInfo(url, HttpVerb.POST);
162+
}
163+
164+
private static CommandInfo deleteC(String url) {
165+
return new CommandInfo(url, HttpVerb.DELETE);
166+
}
117167
}

src/io/appium/java_client/ErrorCodesMobile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2014 Selenium committers
2+
Copyright 2014 Appium contributors
33
Copyright 2014 Software Freedom Conservancy
44
55
Licensed under the Apache License, Version 2.0 (the "License");
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Copyright 2014 Appium committers
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
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.WebElement;
20+
21+
import java.util.List;
22+
23+
public interface FindsByAccessibilityId {
24+
WebElement findElementByAccessibilityId(String using);
25+
26+
List<WebElement> findElementsByAccessibilityId(String using);
27+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
+Copyright 2014 Appium contributors
3+
+Copyright 2014 Software Freedom Conservancy
4+
+
5+
+Licensed under the Apache License, Version 2.0 (the "License");
6+
+you may not use this file except in compliance with the License.
7+
+You may obtain a copy of the License at
8+
+
9+
+ http://www.apache.org/licenses/LICENSE-2.0
10+
+
11+
+Unless required by applicable law or agreed to in writing, software
12+
+distributed under the License is distributed on an "AS IS" BASIS,
13+
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
+See the License for the specific language governing permissions and
15+
+limitations under the License.
16+
+ */
17+
18+
package io.appium.java_client;
19+
20+
/**
21+
* Some common key codes for Android Key Events
22+
*/
23+
public interface IOSKeyCode {
24+
25+
int BACKSPACE = 8;
26+
int ENTER = 13;
27+
int RETURN = 13;
28+
int SPACE = 32;
29+
30+
}

src/io/appium/java_client/MobileBy.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ public static By AndroidUIAutomator(final String uiautomatorText) {
2828
return new ByAndroidUIAutomator(uiautomatorText);
2929
}
3030

31+
public static By AccessibilityId(final String id) {
32+
if (id == null) {
33+
throw new IllegalArgumentException("Must supply a uiautomationText");
34+
}
35+
36+
return new ByAccessibilityId(id);
37+
}
38+
3139
public static class ByIosUIAutomation extends By implements Serializable {
3240

3341
private final String automationText;
@@ -73,6 +81,30 @@ public WebElement findElement(SearchContext context) {
7381
@Override
7482
public String toString() { return "By.AndroidUIAutomator: " + automatorText; }
7583
}
84+
85+
public static class ByAccessibilityId extends By implements Serializable {
86+
87+
private final String id;
88+
89+
public ByAccessibilityId(String id) {
90+
this.id = id;
91+
}
92+
93+
@Override
94+
public List<WebElement> findElements(SearchContext context) {
95+
return ((FindsByAccessibilityId) context).findElementsByAccessibilityId(id);
96+
}
97+
98+
@Override
99+
public WebElement findElement(SearchContext context) {
100+
return ((FindsByAccessibilityId) context).findElementByAccessibilityId(id);
101+
}
102+
103+
@Override
104+
public String toString() {
105+
return "By.AccessibilityId: " + id;
106+
}
107+
}
76108
}
77109

78110

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
+Copyright 2014 Appium contributors
3+
+Copyright 2014 Software Freedom Conservancy
4+
+
5+
+Licensed under the Apache License, Version 2.0 (the "License");
6+
+you may not use this file except in compliance with the License.
7+
+You may obtain a copy of the License at
8+
+
9+
+ http://www.apache.org/licenses/LICENSE-2.0
10+
+
11+
+Unless required by applicable law or agreed to in writing, software
12+
+distributed under the License is distributed on an "AS IS" BASIS,
13+
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
+See the License for the specific language governing permissions and
15+
+limitations under the License.
16+
+ */
17+
18+
package io.appium.java_client;
19+
20+
/**
21+
* An empty interface defining constants for the mobile commands defined in the Mobile JSON
22+
* wire protocol.
23+
*
24+
*/
25+
public interface MobileCommand {
26+
27+
String RESET = "reset";
28+
String GET_STRINGS = "getStrings";
29+
String KEY_EVENT = "keyEvent";
30+
31+
}

src/io/appium/java_client/MobileDriver.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
+Copyright 2014 Appium contributors
23
+Copyright 2014 Software Freedom Conservancy
34
+
45
+Licensed under the Apache License, Version 2.0 (the "License");
@@ -21,4 +22,30 @@
2122

2223
public interface MobileDriver extends WebDriver {
2324

25+
/**
26+
* Reset the currently running app for this session
27+
*/
28+
void resetApp();
29+
30+
/**
31+
* Get all defined Strings from an Android app
32+
*/
33+
String getAppStrings();
34+
35+
/**
36+
* Send a key event to the device
37+
*
38+
* @param key code for the key pressed on the device
39+
*/
40+
void sendKeyEvent(int key);
41+
42+
/**
43+
* Send a key event along with an Android metastate to an Android device
44+
* Metastates are things like *shift* to get uppercase characters
45+
*
46+
* @param key code for the key pressed on the Android device
47+
* @param metastate metastate for the keypress
48+
*/
49+
void sendKeyEvent(int key, Integer metastate);
50+
2451
}

src/io/appium/java_client/MobileErrorHandler.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/*
2+
+Copyright 2014 Appium contributors
3+
+Copyright 2014 Software Freedom Conservancy
4+
+
5+
+Licensed under the Apache License, Version 2.0 (the "License");
6+
+you may not use this file except in compliance with the License.
7+
+You may obtain a copy of the License at
8+
+
9+
+ http://www.apache.org/licenses/LICENSE-2.0
10+
+
11+
+Unless required by applicable law or agreed to in writing, software
12+
+distributed under the License is distributed on an "AS IS" BASIS,
13+
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
+See the License for the specific language governing permissions and
15+
+limitations under the License.
16+
+ */
17+
118
package io.appium.java_client;
219

320
import org.openqa.selenium.WebDriverException;

0 commit comments

Comments
 (0)