Skip to content

Commit 6d35898

Browse files
committed
Merge pull request #4 from Jonahss/uiautomator2
added -android uiautomator locator strategy
2 parents 3b817ad + 68aed34 commit 6d35898

File tree

6 files changed

+143
-3
lines changed

6 files changed

+143
-3
lines changed

src/io/appium/java_client/AppiumDriver.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
import java.util.Map;
2828
import java.util.Set;
2929

30-
public class AppiumDriver extends RemoteWebDriver implements MobileDriver, ContextAware, FindsByIosUIAutomation {
30+
public class AppiumDriver extends RemoteWebDriver implements MobileDriver, ContextAware, FindsByIosUIAutomation,
31+
FindsByAndroidUIAutomator {
3132

3233
private final MobileErrorHandler errorHandler = new MobileErrorHandler();
3334

@@ -103,4 +104,14 @@ public WebElement findElementByIosUIAutomation(String using) {
103104
public List<WebElement> findElementsByIosUIAutomation(String using) {
104105
return findElements("-ios uiautomation", using);
105106
}
107+
108+
@Override
109+
public WebElement findElementByAndroidUIAutomator(String using) {
110+
return findElement("-android uiautomator", using);
111+
}
112+
113+
@Override
114+
public List<WebElement> findElementsByAndroidUIAutomator(String using) {
115+
return findElements("-android uiautomator", using);
116+
}
106117
}
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 FindsByAndroidUIAutomator {
24+
WebElement findElementByAndroidUIAutomator(String using);
25+
26+
List<WebElement> findElementsByAndroidUIAutomator(String using);
27+
}

src/io/appium/java_client/MobileBy.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,20 @@ public abstract class MobileBy extends By {
1414

1515
public static By IosUIAutomation(final String uiautomationText) {
1616
if (uiautomationText == null) {
17-
throw new IllegalArgumentException("Must supply a uiautomationText");
17+
throw new IllegalArgumentException("Must supply an iOS UIAutomation string");
1818
}
1919

2020
return new ByIosUIAutomation(uiautomationText);
2121
}
2222

23+
public static By AndroidUIAutomator(final String uiautomatorText) {
24+
if (uiautomatorText == null) {
25+
throw new IllegalArgumentException("Must supply an Android UIAutomator string");
26+
}
27+
28+
return new ByAndroidUIAutomator(uiautomatorText);
29+
}
30+
2331
public static class ByIosUIAutomation extends By implements Serializable {
2432

2533
private final String automationText;
@@ -43,6 +51,28 @@ public String toString() {
4351
return "By.IosUIAutomation: " + automationText;
4452
}
4553
}
54+
55+
public static class ByAndroidUIAutomator extends By implements Serializable {
56+
57+
private final String automatorText;
58+
59+
public ByAndroidUIAutomator(String uiautomatorText) {
60+
automatorText = uiautomatorText;
61+
}
62+
63+
@Override
64+
public List<WebElement> findElements(SearchContext context) {
65+
return ((FindsByAndroidUIAutomator) context).findElementsByAndroidUIAutomator(automatorText);
66+
}
67+
68+
@Override
69+
public WebElement findElement(SearchContext context) {
70+
return ((FindsByAndroidUIAutomator) context).findElementByAndroidUIAutomator(automatorText);
71+
}
72+
73+
@Override
74+
public String toString() { return "By.AndroidUIAutomator: " + automatorText; }
75+
}
4676
}
4777

4878

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package io.appium.java_client;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.openqa.selenium.WebElement;
7+
import org.openqa.selenium.remote.CapabilityType;
8+
import org.openqa.selenium.remote.DesiredCapabilities;
9+
10+
import java.io.File;
11+
import java.net.URL;
12+
import java.util.List;
13+
14+
import static org.junit.Assert.assertEquals;
15+
import static org.junit.Assert.assertTrue;
16+
17+
/**
18+
* Test -android uiautomator locator strategy
19+
*/
20+
public class AndroidUIAutomatorTest {
21+
22+
private AppiumDriver driver;
23+
24+
@Before
25+
public void setup() throws Exception {
26+
File appDir = new File("test/io/appium/java_client");
27+
File app = new File(appDir, "ApiDemos-debug.apk.zip");
28+
DesiredCapabilities capabilities = new DesiredCapabilities();
29+
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
30+
capabilities.setCapability("device", "Android");
31+
capabilities.setCapability("app", app.getAbsolutePath());
32+
driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
33+
}
34+
35+
@After
36+
public void tearDown() throws Exception {
37+
driver.quit();
38+
}
39+
40+
@Test
41+
public void findElementTest() {
42+
WebElement element = driver.findElementByAndroidUIAutomator("new UiSelector().index(0)");
43+
assertEquals("android.widget.FrameLayout", element.getTagName());
44+
}
45+
46+
@Test
47+
public void findElementsTest() {
48+
List<WebElement> elements = driver.findElementsByAndroidUIAutomator("new UiSelector().clickable(true)");
49+
assertTrue(elements.size() > 11);
50+
}
51+
52+
@Test
53+
public void findElementByTest() {
54+
WebElement element = driver.findElement(MobileBy.AndroidUIAutomator("new UiSelector().index(0)"));
55+
assertEquals("android.widget.FrameLayout", element.getTagName());
56+
}
57+
58+
@Test
59+
public void findElementsByTest() {
60+
List<WebElement> elements = driver.findElements(MobileBy.AndroidUIAutomator("new UiSelector().clickable(true)"));
61+
assertTrue(elements.size() > 11);
62+
}
63+
64+
@Test(expected = IllegalArgumentException.class)
65+
public void ErrorTest() {
66+
driver.findElementByAndroidUIAutomator(null);
67+
}
68+
}
2.94 MB
Binary file not shown.

test/io/appium/java_client/IosUIAutomationTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import static org.junit.Assert.assertEquals;
1515

1616
/**
17-
* Test context-related features
17+
* Test -ios uiautomation locator strategy
1818
*/
1919
public class IosUIAutomationTest {
2020

@@ -63,4 +63,8 @@ public void MobileElementsByTest() {
6363
assertEquals(3, elements.size());
6464
}
6565

66+
@Test(expected = IllegalArgumentException.class)
67+
public void ErrorTest() {
68+
driver.findElementByIosUIAutomation(null);
69+
}
6670
}

0 commit comments

Comments
 (0)