Skip to content

Commit 3b817ad

Browse files
committed
Merge pull request #2 from Jonahss/uiautomation
added -ios uiautomation locator strategy
2 parents 347a658 + 1f23364 commit 3b817ad

File tree

21 files changed

+159
-10
lines changed

21 files changed

+159
-10
lines changed

src/io/appium/java_client/AppiumDriver.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818

1919

2020
import com.google.common.collect.ImmutableMap;
21-
import org.openqa.selenium.Capabilities;
22-
import org.openqa.selenium.ContextAware;
23-
import org.openqa.selenium.WebDriver;
24-
import org.openqa.selenium.WebDriverException;
21+
import org.openqa.selenium.*;
2522
import org.openqa.selenium.remote.*;
2623

2724
import java.net.URL;
@@ -30,7 +27,7 @@
3027
import java.util.Map;
3128
import java.util.Set;
3229

33-
public class AppiumDriver extends RemoteWebDriver implements MobileDriver, ContextAware {
30+
public class AppiumDriver extends RemoteWebDriver implements MobileDriver, ContextAware, FindsByIosUIAutomation {
3431

3532
private final MobileErrorHandler errorHandler = new MobileErrorHandler();
3633

@@ -45,6 +42,7 @@ public AppiumDriver(URL remoteAddress, Capabilities desiredCapabilities){
4542

4643
}
4744

45+
@Override
4846
protected Response execute(String driverCommand, Map<String, ?> parameters) {
4947
try {
5048
return super.execute(driverCommand, parameters);
@@ -56,14 +54,15 @@ protected Response execute(String driverCommand, Map<String, ?> parameters) {
5654
"definitely in the Appium Driver");
5755
}
5856

57+
@Override
5958
protected Response execute(String command) {
6059
return execute(command, ImmutableMap.<String, Object>of());
6160
}
6261

6362

6463

6564

66-
65+
@Override
6766
public WebDriver context(String name) {
6867
if (name == null) {
6968
throw new IllegalArgumentException("Must supply a context name");
@@ -74,7 +73,7 @@ public WebDriver context(String name) {
7473
return AppiumDriver.this;
7574
}
7675

77-
76+
@Override
7877
public Set<String> getContextHandles() {
7978
Response response = execute(DriverCommand.GET_CONTEXT_HANDLES);
8079
Object value = response.getValue();
@@ -86,12 +85,22 @@ public Set<String> getContextHandles() {
8685
}
8786
}
8887

89-
88+
@Override
9089
public String getContext() {
9190
String contextName = String.valueOf(execute(DriverCommand.GET_CURRENT_CONTEXT_HANDLE).getValue());
9291
if (contextName.equals("null")) {
9392
return null;
9493
}
9594
return contextName;
9695
}
96+
97+
@Override
98+
public WebElement findElementByIosUIAutomation(String using) {
99+
return findElement("-ios uiautomation", using);
100+
}
101+
102+
@Override
103+
public List<WebElement> findElementsByIosUIAutomation(String using) {
104+
return findElements("-ios uiautomation", using);
105+
}
97106
}
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 FindsByIosUIAutomation {
24+
WebElement findElementByIosUIAutomation(String using);
25+
26+
List<WebElement> findElementsByIosUIAutomation(String using);
27+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package io.appium.java_client;
2+
3+
import org.openqa.selenium.By;
4+
import org.openqa.selenium.SearchContext;
5+
import org.openqa.selenium.WebElement;
6+
7+
import java.io.Serializable;
8+
import java.util.List;
9+
10+
/**
11+
* Created by jonahss on 4/10/14.
12+
*/
13+
public abstract class MobileBy extends By {
14+
15+
public static By IosUIAutomation(final String uiautomationText) {
16+
if (uiautomationText == null) {
17+
throw new IllegalArgumentException("Must supply a uiautomationText");
18+
}
19+
20+
return new ByIosUIAutomation(uiautomationText);
21+
}
22+
23+
public static class ByIosUIAutomation extends By implements Serializable {
24+
25+
private final String automationText;
26+
27+
public ByIosUIAutomation(String uiautomationText) {
28+
automationText = uiautomationText;
29+
}
30+
31+
@Override
32+
public List<WebElement> findElements(SearchContext context) {
33+
return ((FindsByIosUIAutomation) context).findElementsByIosUIAutomation(automationText);
34+
}
35+
36+
@Override
37+
public WebElement findElement(SearchContext context) {
38+
return ((FindsByIosUIAutomation) context).findElementByIosUIAutomation(automationText);
39+
}
40+
41+
@Override
42+
public String toString() {
43+
return "By.IosUIAutomation: " + automationText;
44+
}
45+
}
46+
}
47+
48+

test/io/appium/java_client/ContextTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class ContextTest {
2121
@Before
2222
public void setup() throws Exception {
2323
File appDir = new File("test/io/appium/java_client");
24-
File app = new File(appDir, "WebViewApp.app");
24+
File app = new File(appDir, "WebViewApp.app.zip");
2525
DesiredCapabilities capabilities = new DesiredCapabilities();
2626
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
2727
capabilities.setCapability(CapabilityType.VERSION, "7.1");
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
16+
/**
17+
* Test context-related features
18+
*/
19+
public class IosUIAutomationTest {
20+
21+
private AppiumDriver driver;
22+
23+
@Before
24+
public void setup() throws Exception {
25+
File appDir = new File("test/io/appium/java_client");
26+
File app = new File(appDir, "UICatalog.app.zip");
27+
DesiredCapabilities capabilities = new DesiredCapabilities();
28+
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
29+
capabilities.setCapability(CapabilityType.VERSION, "7.1");
30+
capabilities.setCapability(CapabilityType.PLATFORM, "Mac");
31+
capabilities.setCapability("device", "iPhone Simulator");
32+
capabilities.setCapability("app", app.getAbsolutePath());
33+
driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
34+
}
35+
36+
@After
37+
public void tearDown() throws Exception {
38+
driver.quit();
39+
}
40+
41+
@Test
42+
public void findElementTest() {
43+
WebElement element = driver.findElementByIosUIAutomation(".elements()[0]");
44+
assertEquals(element.getAttribute("name"), "UICatalog");
45+
}
46+
47+
@Test
48+
public void findElementsTest() {
49+
List<WebElement> elements = driver.findElementsByIosUIAutomation("elements()");
50+
assertEquals(3, elements.size());
51+
}
52+
53+
@Test
54+
public void MobileElementByTest() {
55+
WebElement element = driver.findElement(MobileBy.IosUIAutomation(".elements()[0]"));
56+
System.out.println(element);
57+
assertEquals(element.getAttribute("name"), "UICatalog");
58+
}
59+
60+
@Test
61+
public void MobileElementsByTest() {
62+
List<WebElement> elements = driver.findElements(MobileBy.IosUIAutomation(".elements()"));
63+
assertEquals(3, elements.size());
64+
}
65+
66+
}
353 KB
Binary file not shown.
28.9 KB
Binary file not shown.
-18.2 KB
Binary file not shown.
-6.39 KB
Binary file not shown.
-15.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)