Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

-android uiautomator locator strategy #3

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#IDE specific files
.idea/*
out/*
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ java-client
Java language binding for writing Appium Tests, conforms to [Mobile JSON Wire Protocol](https://code.google.com/p/selenium/source/browse/spec-draft.md?repo=mobile)

Depends upon the Selenium Java client library, available [here](http://docs.seleniumhq.org/download/)

##Project Status##
Appium 1.0 features/functions implemented so far:

-Context Switching (.context(), .getContextHandles(), getContext())
Binary file removed out/artifacts/java_client/java-client.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
36 changes: 28 additions & 8 deletions src/io/appium/java_client/AppiumDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@


import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.ContextAware;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.*;

import java.net.URL;
Expand All @@ -30,7 +27,8 @@
import java.util.Map;
import java.util.Set;

public class AppiumDriver extends RemoteWebDriver implements MobileDriver, ContextAware {
public class AppiumDriver extends RemoteWebDriver implements MobileDriver, ContextAware, FindsByIosUIAutomation,
FindsByAndroidUIAutomator {

private final MobileErrorHandler errorHandler = new MobileErrorHandler();

Expand All @@ -45,6 +43,7 @@ public AppiumDriver(URL remoteAddress, Capabilities desiredCapabilities){

}

@Override
protected Response execute(String driverCommand, Map<String, ?> parameters) {
try {
return super.execute(driverCommand, parameters);
Expand All @@ -56,14 +55,15 @@ protected Response execute(String driverCommand, Map<String, ?> parameters) {
"definitely in the Appium Driver");
}

@Override
protected Response execute(String command) {
return execute(command, ImmutableMap.<String, Object>of());
}





@Override
public WebDriver context(String name) {
if (name == null) {
throw new IllegalArgumentException("Must supply a context name");
Expand All @@ -74,7 +74,7 @@ public WebDriver context(String name) {
return AppiumDriver.this;
}


@Override
public Set<String> getContextHandles() {
Response response = execute(DriverCommand.GET_CONTEXT_HANDLES);
Object value = response.getValue();
Expand All @@ -86,12 +86,32 @@ public Set<String> getContextHandles() {
}
}


@Override
public String getContext() {
String contextName = String.valueOf(execute(DriverCommand.GET_CURRENT_CONTEXT_HANDLE).getValue());
if (contextName.equals("null")) {
return null;
}
return contextName;
}

@Override
public WebElement findElementByIosUIAutomation(String using) {
return findElement("-ios uiautomation", using);
}

@Override
public List<WebElement> findElementsByIosUIAutomation(String using) {
return findElements("-ios uiautomation", using);
}

@Override
public WebElement findElementByAndroidUIAutomator(String using) {
return findElement("-android uiautomator", using);
}

@Override
public List<WebElement> findElementsByAndroidUIAutomator(String using) {
return findElements("-android uiautomator", using);
}
}
27 changes: 27 additions & 0 deletions src/io/appium/java_client/FindsByAndroidUIAutomator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright 2014 Appium committers

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package io.appium.java_client;

import org.openqa.selenium.WebElement;

import java.util.List;

public interface FindsByAndroidUIAutomator {
WebElement findElementByAndroidUIAutomator(String using);

List<WebElement> findElementsByAndroidUIAutomator(String using);
}
27 changes: 27 additions & 0 deletions src/io/appium/java_client/FindsByIosUIAutomation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright 2014 Appium committers

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package io.appium.java_client;

import org.openqa.selenium.WebElement;

import java.util.List;

public interface FindsByIosUIAutomation {
WebElement findElementByIosUIAutomation(String using);

List<WebElement> findElementsByIosUIAutomation(String using);
}
78 changes: 78 additions & 0 deletions src/io/appium/java_client/MobileBy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package io.appium.java_client;

import org.openqa.selenium.By;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebElement;

import java.io.Serializable;
import java.util.List;

/**
* Created by jonahss on 4/10/14.
*/
public abstract class MobileBy extends By {

public static By IosUIAutomation(final String uiautomationText) {
if (uiautomationText == null) {
throw new IllegalArgumentException("Must supply an iOS UIAutomation string");
}

return new ByIosUIAutomation(uiautomationText);
}

public static By AndroidUIAutomator(final String uiautomatorText) {
if (uiautomatorText == null) {
throw new IllegalArgumentException("Must supply an Android UIAutomator string");
}

return new ByAndroidUIAutomator(uiautomatorText);
}

public static class ByIosUIAutomation extends By implements Serializable {

private final String automationText;

public ByIosUIAutomation(String uiautomationText) {
automationText = uiautomationText;
}

@Override
public List<WebElement> findElements(SearchContext context) {
return ((FindsByIosUIAutomation) context).findElementsByIosUIAutomation(automationText);
}

@Override
public WebElement findElement(SearchContext context) {
return ((FindsByIosUIAutomation) context).findElementByIosUIAutomation(automationText);
}

@Override
public String toString() {
return "By.IosUIAutomation: " + automationText;
}
}

public static class ByAndroidUIAutomator extends By implements Serializable {

private final String automatorText;

public ByAndroidUIAutomator(String uiautomatorText) {
automatorText = uiautomatorText;
}

@Override
public List<WebElement> findElements(SearchContext context) {
return ((FindsByAndroidUIAutomator) context).findElementsByAndroidUIAutomator(automatorText);
}

@Override
public WebElement findElement(SearchContext context) {
return ((FindsByAndroidUIAutomator) context).findElementByAndroidUIAutomator(automatorText);
}

@Override
public String toString() { return "By.AndroidUIAutomator: " + automatorText; }
}
}


