Skip to content

Commit cd842f7

Browse files
committed
Merge pull request #6 from Jonahss/currentActivity
currentActivity, setValue
2 parents 3ffce8a + 15c6cac commit cd842f7

File tree

7 files changed

+122
-5
lines changed

7 files changed

+122
-5
lines changed

src/io/appium/java_client/AppiumDriver.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public AppiumDriver(URL remoteAddress, Capabilities desiredCapabilities){
4343
.put(RESET, postC("/session/:sessionId/appium/app/reset"))
4444
.put(GET_STRINGS, getC("/session/:sessionId/appium/app/strings"))
4545
.put(KEY_EVENT, postC("/session/:sessionId/appium/device/keyevent"))
46+
.put(CURRENT_ACTIVITY, getC("/session/:sessionId/appium/device/current_activity"))
47+
.put(SET_VALUE, postC("/session/:sessionId/appium/element/:id/value"))
4648
;
4749
ImmutableMap<String, CommandInfo> mobileCommands = builder.build();
4850

@@ -52,7 +54,7 @@ public AppiumDriver(URL remoteAddress, Capabilities desiredCapabilities){
5254
}
5355

5456
@Override
55-
protected Response execute(String driverCommand, Map<String, ?> parameters) {
57+
public Response execute(String driverCommand, Map<String, ?> parameters) {
5658
try {
5759
return super.execute(driverCommand, parameters);
5860
} catch (WebDriverException ex) {
@@ -90,6 +92,11 @@ public void sendKeyEvent(int key, Integer metastate) {
9092
execute(KEY_EVENT, parameters);
9193
}
9294

95+
public String currentActivity() {
96+
Response response = execute(CURRENT_ACTIVITY);
97+
return response.getValue().toString();
98+
}
99+
93100

94101
@Override
95102
public WebDriver context(String name) {

src/io/appium/java_client/MobileCommand.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,8 @@ public interface MobileCommand {
2727
String RESET = "reset";
2828
String GET_STRINGS = "getStrings";
2929
String KEY_EVENT = "keyEvent";
30+
String CURRENT_ACTIVITY = "currentActivity";
31+
String SET_VALUE = "setValue";
32+
3033

3134
}

src/io/appium/java_client/MobileDriver.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@
1919

2020

2121
import org.openqa.selenium.WebDriver;
22+
import org.openqa.selenium.remote.Response;
23+
24+
import java.util.Map;
2225

2326
public interface MobileDriver extends WebDriver {
2427

28+
29+
public Response execute(String driverCommand, Map<String, ?> parameters);
30+
2531
/**
2632
* Reset the currently running app for this session
2733
*/
@@ -48,4 +54,9 @@ public interface MobileDriver extends WebDriver {
4854
*/
4955
void sendKeyEvent(int key, Integer metastate);
5056

57+
/**
58+
* Get the current activity being run on the mobile device
59+
*/
60+
String currentActivity();
61+
5162
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
import com.google.common.collect.ImmutableMap;
21+
import org.openqa.selenium.By;
22+
import org.openqa.selenium.WebElement;
23+
import org.openqa.selenium.remote.*;
24+
25+
import java.util.List;
26+
import java.util.Map;
27+
28+
public class MobileElement extends RemoteWebElement {
29+
30+
private String foundBy;
31+
protected FileDetector fileDetector;
32+
33+
private WebElement webElement;
34+
private MobileDriver parent;
35+
36+
public MobileElement(RemoteWebElement originalElement, MobileDriver parentDriver) {
37+
38+
webElement = originalElement;
39+
this.id = originalElement.getId();
40+
this.parent = parentDriver;
41+
//The super doesn't need the parent object at ALL, it's terrible that it asks for one, when it only needs it for setFoundBy()
42+
super.setParent(new FakeRemoteWebDriver());
43+
}
44+
45+
public List<WebElement> findElements(By by) {
46+
return by.findElements(this);
47+
}
48+
49+
public WebElement findElement(By by) {
50+
return by.findElement(this);
51+
}
52+
53+
public WebElement findElementByIosUIAutomation(String using) { return findElement("-ios uiautomation", using); }
54+
55+
public List<WebElement> findElementsByIosUIAutomation(String using) { return findElements("-ios uiautomation", using); }
56+
57+
public WebElement findElementByAndroidUIAutomator(String using) { return findElement("-android uiautomator", using); }
58+
59+
public List<WebElement> findElementsByAndroidUIAutomator(String using) { return findElements("-android uiautomator", using); }
60+
61+
public WebElement findElementByAccessibilityId(String using) { return findElement("accessibility id", using); }
62+
63+
public List<WebElement> findElementsByAccessibilityId(String using) { return findElements("accessibility id", using); }
64+
65+
public void setValue(String value) {
66+
ImmutableMap.Builder builder = ImmutableMap.builder();
67+
builder.put("id", id).put("value", value);
68+
execute(MobileCommand.SET_VALUE, builder.build());
69+
}
70+
71+
protected Response execute(String command, Map<String, ?> parameters) {
72+
return parent.execute(command, parameters);
73+
}
74+
75+
private class FakeRemoteWebDriver extends RemoteWebDriver {
76+
public FakeRemoteWebDriver() {
77+
//hahaha do NOTHING!
78+
}
79+
}
80+
}

test/io/appium/java_client/AccessibilityIdTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,4 @@ public void MobileElementsByTest() {
6262
List<WebElement> elements = driver.findElements(MobileBy.AccessibilityId("UICatalog"));
6363
assertEquals(1, elements.size());
6464
}
65-
6665
}

test/io/appium/java_client/MobileDriverAndroidTest.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import java.io.File;
2727
import java.net.URL;
2828

29+
import static org.junit.Assert.assertEquals;
30+
2931
/**
3032
* Test Mobile Driver features
3133
*/
@@ -50,18 +52,24 @@ public void tearDown() throws Exception {
5052
}
5153

5254
@Test
53-
public void getStrings() {
55+
public void getStringsTest() {
5456
String strings = driver.getAppStrings();
5557
assert(strings.length() > 100);
5658
}
5759

5860
@Test
59-
public void keyEvent() {
61+
public void keyEventTest() {
6062
driver.sendKeyEvent(AndroidKeyCode.HOME);
6163
}
6264

6365
@Test
64-
public void keyEventWithMetastate() {
66+
public void keyEventWithMetastateTest() {
6567
driver.sendKeyEvent(AndroidKeyCode.SPACE, AndroidKeyMetastate.META_SHIFT_ON);
6668
}
69+
70+
@Test
71+
public void currentActivityTest() {
72+
String activity = driver.currentActivity();
73+
assertEquals(".ApiDemos", activity);
74+
}
6775
}

test/io/appium/java_client/MobileDriverIOSTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.junit.Test;
2323
import org.openqa.selenium.remote.CapabilityType;
2424
import org.openqa.selenium.remote.DesiredCapabilities;
25+
import org.openqa.selenium.remote.RemoteWebElement;
2526

2627
import java.io.File;
2728
import java.net.URL;
@@ -56,4 +57,12 @@ public void resetTest() {
5657
driver.resetApp();
5758
}
5859

60+
@Test
61+
public void setValueTest() {
62+
MobileElement element = new MobileElement((RemoteWebElement)driver.findElementByAccessibilityId("TextFields, Uses of UITextField"), driver);
63+
element.click();
64+
element = new MobileElement((RemoteWebElement)driver.findElementByAccessibilityId("Normal"), driver);
65+
element.setValue("Grace Hopper");
66+
}
67+
5968
}

0 commit comments

Comments
 (0)