Skip to content

Commit

Permalink
updating java-client sample code
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonahss committed May 3, 2014
1 parent af5d3a3 commit 74bfc51
Show file tree
Hide file tree
Showing 19 changed files with 179 additions and 1,516 deletions.
4 changes: 2 additions & 2 deletions sample-code/examples/java/junit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ In order to run the tests, you will need to install [Apache Maven](http://maven.

You will then need to start appium, eg:

grunt appium
appium

To compile and run all tests, run:

mvn test

To run a single test, run:

mvn -Dtest=com.saucelabs.appium.SimpleTest test
mvn -Dtest=com.saucelabs.appium.SimpleTest test
5 changes: 5 additions & 0 deletions sample-code/examples/java/junit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
<version>LATEST</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package com.saucelabs.appium;

import io.appium.java_client.AppiumDriver;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.HasTouchScreen;
import org.openqa.selenium.interactions.TouchScreen;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteTouchScreen;
import org.openqa.selenium.remote.RemoteWebDriver;

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

public class AndroidContactsTest {
private WebDriver driver;
private AppiumDriver driver;

@Before
public void setUp() throws Exception {
Expand All @@ -26,12 +25,11 @@ public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("device","Android");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability(CapabilityType.VERSION, "4.2");
capabilities.setCapability(CapabilityType.PLATFORM, "MAC");
capabilities.setCapability(CapabilityType.VERSION, "4.4");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("app-package", "com.example.android.contactmanager");
capabilities.setCapability("app-activity", ".ContactManager");
driver = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}

@After
Expand All @@ -43,22 +41,11 @@ public void tearDown() throws Exception {
public void addContact(){
WebElement el = driver.findElement(By.name("Add Contact"));
el.click();
List<WebElement> textFieldsList = driver.findElements(By.tagName("textfield"));
List<WebElement> textFieldsList = driver.findElementsByClassName("android.widget.EditText");
textFieldsList.get(0).sendKeys("Some Name");
textFieldsList.get(2).sendKeys("Some@example.com");
driver.findElement(By.name("Save")).click();
driver.swipe(100, 500, 100, 100, 2);
driver.findElementByName("Save").click();
}

public class SwipeableWebDriver extends RemoteWebDriver implements HasTouchScreen {
private RemoteTouchScreen touch;

public SwipeableWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
super(remoteAddress, desiredCapabilities);
touch = new RemoteTouchScreen(getExecuteMethod());
}

public TouchScreen getTouch() {
return touch;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package com.saucelabs.appium;

import io.appium.java_client.AppiumDriver;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.HasTouchScreen;
import org.openqa.selenium.interactions.TouchScreen;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteTouchScreen;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.io.File;
import java.net.URL;
Expand All @@ -19,7 +17,7 @@

public class AndroidTest {

private WebDriver driver;
private AppiumDriver driver;

@Before
public void setUp() throws Exception {
Expand All @@ -29,12 +27,11 @@ public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("device","Android");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability(CapabilityType.VERSION, "4.2");
capabilities.setCapability(CapabilityType.PLATFORM, "MAC");
capabilities.setCapability(CapabilityType.VERSION, "4.4");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("app-package", "com.example.android.apis");
capabilities.setCapability("app-activity", ".ApiDemos");
driver = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}

@After
Expand All @@ -46,27 +43,14 @@ public void tearDown() throws Exception {
public void apiDemo(){
WebElement el = driver.findElement(By.name("Animation"));
assertEquals("Animation", el.getText());
el = driver.findElement(By.tagName("text"));
el = driver.findElementByClassName("android.widget.TextView");
assertEquals("API Demos", el.getText());
el = driver.findElement(By.name("App"));
el.click();
List<WebElement> els = driver.findElements(By.tagName("text"));
List<WebElement> els = driver.findElementsByClassName("android.widget.TextView");
assertEquals("Activity", els.get(2).getText());
}

public class SwipeableWebDriver extends RemoteWebDriver implements HasTouchScreen {
private RemoteTouchScreen touch;

public SwipeableWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
super(remoteAddress, desiredCapabilities);
touch = new RemoteTouchScreen(getExecuteMethod());
}

public TouchScreen getTouch() {
return touch;
}
}

}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,59 +1,54 @@
package com.saucelabs.appium;

import io.appium.java_client.AppiumDriver;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.HasTouchScreen;
import org.openqa.selenium.interactions.TouchScreen;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteTouchScreen;
import org.openqa.selenium.remote.RemoteWebDriver;

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

public class AndroidWebViewTest {
private WebDriver driver;
private AppiumDriver driver;

@Before
public void setUp() throws Exception {
// set up appium
File classpathRoot = new File(System.getProperty("user.dir"));
File app = new File(classpathRoot, "../../../apps/selendroid-test-app.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("device","selendroid");
capabilities.setCapability("automationName","selendroid");
capabilities.setCapability(CapabilityType.PLATFORM, "android");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("app-package", "io.selendroid.testapp");
capabilities.setCapability("app-activity", ".HomeScreenActivity");
driver = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
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 webView(){
public void webView() throws InterruptedException {
WebElement button = driver.findElement(By.id("buttonStartWebview"));
button.click();
driver.switchTo().window("WEBVIEW");
Thread.sleep(6000);
Set<String> contextNames = driver.getContextHandles();
for (String contextName : contextNames) {
System.out.println(contextName);
if (contextName.contains("WEBVIEW")){
driver.context(contextName);
}
}
WebElement inputField = driver.findElement(By.id("name_input"));
inputField.sendKeys("Some name");
inputField.submit();
}


public class SwipeableWebDriver extends RemoteWebDriver implements HasTouchScreen {
private RemoteTouchScreen touch;

public SwipeableWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
super(remoteAddress, desiredCapabilities);
touch = new RemoteTouchScreen(getExecuteMethod());
}

public TouchScreen getTouch() {
return touch;
}
}

}
Original file line number Diff line number Diff line change
@@ -1,49 +1,35 @@
package com.saucelabs.appium;

import java.net.URL;

import org.apache.http.HttpEntity;
import com.google.gson.JsonParser;
import io.appium.java_client.AppiumDriver;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
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 org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.RemoteWebElement;

import com.google.gson.JsonParser;
import java.net.URL;

public class MobileFindJavaTest {

private RemoteWebDriver driver;
private AppiumDriver driver;
private static final String url = "http://127.0.0.1:4723/wd/hub";
private static final HttpClient client = HttpClients.createDefault();
private static final JsonParser parser = new JsonParser();

@Test
public void apiDemo() throws Exception {
final WebElement about_phone = scroll_to("about phone");
final String about_phone = scroll_to("about phone");
if (about_phone != null) {
System.out.println("scrolled to: " + about_phone.getText());
System.out.println("scrolled to: aboutPhone");
System.out.println("returned: " + about_phone);
}

scroll_to("bluetooth");
}

/** Create a new remote web element. **/
public RemoteWebElement newElement(final String elementId) {
final RemoteWebElement element = new RemoteWebElement();
element.setParent(driver);
element.setId(elementId);
element.setFileDetector(driver.getFileDetector());
return element;
}

// @formatter:off
/*
Expand All @@ -65,52 +51,24 @@ public RemoteWebElement newElement(final String elementId) {
end
*/
// @formatter:on
public WebElement scroll_to(String text) {
RemoteWebElement element = null;
try {
text = text.replaceAll("\"", "\\\""); // quotes must be escaped.
final String jsonString = "{\"script\":\"mobile: find\",\"args\":[[\"scroll\",[[3,\""
+ text + "\"]],[[7,\"" + text + "\"]]]]}";
final String id = driver.getSessionId().toString();
final String executeURL = url + "/session/" + id + "/execute";

final HttpPost post = new HttpPost(executeURL);
post.setEntity(new StringEntity(jsonString, "UTF8"));
post.setHeader("Content-type", "application/json");

final HttpEntity responseEntity = client.execute(post).getEntity();

if (responseEntity != null) {
try {
final String responseString = EntityUtils.toString(responseEntity);
// {"status":0,"value":{"ELEMENT":"1"},"sessionId":"8e982755-980f-4036-b3d1-c0e14e890273"}
final String elementId = parser.parse(responseString)
.getAsJsonObject().get("value").getAsJsonObject().get("ELEMENT")
.getAsString();

element = newElement(elementId);
} catch (final Exception e) {
e.printStackTrace();
} finally {
EntityUtils.consume(responseEntity);
}
}
} catch (final Exception e) {
e.printStackTrace();
}
return element;
public String scroll_to(String text) {

text = text.replaceAll("\"", "\\\""); // quotes must be escaped.
final String[] jsonString = {"\"scroll\"","[[3,\"" + text + "\"]]","[[7,\"" + text + "\"]]"};


return driver.complexFind(jsonString);

}

@Before
public void setUp() throws Exception {
final DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("device", "Android");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability(CapabilityType.VERSION, "4.2");
capabilities.setCapability(CapabilityType.PLATFORM, "MAC");
capabilities.setCapability("app-package", "com.android.settings");
capabilities.setCapability("app-activity", ".Settings");
driver = new RemoteWebDriver(new URL(url), capabilities);
capabilities.setCapability("device", "android");
capabilities.setCapability(CapabilityType.PLATFORM, "android");
capabilities.setCapability("appPackage", "com.android.settings");
capabilities.setCapability("appActivity", ".Settings");
driver = new AppiumDriver(new URL(url), capabilities);
}

@After
Expand Down

0 comments on commit 74bfc51

Please sign in to comment.