68 changes: 68 additions & 0 deletions test/io/appium/java_client/AndroidUIAutomatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package io.appium.java_client;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.File;
import java.net.URL;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* Test -android uiautomator locator strategy
*/
public class AndroidUIAutomatorTest {

private AppiumDriver driver;

@Before
public void setup() throws Exception {
File appDir = new File("test/io/appium/java_client");
File app = new File(appDir, "ApiDemos-debug.apk.zip");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("device", "Android");
capabilities.setCapability("app", app.getAbsolutePath());
driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}

@After
public void tearDown() throws Exception {
driver.quit();
}

@Test
public void findElementTest() {
WebElement element = driver.findElementByAndroidUIAutomator("new UiSelector().index(0)");
assertEquals("android.widget.FrameLayout", element.getTagName());
}

@Test
public void findElementsTest() {
List<WebElement> elements = driver.findElementsByAndroidUIAutomator("new UiSelector().clickable(true)");
assertTrue(elements.size() > 11);
}

@Test
public void findElementByTest() {
WebElement element = driver.findElement(MobileBy.AndroidUIAutomator("new UiSelector().index(0)"));
assertEquals("android.widget.FrameLayout", element.getTagName());
}

@Test
public void findElementsByTest() {
List<WebElement> elements = driver.findElements(MobileBy.AndroidUIAutomator("new UiSelector().clickable(true)"));
assertTrue(elements.size() > 11);
}

@Test(expected = IllegalArgumentException.class)
public void ErrorTest() {
driver.findElementByAndroidUIAutomator(null);
}
}
Binary file added test/io/appium/java_client/ApiDemos-debug.apk
Binary file not shown.
3 changes: 2 additions & 1 deletion test/io/appium/java_client/ContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ContextTest {
@Before
public void setup() throws Exception {
File appDir = new File("test/io/appium/java_client");
File app = new File(appDir, "WebViewApp.app");
File app = new File(appDir, "WebViewApp.app.zip");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability(CapabilityType.VERSION, "7.1");
Expand Down Expand Up @@ -49,6 +49,7 @@ public void testGetContextHandles() {

@Test
public void testSwitchContext() {
driver.getContextHandles();
driver.context("WEBVIEW_1");
assertEquals(driver.getContext(), "WEBVIEW_1");
}
Expand Down
Loading